use of io.xol.chunkstories.api.events.EventExecutor in project chunkstories by Hugobros3.
the class DefaultPluginManager method registerEventListener.
@Override
public void registerEventListener(Listener listener, ChunkStoriesPlugin plugin) {
System.out.println("Registering " + listener);
try {
// Get a list of all the classes methods
Set<Method> methods = new HashSet<Method>();
for (Method method : listener.getClass().getMethods()) methods.add(method);
for (Method method : listener.getClass().getDeclaredMethods()) methods.add(method);
// Filter it so only interested in @EventHandler annoted methods
for (final Method method : methods) {
EventHandler eventHandlerAnnotation = method.getAnnotation(EventHandler.class);
// We look for the annotation in the method
if (eventHandlerAnnotation == null)
continue;
// TODO something about priority
if (method.getParameterTypes().length != 1 || !Event.class.isAssignableFrom(method.getParameterTypes()[0])) {
logger().warn("Plugin " + plugin + " attempted to register an invalid EventHandler");
continue;
}
Class<? extends Event> parameter = method.getParameterTypes()[0].asSubclass(Event.class);
// Create an EventExecutor to launch the event code
EventExecutor executor = new EventExecutor() {
@Override
public void fireEvent(Event event) throws Exception {
method.invoke(listener, event);
}
};
RegisteredListener registeredListener = new RegisteredListener(listener, plugin, executor, eventHandlerAnnotation.priority());
// Get the listeners list for this event
Method getListeners = parameter.getMethod("getListenersStatic");
getListeners.setAccessible(true);
EventListeners thisEventKindOfListeners = (EventListeners) getListeners.invoke(null);
// Add our own to it
thisEventKindOfListeners.registerListener(registeredListener);
registeredEventListeners.put(thisEventKindOfListeners, registeredListener);
// Depending on the event configuration we may or may not care about the children events
if (eventHandlerAnnotation.listenToChildEvents() != EventHandler.ListenToChildEvents.NO)
addRegisteredListenerToEventChildren(thisEventKindOfListeners, registeredListener, eventHandlerAnnotation.listenToChildEvents() == EventHandler.ListenToChildEvents.RECURSIVE);
logger().info("Successuflly added EventHandler for " + parameter.getName() + "in " + listener + " of plugin " + plugin);
}
} catch (Exception e) {
e.printStackTrace();
}
}
Aggregations