From 9ad981d22ae760853915514f6223355524c856af Mon Sep 17 00:00:00 2001 From: asofold Date: Fri, 16 Nov 2012 15:04:07 +0100 Subject: [PATCH] ListenerManager: Add annotation for MethodOrder. Change overriding principle. Annotation overrides ComponentWithName overrides given tag. Annotation overrides IHaveMethodOrder overrides given MethodOrder. --- .../nocheatplus/event/GenericListener.java | 3 +++ .../nocheatplus/event/ListenerManager.java | 21 ++++++++++++++----- .../nocheatplus/event/MethodOrder.java | 11 ++++++++++ 3 files changed, 30 insertions(+), 5 deletions(-) create mode 100644 src/fr/neatmonster/nocheatplus/event/MethodOrder.java diff --git a/src/fr/neatmonster/nocheatplus/event/GenericListener.java b/src/fr/neatmonster/nocheatplus/event/GenericListener.java index 2e28dc0e..017d65b8 100644 --- a/src/fr/neatmonster/nocheatplus/event/GenericListener.java +++ b/src/fr/neatmonster/nocheatplus/event/GenericListener.java @@ -31,6 +31,9 @@ public class GenericListener implements Listener, EventExecutor * */ public static class MethodOrder{ + public static final MethodOrder getMethodOrder(fr.neatmonster.nocheatplus.event.MethodOrder anno){ + return anno.beforeTag().isEmpty() ? null : new MethodOrder(anno.beforeTag()); + } public final String beforeTag; /** * You can use regular expressions for beforeTag, so it will be checked from position 0 on.
diff --git a/src/fr/neatmonster/nocheatplus/event/ListenerManager.java b/src/fr/neatmonster/nocheatplus/event/ListenerManager.java index ae0d87cd..5284cc6b 100644 --- a/src/fr/neatmonster/nocheatplus/event/ListenerManager.java +++ b/src/fr/neatmonster/nocheatplus/event/ListenerManager.java @@ -106,16 +106,19 @@ public class ListenerManager { /** * This registers all methods that have the @EventHandler annotation.
* Interfaces checked if arguments are not given: IHaveMethodOrder (order), ComponentWithName (tag)
- * NOTE: Does not do any super class checking. + * NOTES:
+ * - Does not do any super class checking.
+ * - Given MethodOrder overridden by implementing IHaveMethodOrder overridden by per method @MethodOrder annotations.
+ * - Given tag overridden by Listener implementing ComponentWithName overridden by per method @MEthodOrder annotations.
* @param listener - * @param tag Identifier for the registering plugin / agent, null is not discouraged, but null entries are ignored concerning sortin order. + * @param tag Identifier for the registering plugin / agent, null is not discouraged, but null entries are ignored concerning sorting order. * @param order Allows to register before other tags or just first. Expect MethodOrder to change in near future. The method order of already registered methods will not be compared to. */ public void registerAllEventHandlers(Listener listener, String tag, MethodOrder order){ - if (order == null && listener instanceof IHaveMethodOrder){ + if (listener instanceof IHaveMethodOrder){ order = ((IHaveMethodOrder) listener).getMethodOrder(); } - if (tag == null && listener instanceof ComponentWithName){ + if (listener instanceof ComponentWithName){ // TODO: maybe change to an interface only defined here. tag = ((ComponentWithName) listener).getComponentName(); } @@ -149,7 +152,15 @@ public class ListenerManager { continue; } Class checkedEventType = eventType.asSubclass(Event.class); - getListener(checkedEventType, anno.priority()).addMethodEntry(new MethodEntry(listener, method, anno.ignoreCancelled(), tag, order)); + MethodOrder tempOrder = order; + String tempTag = tag; + fr.neatmonster.nocheatplus.event.MethodOrder orderAnno = method.getAnnotation(fr.neatmonster.nocheatplus.event.MethodOrder.class); + if (orderAnno != null){ + MethodOrder veryTempOrder = tempOrder = MethodOrder.getMethodOrder(orderAnno); + if (veryTempOrder != null) tempOrder = veryTempOrder; + if (!orderAnno.tag().isEmpty()) tempTag = orderAnno.tag(); + } + getListener(checkedEventType, anno.priority()).addMethodEntry(new MethodEntry(listener, method, anno.ignoreCancelled(), tempTag, tempOrder)); } } diff --git a/src/fr/neatmonster/nocheatplus/event/MethodOrder.java b/src/fr/neatmonster/nocheatplus/event/MethodOrder.java new file mode 100644 index 00000000..2d820176 --- /dev/null +++ b/src/fr/neatmonster/nocheatplus/event/MethodOrder.java @@ -0,0 +1,11 @@ +package fr.neatmonster.nocheatplus.event; + +/** + * Annotation to allow per-method method-order. Empty strings are regarded as "not set". + * @author mc_dev + * + */ +public @interface MethodOrder { + public String tag() default ""; + public String beforeTag() default ""; +}