Best Way to Register Events from other Classes

Discussion in 'Plugin Development' started by Goty_Metal, Dec 3, 2012.

Thread Status:
Not open for further replies.
  1. Well i know 2 ways, but i don't know the best (performance):

    - First:
    In your main:

    Code:
    PluginManager pm = getServer().getPluginManager();
            pm.registerEvents(new MyBlockListener(), this);
    In every class:

    Code:
    public class MyBlockListener implements Listener{
        public static Main plugin;
    - Second:
    In your main:

    Code:
     public final MyBlockListener mbl = new MyBlockListener(this);
    In every class:

    Code:
    public MyBlockListener(Main instance){plugin = instance;}
    

    THANKS!
     
  2. Offline

    CorrieKay

    I dont see how a singular event (registering an event) should matter. I think it would be simpler for each listener to register themselves in their constructors.

    For the record, i have a superclass that automatically registers the events of its subclasses in the superconstructor :p
     
  3. Can you tell me your "method" please? :3
     
  4. Offline

    CorrieKay

    Sure! All of my "modules" in my plugin have a superclass that contains various different utility methods, blah blah, in short, they have a superclass that all must extend. In the superclass, it contains a for loop that iterates over the methods in the class for event, and checks if theyre listener methods. This is the point in which i deviate from the norm, as its not necessary to actually register your listener methods this way. (if you were interested however, you can see how this process works from my source code)

    Thats an obtuse way to do what you want though, basically, in the constructor, have it register the classes listeners, thats it. :p
     
  5. Wow that's really smart, i don't know enought java to do that by myselft, so i think i'll use normal method, but i have yours to learn more about it, really thanks man :)
     
Thread Status:
Not open for further replies.

Share This Page