Unregistering a Listener

Discussion in 'Plugin Development' started by zajacmp3, Aug 1, 2012.

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

    zajacmp3

    Hello,

    I am wondering can I unregister an event?

    Code:
                this.getServer().getPluginManager().registerEvents(new Listener(){
                    @SuppressWarnings("unused")
                    @EventHandler(priority = EventPriority.NORMAL)
                    public void onDeath(final PlayerDeathEvent event) throws InterruptedException{
                        listOfQualifiedPlayers.remove(event.getEntity().getName());
                        Bukkit.broadcastMessage(event.getEntity().getName()+" Died in combat");
                        if(listOfQualifiedPlayers.size()==1){
                            Thread.sleep(200);
                            finishBattle();
                        }
                    }
                }, this);
    
    Let's take this for an example. I want to unregister it after listOfQualifiedPlayers.size will give me int value of 1.
    I just don't want useless code running over and over and over again
    I obviously could do what I want in other way and I will if there is not way to unregister it.
     
  2. Offline

    CorrieKay

    just do nothing if the size == 1
     
  3. Offline

    zajacmp3

    No shit... Sorry but as I said
    Is there a way to unregister it? This is not a frequent event really (death event). But a move event is something that a lot of players do and I wonder how much does it slow the game if event have to be checked constantly.
     
  4. Offline

    CorrieKay

    Looking into the plugin manager, it doesnt seem as though its possible on a cursory glance. However, i could be wrong.

    Also, if you use a movement listener, and do your checks in a decent order (trying to exit as fast as you can) it wont slow down the server. (well, not guarenteed, but... you get the picture :p)
     
  5. Offline

    Malikk

    The only way, afaik, is to to set the instance of that listener class back to null. But by that point, you have to have another listener class to toggle the second listener class null and back, so it hardly seems worth it.

    Honestly tho, this isn't something you need to worry about. There are methods in Bukkit, which runs thousands of lines of code a second; running 3 or 4 lines to check the amount of players really isn't going to affect anything.

    Also, you shouldn't use Thread.sleep *facepalm* This is not delaying your plugin, but the server's main thread. This means EVERYTHING the server is doing completely stops for 200 ticks... Have a look at Delayed Tasks.
     
  6. Offline

    nisovin

    Code:
    HandlerList.unregisterAll(listener);
    
    You'll have to actually have a Listener object instead of using an anonymous class, of course.
     
  7. Offline

    zajacmp3

    Thanks for telling me it :) When new bukkit would appear and I will be testing this I really would have a problem what the hell is going on :) Thread.sleep at the time seemed like a wise and simple idea :)

    That's something I can work with. First I will print myself what is really registered to figure out how to operate it :)
     
  8. Offline

    Digi

    Firefly, desht and CorrieKay like this.
  9. Offline

    CorrieKay

  10. Offline

    Firefly

Thread Status:
Not open for further replies.

Share This Page