diff --git a/core/src/com/me/common/ecs/ListenerRegistry.java b/core/src/com/me/common/ecs/ListenerRegistry.java index aaabff0..40d42ae 100644 --- a/core/src/com/me/common/ecs/ListenerRegistry.java +++ b/core/src/com/me/common/ecs/ListenerRegistry.java @@ -20,9 +20,7 @@ public class ListenerRegistry { } protected void registerListener(Listener listener) { - Method[] methods = listener.getClass().getMethods(); - - for (Method method : methods) { + for (Method method : listener.getClass().getMethods()) { EventHandler eh = method.getAnnotation(EventHandler.class); if (eh == null) { continue; @@ -33,24 +31,18 @@ public class ListenerRegistry { System.err.println("Attempting to register an EventHandler with an invalid method signature: " + method.toGenericString() + " in " + listener.getClass()); continue; } - - Class eventClass = parameters[0].asSubclass(Event.class); method.setAccessible(true); - List executors = registeredListeners.get(eventClass); - if (executors == null) { - executors = new ArrayList<>(); - registeredListeners.put(eventClass, executors); - } - + Class eventClass = parameters[0].asSubclass(Event.class); + List executors = getRegisteredListeners(eventClass); executors.add(new RegisteredListener(listener, method, eventClass, eh)); } } protected void callEvent(Event event) { - List listeners = registeredListeners.get(event.getClass()); - if (listeners == null) { + List listeners = getRegisteredListeners(event.getClass()); + if (listeners.isEmpty()) { return; } @@ -58,9 +50,17 @@ public class ListenerRegistry { if (listener.isIgnoringCancelled() && event.isCancelled()) { continue; } - listener.execute(event); } } + private List getRegisteredListeners(Class eventClass) { + List listeners = registeredListeners.get(eventClass); + if (listeners == null) { + listeners = new ArrayList<>(); + registeredListeners.put(eventClass, listeners); + } + return listeners; + } + }