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) {
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<? extends Event> eventClass = parameters[0].asSubclass(Event.class);
method.setAccessible(true);
List<RegisteredListener> executors = registeredListeners.get(eventClass);
if (executors == null) {
executors = new ArrayList<>();
registeredListeners.put(eventClass, executors);
}
Class<? extends Event> eventClass = parameters[0].asSubclass(Event.class);
List<RegisteredListener> executors = getRegisteredListeners(eventClass);
executors.add(new RegisteredListener(listener, method, eventClass, eh));
}
}
protected void callEvent(Event event) {
List<RegisteredListener> listeners = registeredListeners.get(event.getClass());
if (listeners == null) {
List<RegisteredListener> 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<RegisteredListener> getRegisteredListeners(Class<? extends Event> eventClass) {
List<RegisteredListener> listeners = registeredListeners.get(eventClass);
if (listeners == null) {
listeners = new ArrayList<>();
registeredListeners.put(eventClass, listeners);
}
return listeners;
}
}