Multiple Listener Classes

Discussion in 'Plugin Development' started by haydenaa, Dec 16, 2014.

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

    haydenaa

    Hello. This is a really quick question. So I am in process of making a (huge) plugin implementing a currently popular video game into minecraft. I have a huge listeners class so I was wondering if I could have multiple classes that implement listener and than register them in my onEnable? Someone else told me to use one listener class, but i'm not sure if he/she is correct. Thanks.
     
  2. Offline

    Tecno_Wizard

    @haydenaa, nope, you can have as many listener classes as you want within reasonable bounds.
     
    mine-care likes this.
  3. Offline

    ZeldoKavira

    I've deleted all the off topic posts on this thread. Keep it on topic and consider yourselves lucky I didnt warn for every post, half of you would be banned right now. There is no need to start insulting each other.
     
    ChipDev and Burnett like this.
  4. Offline

    ChipDev

    @haydenaa You make a class that implements Listener, then register your class in the onEnable :)
     
  5. Offline

    Webbeh

    To re-write what I told before, that got deleted by ZeldoKavira :

    @haydenaa : I'm using the exact invert as you do. All the classes that need an event listener actually contain them. I have over 200 listeners dispatched in approx 40 classes, and it works without issue for me.
     
  6. Offline

    haydenaa

  7. Offline

    Webbeh

    There might be a few advantages to use a single class that contains all listeners (that you dispatch yourself afterwards) :

    - It frees space in the onEnable, you only have one line to write there to register all events
    - If all the classes that use the same listener have the same "conditions" or "filters" (for example, all entity-related events must have their entities be instance of Player), you can filter the result there and save a few lines of code
    - It can be easier and faster to re-read and find what is registered, and in which order the classes use the event
    - Maybe a few more advantages

    The only con I see when doing that is that you can't make use of the event priorities, unless you write two listeners in the same class.

    I don't know about performance, didn't benchmark but I could probably do that someday.
     
  8. Offline

    gamemster2468

    For games, I personally would just create a different class for each "game" listener.

    So death event, join event, quit event etc.

    Then for the rest (that are literally just cancelled events) I would have a "Misc Class" that contains all of them.

    Also here's an example of how you would register more than one listener in your onEnable:

    http://hastebin.com/isoqusedey.java
     
Thread Status:
Not open for further replies.

Share This Page