Listeners

Discussion in 'Spigot Plugin Development' started by RiotShielder, May 2, 2015.

  1. Hey, pretty simple question here.

    Is it considered good practice to move my listeners out of my main plugin class into their own separate one? If it is, should I create one class for all my listeners, or one per listener?

    Thanks.
     
  2. Depends on the size.
     
  3. gigosaurus

    Supporter

    It depends on the size of the plugin and the methods. Generally having a separate class for each listener is a good idea, but in some cases it can be overkill especially if you're just cancelling several events.

    If you end up spending a lot of time scrolling through classes to find the EventHandler method you are looking for, then that is a fairly good sign that you should break it up into multiple classes.
     
    • Like Like x 3
  4. I have a fairly small plugin, 3 commands, 3 listeners. It's more of for the sake of getting into the habit of creating conventional code.
     
  5. @RiotShielder

    I prefer to make a different listener for each category of things.
    Its all about organising your code ;)

    e.g. a listener for detecting double-jumping, a seperate listener to add more damage to a diamond sword, another listener to change the color of the chat.

    Its all about organising your code so its easy for you (and others) to browse your code.
     
    • Like Like x 2
  6. Thanks everyone!

    I ended up doing this, and created a different class for each listener. It might be a bit unnecessary considering the size of my plugin, but I want to get used to doing things the right way.

    I ended up with a fairly small main class too.
    Code (Text):
    package com.shepherdjerred.stservermessages;

    import org.bukkit.plugin.java.JavaPlugin;

    import com.shepherdjerred.stservermessages.commands.me;
    import com.shepherdjerred.stservermessages.commands.reload;
    import com.shepherdjerred.stservermessages.commands.say;
    import com.shepherdjerred.stservermessages.listeners.commandpreprocess;
    import com.shepherdjerred.stservermessages.listeners.joinquit;
    import com.shepherdjerred.stservermessages.listeners.ping;

    public class stservermessages extends JavaPlugin {
        @Override
        public void onEnable() {
            this.saveDefaultConfig();
            getServer().getPluginManager().registerEvents(new joinquit(this), this);
            getServer().getPluginManager().registerEvents(new ping(this), this);
            getServer().getPluginManager().registerEvents(new commandpreprocess(this), this);
            this.getCommand("say").setExecutor(new say(this));
            this.getCommand("me").setExecutor(new me(this));
            this.getCommand("stsm").setExecutor(new reload(this));
        }
    }
     
  7. If you have more than one listener, I'd use separate classes. I almost always do this for commands/listeners.

    @RiotShielder
     
  8. In that case, I myself put multiple similar events or events that have something to do together into one class. I still write it into comments in my main class which Listener is in which class when I register them. As @Plo457 already mentioned, It's all about organising ;)
     
  9. Capitalize your classes.
     
  10. I fixed that after another person had pointed it out to me, thanks!