Use ComponentWithName to set the tag parameter (managed listeners).

This commit is contained in:
asofold 2012-11-08 10:58:43 +01:00
parent 138431c413
commit 04761af8b8
2 changed files with 21 additions and 11 deletions

View File

@ -33,6 +33,7 @@ import fr.neatmonster.nocheatplus.checks.inventory.InventoryListener;
import fr.neatmonster.nocheatplus.checks.moving.MovingListener;
import fr.neatmonster.nocheatplus.command.CommandHandler;
import fr.neatmonster.nocheatplus.command.INotifyReload;
import fr.neatmonster.nocheatplus.components.ComponentWithName;
import fr.neatmonster.nocheatplus.components.INeedConfig;
import fr.neatmonster.nocheatplus.components.NoCheatPlusAPI;
import fr.neatmonster.nocheatplus.config.ConfPaths;
@ -182,6 +183,9 @@ public class NoCheatPlus extends JavaPlugin implements Listener, NoCheatPlusAPI
private boolean manageListeners = true;
/**
* Interfaces checked for managed listeners: IHaveMethodOrder (method), ComponentWithName (tag)<br>
*/
@Override
public void addComponent(final Object obj) {
if (obj instanceof Listener) {
@ -195,10 +199,18 @@ public class NoCheatPlus extends JavaPlugin implements Listener, NoCheatPlusAPI
}
dataMan.addComponent(obj);
}
/**
* Interfaces checked for managed listeners: IHaveMethodOrder (method), ComponentWithName (tag)<br>
* @param listener
*/
private void addListener(final Listener listener) {
if (manageListeners){
listenerManager.registerAllEventHandlers(listener, "NoCheatPlus");
String tag = "NoCheatPlus";
if (listener instanceof ComponentWithName){
tag = ((ComponentWithName) listener).getComponentName();
}
listenerManager.registerAllEventHandlers(listener, tag);
listeners.add(listener);
}
else{

View File

@ -13,6 +13,7 @@ import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.plugin.Plugin;
import fr.neatmonster.nocheatplus.components.ComponentWithName;
import fr.neatmonster.nocheatplus.event.GenericListener.MethodEntry;
import fr.neatmonster.nocheatplus.event.GenericListener.MethodEntry.MethodOrder;
import fr.neatmonster.nocheatplus.utilities.CheckUtils;
@ -93,6 +94,7 @@ public class ListenerManager {
/**
* This registers all declared methods that have the @EventHandler annotation.<br>
* Interfaces checked if arguments are not given: IHaveMethodOrder (order), ComponentWithName (tag)<br>
* NOTE: Does not do any super class checking.
* @param listener
* @param tag Identifier for the registering plugin / agent, null is not discouraged, but null entries are ignored concerning sortin order.
@ -103,6 +105,7 @@ public class ListenerManager {
/**
* This registers all methods that have the @EventHandler annotation.<br>
* Interfaces checked if arguments are not given: IHaveMethodOrder (order), ComponentWithName (tag)<br>
* NOTE: Does not do any super class checking.
* @param listener
* @param tag Identifier for the registering plugin / agent, null is not discouraged, but null entries are ignored concerning sortin order.
@ -112,6 +115,10 @@ public class ListenerManager {
if (order == null && listener instanceof IHaveMethodOrder){
order = ((IHaveMethodOrder) listener).getMethodOrder();
}
if (tag == null && listener instanceof ComponentWithName){
// TODO: maybe change to an interface only defined here.
tag = ((ComponentWithName) listener).getComponentName();
}
Class<?> clazz = listener.getClass();
Set<Method> allMethods = new HashSet<Method>();
for (Method method : clazz.getMethods()){
@ -146,15 +153,6 @@ public class ListenerManager {
}
}
protected static boolean extendsEvent(Class<?> eventType) {
Class<?> superClass = eventType.getSuperclass();
while (superClass != null && superClass != Object.class){
if (superClass == Event.class) return true;
superClass = superClass.getSuperclass();
}
return false;
}
/**
* TODO: more methods for tags ? (+ return something?).
* @param listener