1.16.5 How to cancel just ONE listener.

Discussion in 'Spigot Plugin Development' started by Kingskew, Jun 9, 2021.

  1. How do I cancel just one listener, not the whole entire plugin? For example: if I has a listener that checked for an action, then another listener that checked for an item, how do I cancel just one of those listeners and not the whole plugin.

    code so far:
    Code (Text):
    package me.computer.DiamondsRich.commands;

    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;

    import me.computer.DiamondsRich.Main;

    public class Stopdiarich implements CommandExecutor {
        private Main plugin;
       
        public Stopdiarich(Main plugin) {
            this.plugin = plugin;
            plugin.getCommand("stopdiarich").setExecutor(this);
        }
       
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
           
            Player p = (Player) sender;
            p.getServer().getScheduler().cancelTasks(this.plugin);
            p.getServer().broadcastMessage("Stopping Diamond Rich");
       
            return false;
        }
    }
     
     
    • Like Like x 1
  2. If they’re in the same class this isn’t possible possible though. If they are in different classes you can use the method above.
     
  3. Well, yes and no.
    You can take a specific event and remove a specific listener/plugin from its handler list.
    Example:
    Code (Java):
    PlayerQuitEvent.getHandlerList().unregister(plugin/listener);
     
    #4 Andross, Jun 9, 2021
    Last edited: Jun 9, 2021
    • Like Like x 1
  4. What do I put in the (Listener) thing? I tried to put in the name of the listener, but it didnt work.
     
  5. You have to put your listener instance:
    Code (Java):
    final YourListener myListener = new YourListener();
    // Register your listener
    plugin.getServer().getPluginManager().registerEvents(myListener, plugin);
    // Unregister your listener
    HandlerList.unregister(myListener);
     
  6. Keep in mind that the mentioned methods have it's flaws and will permanently cancel it until it got registered anew
     
    • Agree Agree x 1
  7. Strahan

    Benefactor

    Why though? Just have logic in the listener to determine if it should proceed or not. Seems much easier to me than trying to fiddle with the listener registration.
     
    • Agree Agree x 1
  8. it doesn't work. I did the top code, then the unregister thing.
     
  9. You can just add logic in your listener to check if is enabled or not.

    For example adding a boolean flag and check if is true or false.
     
    #10 MrNadix, Jun 9, 2021
    Last edited: Jun 9, 2021
    • Agree Agree x 1
  10. Strahan

    Benefactor

    Yep, that's what I was getting at. The idea of unregistering/registering a listener as needed is akin to wanting to punish your kid by not letting him drive the car so you jack it up and remove all the wheels instead of, you know, just taking away the keys :)
     
    • Friendly Friendly x 1