Registering listeners not in the main class

Discussion in 'Plugin Development' started by TheRoflcopter, Sep 29, 2014.

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

    TheRoflcopter

    I'm really confused with plugin instances..

    Currently I have 2 listeners. One listener get's registered in the onEnable() method, the other listener get's registered in it's own class (basically, 1 minigame = 1 listener, there are multiple minigames).

    What I want to do is that when a minigame ends, I want to unregister the listener that the minigame was using by doing HandlerList.unregisterAll(plugin).

    The result is either none of the listeners get's unregistered or ALL the listeners get unregistered... :(

    My code :

    The main listener that get's registered once, and continues to listen while the server is running.
    Code:java
    1.  
    2. public static Core plugin;
    3.  
    4. public final mainListener listener= new mainListener();
    5.  
    6. public void onEnable(){
    7. plugin = this;
    8. new test(this); // <- for the temporary listener
    9. .....
    10. }



    The other listener that should be registered temporarily and unregistered when the minigame ends.
    Code:java
    1. public class test implements Listener {
    2.  
    3. static Core plugin;
    4.  
    5. public test(Core plugin) {
    6. Core.plugin.getServer().getPluginManager().registerEvents(this, Core.plugin );
    7. }



    Now, how do i need to unregister the temporary listener? I tried doing this, but then both listeners get unregistered..
    Code:java
    1. HandlerList.unregisterAll(plugin);
     
  2. Offline

    fireblast709

    TheRoflcopter HandlerList.unregisterAll(<Listener instance>) exists for that cause ;3. And please, remove that static plugin instance (static doesn't exist for your convenience)
     
    es359 likes this.
  3. Offline

    TheRoflcopter

    How do i make a listener instance?

    EDIT: I tried removing the static plugin instance, which basically lead to deleting all the static's in my code (P.S there were a lot of them), but i couldn't make an instance of
    Code:java
    1. public abstract class GameCountdown extends BukkitRunnable
     
  4. For your "test" class, add a method named "destroy" for example, and add HandlerList.unregisterAll(this) to it. That should unregister the listener. Usage:

    test test1 = new test(coreplugin);
    After minigame ends:
    test1.destroy();
     
    TheRoflcopter likes this.
  5. Offline

    nuno1212sss

    *Just a guess, instead of Core.plugin, you could do Bukkit.getServer() ...
     
    TheRoflcopter likes this.
  6. Offline

    TheRoflcopter

    Didn't work :/ It still doesn't unregister the temp. listener...

    Updated code :

    Code:java
    1. private Core plugin;
    2.  
    3. public test(Core plugin) {
    4.  
    5. this.plugin = plugin;
    6. }
    7.  
    8.  
    9.  
    10. public boolean start(){
    11.  
    12. Bukkit.getServer().getPluginManager().registerEvents(this, plugin );
    13. .....
    14. }
    15.  
    16.  
    17.  
    18. public void killEventListeners(){
    19. HandlerList.unregisterAll(this);
    20. }
     
  7. Offline

    TheRoflcopter

Thread Status:
Not open for further replies.

Share This Page