Cleanup of ListenerRegistry

This commit is contained in:
Matt Low 2020-01-25 14:03:50 +04:00
parent 90e5ff36e7
commit d97c956607

View File

@ -20,9 +20,7 @@ public class ListenerRegistry {
} }
protected void registerListener(Listener listener) { protected void registerListener(Listener listener) {
Method[] methods = listener.getClass().getMethods(); for (Method method : listener.getClass().getMethods()) {
for (Method method : methods) {
EventHandler eh = method.getAnnotation(EventHandler.class); EventHandler eh = method.getAnnotation(EventHandler.class);
if (eh == null) { if (eh == null) {
continue; 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()); System.err.println("Attempting to register an EventHandler with an invalid method signature: " + method.toGenericString() + " in " + listener.getClass());
continue; continue;
} }
Class<? extends Event> eventClass = parameters[0].asSubclass(Event.class);
method.setAccessible(true); method.setAccessible(true);
List<RegisteredListener> executors = registeredListeners.get(eventClass); Class<? extends Event> eventClass = parameters[0].asSubclass(Event.class);
if (executors == null) { List<RegisteredListener> executors = getRegisteredListeners(eventClass);
executors = new ArrayList<>();
registeredListeners.put(eventClass, executors);
}
executors.add(new RegisteredListener(listener, method, eventClass, eh)); executors.add(new RegisteredListener(listener, method, eventClass, eh));
} }
} }
protected void callEvent(Event event) { protected void callEvent(Event event) {
List<RegisteredListener> listeners = registeredListeners.get(event.getClass()); List<RegisteredListener> listeners = getRegisteredListeners(event.getClass());
if (listeners == null) { if (listeners.isEmpty()) {
return; return;
} }
@ -58,9 +50,17 @@ public class ListenerRegistry {
if (listener.isIgnoringCancelled() && event.isCancelled()) { if (listener.isIgnoringCancelled() && event.isCancelled()) {
continue; continue;
} }
listener.execute(event); listener.execute(event);
} }
} }
private List<RegisteredListener> getRegisteredListeners(Class<? extends Event> eventClass) {
List<RegisteredListener> listeners = registeredListeners.get(eventClass);
if (listeners == null) {
listeners = new ArrayList<>();
registeredListeners.put(eventClass, listeners);
}
return listeners;
}
} }