ListenerManager: Add annotation for MethodOrder. Change overriding

principle.

Annotation overrides ComponentWithName overrides given tag. Annotation
overrides IHaveMethodOrder overrides given MethodOrder.
This commit is contained in:
asofold 2012-11-16 15:04:07 +01:00
parent a10ddc9c37
commit 9ad981d22a
3 changed files with 30 additions and 5 deletions

View File

@ -31,6 +31,9 @@ public class GenericListener<E extends Event> implements Listener, EventExecutor
* *
*/ */
public static class MethodOrder{ 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; public final String beforeTag;
/** /**
* You can use regular expressions for beforeTag, so it will be checked from position 0 on.<br> * You can use regular expressions for beforeTag, so it will be checked from position 0 on.<br>

View File

@ -106,16 +106,19 @@ public class ListenerManager {
/** /**
* This registers all methods that have the @EventHandler annotation.<br> * This registers all methods that have the @EventHandler annotation.<br>
* Interfaces checked if arguments are not given: IHaveMethodOrder (order), ComponentWithName (tag)<br> * Interfaces checked if arguments are not given: IHaveMethodOrder (order), ComponentWithName (tag)<br>
* NOTE: Does not do any super class checking. * NOTES: <br>
* - Does not do any super class checking.<br>
* - Given MethodOrder overridden by implementing IHaveMethodOrder overridden by per method @MethodOrder annotations.<br>
* - Given tag overridden by Listener implementing ComponentWithName overridden by per method @MEthodOrder annotations.<br>
* @param listener * @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. * @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){ public void registerAllEventHandlers(Listener listener, String tag, MethodOrder order){
if (order == null && listener instanceof IHaveMethodOrder){ if (listener instanceof IHaveMethodOrder){
order = ((IHaveMethodOrder) listener).getMethodOrder(); order = ((IHaveMethodOrder) listener).getMethodOrder();
} }
if (tag == null && listener instanceof ComponentWithName){ if (listener instanceof ComponentWithName){
// TODO: maybe change to an interface only defined here. // TODO: maybe change to an interface only defined here.
tag = ((ComponentWithName) listener).getComponentName(); tag = ((ComponentWithName) listener).getComponentName();
} }
@ -149,7 +152,15 @@ public class ListenerManager {
continue; continue;
} }
Class<? extends Event> checkedEventType = eventType.asSubclass(Event.class); Class<? extends Event> 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));
} }
} }

View File

@ -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 "";
}