Search in sources :

Example 1 with EventExecutor

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();
    }
}
Also used : EventExecutor(io.xol.chunkstories.api.events.EventExecutor) EventHandler(io.xol.chunkstories.api.events.EventHandler) Event(io.xol.chunkstories.api.events.Event) EventListeners(io.xol.chunkstories.api.events.EventListeners) Method(java.lang.reflect.Method) RegisteredListener(io.xol.chunkstories.api.events.RegisteredListener) PluginLoadException(io.xol.chunkstories.api.exceptions.plugins.PluginLoadException) IOException(java.io.IOException) PluginCreationException(io.xol.chunkstories.api.exceptions.plugins.PluginCreationException) InvocationTargetException(java.lang.reflect.InvocationTargetException) HashSet(java.util.HashSet)

Aggregations

Event (io.xol.chunkstories.api.events.Event)1 EventExecutor (io.xol.chunkstories.api.events.EventExecutor)1 EventHandler (io.xol.chunkstories.api.events.EventHandler)1 EventListeners (io.xol.chunkstories.api.events.EventListeners)1 RegisteredListener (io.xol.chunkstories.api.events.RegisteredListener)1 PluginCreationException (io.xol.chunkstories.api.exceptions.plugins.PluginCreationException)1 PluginLoadException (io.xol.chunkstories.api.exceptions.plugins.PluginLoadException)1 IOException (java.io.IOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 HashSet (java.util.HashSet)1