From 84372f7cd5ff1dff0288acec3282d1c5b8db495d Mon Sep 17 00:00:00 2001 From: Bukkit/Spigot Date: Thu, 19 Jan 2012 08:13:49 +0100 Subject: [PATCH] Use the type of the eventhandler method's parameter as type rather than specificing it. Thanks zml2008. By: Erik Broes --- .../main/java/org/bukkit/event/CustomEventListener.java | 2 -- .../src/main/java/org/bukkit/event/EventHandler.java | 9 ++++++++- .../src/main/java/org/bukkit/plugin/PluginLoader.java | 1 + .../src/main/java/org/bukkit/plugin/PluginManager.java | 2 ++ .../java/org/bukkit/plugin/java/JavaPluginLoader.java | 9 +++++---- 5 files changed, 16 insertions(+), 7 deletions(-) diff --git a/paper-api/src/main/java/org/bukkit/event/CustomEventListener.java b/paper-api/src/main/java/org/bukkit/event/CustomEventListener.java index afdf924a6f..26d2dbda33 100644 --- a/paper-api/src/main/java/org/bukkit/event/CustomEventListener.java +++ b/paper-api/src/main/java/org/bukkit/event/CustomEventListener.java @@ -1,7 +1,5 @@ package org.bukkit.event; -import org.bukkit.event.Listener; - /** * Handles all custom events */ diff --git a/paper-api/src/main/java/org/bukkit/event/EventHandler.java b/paper-api/src/main/java/org/bukkit/event/EventHandler.java index 42c0036887..34c70adb56 100644 --- a/paper-api/src/main/java/org/bukkit/event/EventHandler.java +++ b/paper-api/src/main/java/org/bukkit/event/EventHandler.java @@ -1,15 +1,22 @@ package org.bukkit.event; +import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; /** * An annotation to mark methods as being event handler methods */ +@Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface EventHandler { - Class event(); + /** + * This field is now fetched from the event handler method's parameter + * @return + */ + @Deprecated Class event() default Event.class; EventPriority priority() default EventPriority.NORMAL; } diff --git a/paper-api/src/main/java/org/bukkit/plugin/PluginLoader.java b/paper-api/src/main/java/org/bukkit/plugin/PluginLoader.java index bc4e849fe7..2a9dc10f91 100644 --- a/paper-api/src/main/java/org/bukkit/plugin/PluginLoader.java +++ b/paper-api/src/main/java/org/bukkit/plugin/PluginLoader.java @@ -52,6 +52,7 @@ public interface PluginLoader { * @param type Type of the event executor to create * @param listener the object that will handle the eventual call back * @return The new executor + * @deprecated see PluginLoader#createRegisteredListeners */ @Deprecated public EventExecutor createExecutor(Event.Type type, Listener listener); diff --git a/paper-api/src/main/java/org/bukkit/plugin/PluginManager.java b/paper-api/src/main/java/org/bukkit/plugin/PluginManager.java index ea59dcdebc..c2930e288f 100644 --- a/paper-api/src/main/java/org/bukkit/plugin/PluginManager.java +++ b/paper-api/src/main/java/org/bukkit/plugin/PluginManager.java @@ -103,6 +103,7 @@ public interface PluginManager { * @param listener Listener to register * @param priority Priority of this event * @param plugin Plugin to register + * @deprecated see PluginManager#registerEvents */ @Deprecated public void registerEvent(Event.Type type, Listener listener, Priority priority, Plugin plugin); @@ -115,6 +116,7 @@ public interface PluginManager { * @param executor EventExecutor to register * @param priority Priority of this event * @param plugin Plugin to register + * @deprecated see PluginManager#registerEvent(Class, Listener, EventPriority, EventExecutor, Plugin) */ @Deprecated public void registerEvent(Event.Type type, Listener listener, EventExecutor executor, Priority priority, Plugin plugin); diff --git a/paper-api/src/main/java/org/bukkit/plugin/java/JavaPluginLoader.java b/paper-api/src/main/java/org/bukkit/plugin/java/JavaPluginLoader.java index 46fe3340f9..cfa41c6c89 100644 --- a/paper-api/src/main/java/org/bukkit/plugin/java/JavaPluginLoader.java +++ b/paper-api/src/main/java/org/bukkit/plugin/java/JavaPluginLoader.java @@ -987,20 +987,21 @@ public class JavaPluginLoader implements PluginLoader { final EventHandler eh = method.getAnnotation(EventHandler.class); if (eh == null) continue; final Class checkClass = method.getParameterTypes()[0]; - if (!checkClass.isAssignableFrom(eh.event()) || method.getParameterTypes().length != 1) { + if (!Event.class.isAssignableFrom(checkClass) || method.getParameterTypes().length != 1) { plugin.getServer().getLogger().severe("Wrong method arguments used for event type registered"); continue; } + final Class eventClass = checkClass.asSubclass(Event.class); method.setAccessible(true); - Set eventSet = ret.get(eh.event()); + Set eventSet = ret.get(eventClass); if (eventSet == null) { eventSet = new HashSet(); - ret.put(eh.event(), eventSet); + ret.put(eventClass, eventSet); } eventSet.add(new RegisteredListener(listener, new EventExecutor() { public void execute(Listener listener, Event event) throws EventException { try { - if (!checkClass.isAssignableFrom(event.getClass())) { + if (!eventClass.isAssignableFrom(event.getClass())) { throw new EventException("Wrong event type passed to registered method"); } method.invoke(listener, event);