Conditional event registration

Discussion in 'Plugin Development' started by darkhelmet, Feb 20, 2013.

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

    darkhelmet

    What's the best method to register event listeners conditionally? For performance I want to move how my plugins check for enabled features, so that the check is before we even register events. This way, the plugin won't begin to process events only to then end when they find the feature is disabled.

    I've considered using the .registerEvent but a) there are mixed statements on whether it's no longer recommended, b) there seem to be mixed ways to implement them, and c) seem really verbose to me, making listeners with multiple events sure is nicer.

    Or is there a different method that would be easier to skip registering events on startup?
     
  2. Offline

    Deckerz

    if(feature.isEnabled()){
    new EventListner(this, this);
    }
     
  3. Register entire listener classes and then unregister them (using HandlerList.unregisterAll(listener) ) if they get disabled on config reload.

    You can also unregister individual events by accessing the event class, e.g. PlayerMoveEvent.getHandlerList().unregisterAll(plugin or listener)
     
  4. Offline

    darkhelmet

    Is having a listener for every event the best way? Some of my plugins will have *a lot* (60+).
     
  5. Offline

    mastermustard

    there is no purpose in that it would most likely cause more lag with that many listeners. I usually tend to break my listeners by event category for neatness
     
  6. Offline

    darkhelmet

    We currently have 69 actions that are likely handled by 55 total events, broken down into 5 listener classes. The issue is, I need conditionally register every one. Not an entire listener class, but any number of the individual events.
     
  7. You can just register them all like normal then go through your config options and disable them individually if needed (code in my previous post)... and when you reload you first unregister them all from your plugin, re-register them and un-register each.... it should be pretty quick considering it only adds stuff to lists, but you should add a debug timer on it with all events disabled from config to see how much time it takes to enable them all and then disable them all =)

    Oh and you might want to add a "disable all" option to avoid enabling all to just disable them all one by one, but first test it out.
     
Thread Status:
Not open for further replies.

Share This Page