Skip to content

Commit

Permalink
Allow removal of listeners / commands by plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
roblabla authored and md-5 committed Sep 14, 2013
1 parent 1dca12c commit 80e23d6
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Eclipse stuff
.classpath
.project
.settings
.settings/

# netbeans
nbproject/
Expand Down
49 changes: 49 additions & 0 deletions api/src/main/java/net/md_5/bungee/api/plugin/PluginManager.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -10,6 +12,7 @@
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;
Expand Down Expand Up @@ -43,6 +46,8 @@ public class PluginManager
private final Map<String, Plugin> plugins = new LinkedHashMap<>();
private final Map<String, Command> commandMap = new HashMap<>();
private Map<String, PluginDescription> toLoad = new HashMap<>();
private Multimap<Plugin, Command> commandsByPlugin = ArrayListMultimap.create();
private Multimap<Plugin, Listener> listenersByPlugin = ArrayListMultimap.create();

@SuppressWarnings("unchecked")
public PluginManager(ProxyServer proxy)
Expand All @@ -64,6 +69,7 @@ public void registerCommand(Plugin plugin, Command command)
{
commandMap.put( alias.toLowerCase(), command );
}
commandsByPlugin.put( plugin, command );
}

/**
Expand All @@ -74,6 +80,21 @@ public void registerCommand(Plugin plugin, Command command)
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<Command> it = commandsByPlugin.get( plugin ).iterator(); it.hasNext(); )
{
commandMap.values().remove( it.next() );
it.remove();
}
}

public boolean dispatchCommand(CommandSender sender, String commandLine)
Expand Down Expand Up @@ -341,5 +362,33 @@ public void registerListener(Plugin plugin, Listener listener)
"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<Listener> it = listenersByPlugin.get( plugin ).iterator(); it.hasNext(); )
{
eventBus.unregister( it.next() );
it.remove();
}
}
}

0 comments on commit 80e23d6

Please sign in to comment.