Tip on keeping Listener in the same class as JavaPlugin.

Discussion in 'Resources' started by sebastiannielsen, May 7, 2012.

Thread Status:
Not open for further replies.
  1. Found out this today, So I share this with fellow developers here. I currently use this in version 1.2 of my ChunkAutoClaimer plugin.
    You don't need to put your listener in a separate class.
    You don't need to use a anonymous inner class for your listener.

    Just do this:
    Code:java
    1.  
    2. public class CLASSNAME extends JavaPlugin implements Listener {
    3. public void onEnable() {
    4. getServer().getPluginManager().registerEvents(this,this);
    5. }
    6. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    7. //Example command handler.
    8. }
    9. @EventHandler
    10. public void blockplace(BlockPlaceEvent e) {
    11. //Example event.
    12. }
    13. }
    14.  


    Basically, you let your main class both extend JavaPlugin and implement Listener at the same time. This result in one single .java, and upon compiling, one single .class
    Another good thing is that I shaved off a couple of kilobytes from the final .jar file, since the total size of the listener and JavaPlugin class, was larger than the single double-usage .class.

    Tested and works perfectly.
     
    ferrybig likes this.
  2. Offline

    Kanlaki101

    This is perfectly fine for small plugins, but for will be a disaster for larger plugins.
    It's better to organize it into another class file, just like "normal".

    But overall, nice tip.
     
  3. Yes if your .class and/or .jar begins to approach 4GB (which is the limit for FAT32) then it can be a good thing to separate classes. But I don't think craftbukkit could handle such large plugins, they are larger than minecraft :)

    I haven't noticed any ill effect of letting the class share JavaPlugin and Listener.
    I don't think there will be any ill effects even in a large plugin.

    MAYBE there would be ill effects if you interface other plugin's exposed API, if they share the same function/sub names. But then, its only to make sure you select a another name for your functions/subs.
     
  4. Offline

    heisan213

    I like to keep all my fancy code and the confusing things behind the sceenes. Makes it easier to debug for me :D
    So, I usually have one huge Helper class where I have most of my methods and the fancy schnancy code in ;) Also that's nice because you dont have to copy/paste your own code if you were to re-use it.

    But, great tip, anyway :D I never thought of this unroll now.
     
  5. I am using the same class in most of my personal projects, its located at the same packageat every pluign, and has this inside onEnable():
    if (this instanceof Listener)
    {
    this.getServer().getPluginManager().registerEvents((Listener) this, this);
    }
    Saves some memory foutprint
     
  6. Offline

    ZachBora

    My map is above 15gb.
     
Thread Status:
Not open for further replies.

Share This Page