diff options
Diffstat (limited to 'src/main/java/org/bukkit/plugin/TimedRegisteredListener.java')
-rw-r--r-- | src/main/java/org/bukkit/plugin/TimedRegisteredListener.java | 38 |
1 files changed, 27 insertions, 11 deletions
diff --git a/src/main/java/org/bukkit/plugin/TimedRegisteredListener.java b/src/main/java/org/bukkit/plugin/TimedRegisteredListener.java index ed25e179..d86805b9 100644 --- a/src/main/java/org/bukkit/plugin/TimedRegisteredListener.java +++ b/src/main/java/org/bukkit/plugin/TimedRegisteredListener.java @@ -11,7 +11,7 @@ import org.bukkit.event.Listener; public class TimedRegisteredListener extends RegisteredListener { private int count; private long totalTime; - private Event event; + private Class<? extends Event> eventClass; private boolean multiple = false; public TimedRegisteredListener(final Listener pluginListener, final EventExecutor eventExecutor, final EventPriority eventPriority, final Plugin registeredPlugin, final boolean listenCancelled) { @@ -25,17 +25,25 @@ public class TimedRegisteredListener extends RegisteredListener { return; } count++; - if (this.event == null) { - this.event = event; - } - else if (!this.event.getClass().equals(event.getClass())) { + Class<? extends Event> newEventClass = event.getClass(); + if (this.eventClass == null) { + this.eventClass = newEventClass; + } else if (!this.eventClass.equals(newEventClass)) { multiple = true; + this.eventClass = getCommonSuperclass(newEventClass, this.eventClass).asSubclass(Event.class); } long start = System.nanoTime(); super.callEvent(event); totalTime += System.nanoTime() - start; } + private static Class<?> getCommonSuperclass(Class<?> class1, Class<?> class2) { + while (!class1.isAssignableFrom(class2)) { + class1 = class1.getSuperclass(); + } + return class1; + } + /** * Resets the call count and total time for this listener */ @@ -63,18 +71,26 @@ public class TimedRegisteredListener extends RegisteredListener { } /** - * Gets the first event this listener handled + * Gets the class of the events this listener handled. If it handled + * multiple classes of event, the closest shared superclass will be + * returned, such that for any event this listener has handled, + * <code>this.getEventClass().isAssignableFrom(event.getClass())</code> + * and no class + * <code>this.getEventClass().isAssignableFrom(clazz) + * && this.getEventClass() != clazz && + * event.getClass().isAssignableFrom(clazz)</code> for all handled events. * - * @return An event handled by this RegisteredListener + * @return the event class handled by this RegisteredListener */ - public Event getEvent() { - return event; + public Class<? extends Event> getEventClass() { + return eventClass; } /** - * Gets whether this listener has handled multiple events + * Gets whether this listener has handled multiple events, such that for + * some two events, <code>eventA.getClass() != eventB.getClass()</code>. * - * @return True if this listener has handled multiple events + * @return true if this listener has handled multiple events */ public boolean hasMultiple() { return multiple; |