Use the type of the eventhandler method's parameter as type rather than specificing it. Thanks zml2008.

By: Erik Broes <erikbroes@grum.nl>
This commit is contained in:
Bukkit/Spigot 2012-01-19 08:13:49 +01:00
parent 0c50b9eeb1
commit 84372f7cd5
5 changed files with 16 additions and 7 deletions

View file

@ -1,7 +1,5 @@
package org.bukkit.event; package org.bukkit.event;
import org.bukkit.event.Listener;
/** /**
* Handles all custom events * Handles all custom events
*/ */

View file

@ -1,15 +1,22 @@
package org.bukkit.event; package org.bukkit.event;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/** /**
* An annotation to mark methods as being event handler methods * An annotation to mark methods as being event handler methods
*/ */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
public @interface EventHandler { public @interface EventHandler {
Class<? extends Event> event(); /**
* This field is now fetched from the event handler method's parameter
* @return
*/
@Deprecated Class<? extends Event> event() default Event.class;
EventPriority priority() default EventPriority.NORMAL; EventPriority priority() default EventPriority.NORMAL;
} }

View file

@ -52,6 +52,7 @@ public interface PluginLoader {
* @param type Type of the event executor to create * @param type Type of the event executor to create
* @param listener the object that will handle the eventual call back * @param listener the object that will handle the eventual call back
* @return The new executor * @return The new executor
* @deprecated see PluginLoader#createRegisteredListeners
*/ */
@Deprecated @Deprecated
public EventExecutor createExecutor(Event.Type type, Listener listener); public EventExecutor createExecutor(Event.Type type, Listener listener);

View file

@ -103,6 +103,7 @@ public interface PluginManager {
* @param listener Listener to register * @param listener Listener to register
* @param priority Priority of this event * @param priority Priority of this event
* @param plugin Plugin to register * @param plugin Plugin to register
* @deprecated see PluginManager#registerEvents
*/ */
@Deprecated @Deprecated
public void registerEvent(Event.Type type, Listener listener, Priority priority, Plugin plugin); 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 executor EventExecutor to register
* @param priority Priority of this event * @param priority Priority of this event
* @param plugin Plugin to register * @param plugin Plugin to register
* @deprecated see PluginManager#registerEvent(Class, Listener, EventPriority, EventExecutor, Plugin)
*/ */
@Deprecated @Deprecated
public void registerEvent(Event.Type type, Listener listener, EventExecutor executor, Priority priority, Plugin plugin); public void registerEvent(Event.Type type, Listener listener, EventExecutor executor, Priority priority, Plugin plugin);

View file

@ -987,20 +987,21 @@ public class JavaPluginLoader implements PluginLoader {
final EventHandler eh = method.getAnnotation(EventHandler.class); final EventHandler eh = method.getAnnotation(EventHandler.class);
if (eh == null) continue; if (eh == null) continue;
final Class<?> checkClass = method.getParameterTypes()[0]; 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"); plugin.getServer().getLogger().severe("Wrong method arguments used for event type registered");
continue; continue;
} }
final Class<? extends Event> eventClass = checkClass.asSubclass(Event.class);
method.setAccessible(true); method.setAccessible(true);
Set<RegisteredListener> eventSet = ret.get(eh.event()); Set<RegisteredListener> eventSet = ret.get(eventClass);
if (eventSet == null) { if (eventSet == null) {
eventSet = new HashSet<RegisteredListener>(); eventSet = new HashSet<RegisteredListener>();
ret.put(eh.event(), eventSet); ret.put(eventClass, eventSet);
} }
eventSet.add(new RegisteredListener(listener, new EventExecutor() { eventSet.add(new RegisteredListener(listener, new EventExecutor() {
public void execute(Listener listener, Event event) throws EventException { public void execute(Listener listener, Event event) throws EventException {
try { try {
if (!checkClass.isAssignableFrom(event.getClass())) { if (!eventClass.isAssignableFrom(event.getClass())) {
throw new EventException("Wrong event type passed to registered method"); throw new EventException("Wrong event type passed to registered method");
} }
method.invoke(listener, event); method.invoke(listener, event);