From 80e23d664677795e25fc6a728dbd3ed3b99fecb5 Mon Sep 17 00:00:00 2001 From: Robin Lambertz Date: Sun, 15 Sep 2013 06:52:46 +1000 Subject: [PATCH] Allow removal of listeners / commands by plugin --- .gitignore | 2 +- .../md_5/bungee/api/plugin/PluginManager.java | 49 +++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index f5552fd2..0fde6ec5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,7 @@ # Eclipse stuff .classpath .project -.settings +.settings/ # netbeans nbproject/ diff --git a/api/src/main/java/net/md_5/bungee/api/plugin/PluginManager.java b/api/src/main/java/net/md_5/bungee/api/plugin/PluginManager.java index 4178f522..3ed43896 100644 --- a/api/src/main/java/net/md_5/bungee/api/plugin/PluginManager.java +++ b/api/src/main/java/net/md_5/bungee/api/plugin/PluginManager.java @@ -1,6 +1,8 @@ package net.md_5.bungee.api.plugin; import com.google.common.base.Preconditions; +import com.google.common.collect.ArrayListMultimap; +import com.google.common.collect.Multimap; import com.google.common.eventbus.Subscribe; import java.io.File; import java.io.InputStream; @@ -10,6 +12,7 @@ import java.net.URLClassLoader; import java.util.Arrays; import java.util.Collection; import java.util.HashMap; +import java.util.Iterator; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -43,6 +46,8 @@ public class PluginManager private final Map plugins = new LinkedHashMap<>(); private final Map commandMap = new HashMap<>(); private Map toLoad = new HashMap<>(); + private Multimap commandsByPlugin = ArrayListMultimap.create(); + private Multimap listenersByPlugin = ArrayListMultimap.create(); @SuppressWarnings("unchecked") public PluginManager(ProxyServer proxy) @@ -64,6 +69,7 @@ public class PluginManager { commandMap.put( alias.toLowerCase(), command ); } + commandsByPlugin.put( plugin, command ); } /** @@ -74,6 +80,21 @@ public class PluginManager public void unregisterCommand(Command command) { commandMap.values().remove( command ); + commandsByPlugin.values().remove( command ); + } + + /** + * Unregister all commands owned by a {@link Plugin} + * + * @param plugin the plugin to register the commands of + */ + public void unregisterCommands(Plugin plugin) + { + for ( Iterator it = commandsByPlugin.get( plugin ).iterator(); it.hasNext(); ) + { + commandMap.values().remove( it.next() ); + it.remove(); + } } public boolean dispatchCommand(CommandSender sender, String commandLine) @@ -341,5 +362,33 @@ public class PluginManager "Listener %s has registered using deprecated subscribe annotation! Please update to @EventHandler.", listener ); } eventBus.register( listener ); + listenersByPlugin.put( plugin, listener ); + } + + /** + * Unregister a {@link Listener} so that the events do not reach it anymore. + * + * @param listener the listener to unregister + * @throws IllegalArgumentException if the listener was not previously + * registered + */ + public void unregisterListener(Listener listener) + { + eventBus.unregister( listener ); + listenersByPlugin.values().remove( listener ); + } + + /** + * Unregister all of a Plugin's listener. + * + * @param plugin + */ + public void unregisterListeners(Plugin plugin) + { + for ( Iterator it = listenersByPlugin.get( plugin ).iterator(); it.hasNext(); ) + { + eventBus.unregister( it.next() ); + it.remove(); + } } }