okay so why doesnt this work? should be easy to answer

Discussion in 'Plugin Development' started by eamike261, Feb 22, 2011.

Thread Status:
Not open for further replies.
  1. Offline

    eamike261

    I've been fighting with this a lot, I'm not sure what's wrong with the code. I'm making an extremely simple plugin that will turn any block placed into a TNT block...here's what I've got:

    edit: excuse the poor naming of the class and variables

    By the way, the game does not display the message, or replace the block, so I'm guessing the problem is with the event being triggered.

    Code:
    public class BlockMod extends JavaPlugin {
    
        PlayListen playerListener;
    
        public BlockMod(PluginLoader pluginLoader, Server instance, PluginDescriptionFile desc, File folder, File plugin, ClassLoader cLoader) {
            super(pluginLoader, instance, desc, folder, plugin, cLoader);
    
            playerListener = new PlayListen(this);
            onEnable();
        }
    
        public void onDisable() {
        }
    
        public void onEnable() {
            PluginManager pm = getServer().getPluginManager();
            pm.registerEvent(Event.Type.BLOCK_PLACED, playerListener, Event.Priority.Normal, this);
        }
    }
    Code:
    public class PlayListen extends BlockListener {
    
        BlockMod plugin;
        Block block;
        Player player;
    
        PlayListen(BlockMod instance) {
            plugin = instance;
        }
    
        void blockPlaced(BlockPlaceEvent event) {
            block = event.getBlockPlaced();
            player = event.getPlayer();
            block.setTypeId(46);
            player.sendMessage("Oops!");
        }
    }
     
  2. Offline

    flyslasher

    Code:
    void blockPlaced(BlockPlaceEvent event) {
    should be
    Code:
    public void blockPlaced(BlockPlaceEvent event) {
     
  3. Offline

    Nohup

    actually it should be:

    Code:
        @Override
        public void onBlockPlace(BlockPlaceEvent event) {
            // TODO Auto-generated method stub
            super.onBlockPlace(event);
        }
    
    Are you using an IDE to code your plugin? Eclipse is free, and then you can set up your dependencies to provide code-complete functionality that would help out tremendously.
     
  4. Offline

    eamike261

    I am using eclipse for this, which I thought set variables and methods to public by default when compiled...regardless, that did not fix it.

    Why call super.onBlockPlace? I was just following the Basic pluggin tutorial. I don't see what could be wrong, it's an extremely simple pluggin.

    Like I said, it doesn't print the message or change the block, so I must have messed the listener somehow, or not attached it properly, right?
     
  5. Offline

    Edward Hand

    Ah. I missed your post the first time round. Sorry.

    First, I've never used @override or super.onBlockPlace() in my plugins and they've always worked fine...

    So, a couple of things to note:

    1) You need to make sure you use the correct function name in your listeners. When the BLOCK_PLACED event is called, it will go to your BlockListener class and call the onBlockPlace function. Since you have not defined a function with that name, it will do nothing.

    So change:
    Code:
    void blockPlaced(BlockPlaceEvent event) {
    to
    Code:
    public void onBlockPlace(BlockPlaceEvent event) {
    (to find out what the functions should be called, look at the BlockListener class, or whatever listener you happen to be extending)

    I'm pretty sure that's the only reason its not working.

    2) Calling onEnable() in your constructor is unnecessary. onEnable() will call itself.

    3) As of earlier this week, the way you setup your main plugin class has changed (so make sure to update your bukkit/craftbukkit). You'll need to delete this bit:
    Code:
    public BlockMod(PluginLoader pluginLoader, Server instance, PluginDescriptionFile desc, File folder, File plugin, ClassLoader cLoader) {
            super(pluginLoader, instance, desc, folder, plugin, cLoader);
    
            playerListener = new PlayListen(this);
            onEnable();
        }
    and move playerListener = new PlayListen(this); to onEnable()
    (the tutorials wont have been updated for that yet)

    Let us know if you have any more trouble.
     
  6. Offline

    Nohup

    Sorry, I guess I was a bit vague in my reply. My point was the name of the method was wrong in the OP and the first reply.

    If you are using Eclipse and you have your dependencies configured, just hitting ctrl-space will open up a selection menu for you to choose the on... methods and it will structure them properly for you. The super call was just the default code that was generated. It isn't needed, just what the code template drops in when you override a method.

    As far as the override annotation is concerned, it is a safety mechanism that will ensure you override things the proper way. In the OP case @Override would have told them that there was no method called blockPlaced to override. It will also do checks to make sure parameters are correct and what not. The Javadoc description is:

    "Indicates that a method declaration is intended to override a method declaration in a superclass. If a method is annotated with this annotation type but does not override a superclass method, compilers are required to generate an error message."

    So again, basically a clean code/code-safety measure but will help to keep you from making certain mistakes :)
     
  7. Offline

    eamike261

    Ahhh okay, it works, thank you guys! Very good information.
     
Thread Status:
Not open for further replies.

Share This Page