mirror of
https://github.com/PaperMC/Paper.git
synced 2025-01-08 09:17:36 +01:00
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:
parent
0c50b9eeb1
commit
84372f7cd5
@ -1,7 +1,5 @@
|
||||
package org.bukkit.event;
|
||||
|
||||
import org.bukkit.event.Listener;
|
||||
|
||||
/**
|
||||
* Handles all custom events
|
||||
*/
|
||||
|
@ -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<? 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;
|
||||
}
|
||||
|
@ -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);
|
||||
|
@ -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);
|
||||
|
@ -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<? extends Event> eventClass = checkClass.asSubclass(Event.class);
|
||||
method.setAccessible(true);
|
||||
Set<RegisteredListener> eventSet = ret.get(eh.event());
|
||||
Set<RegisteredListener> eventSet = ret.get(eventClass);
|
||||
if (eventSet == null) {
|
||||
eventSet = new HashSet<RegisteredListener>();
|
||||
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);
|
||||
|
Loading…
Reference in New Issue
Block a user