Bug fixes
This commit is contained in:
@@ -18,9 +18,20 @@ public final class EventManager extends DefaultMethodsOverrider {
|
||||
public void registerListener(Class<? extends EventListener> listenerClass) throws Exception {
|
||||
if (isListenerRegistered(listenerClass)) return;
|
||||
|
||||
Object instance = listenerClass.getDeclaredConstructor().newInstance();
|
||||
EventListener instance = listenerClass.getDeclaredConstructor().newInstance();
|
||||
registerListenerInstance(instance);
|
||||
}
|
||||
|
||||
for (Method method : listenerClass.getDeclaredMethods()) {
|
||||
public void registerListener(EventListener listenerInstance) {
|
||||
if (listenerInstance == null) throw new IllegalArgumentException("Listener instance cannot be null");
|
||||
Class<? extends EventListener> listenerClass = listenerInstance.getClass();
|
||||
if (isListenerRegistered(listenerClass)) return;
|
||||
|
||||
registerListenerInstance(listenerInstance);
|
||||
}
|
||||
|
||||
private void registerListenerInstance(Object instance) {
|
||||
for (Method method : instance.getClass().getDeclaredMethods()) {
|
||||
Listener listenerAnnotation = method.getAnnotation(Listener.class);
|
||||
|
||||
if (listenerAnnotation == null || method.getParameterCount() != 1) continue;
|
||||
@@ -37,7 +48,7 @@ public final class EventManager extends DefaultMethodsOverrider {
|
||||
.put(instance, method);
|
||||
}
|
||||
|
||||
eventListeners.put(listenerClass, instance);
|
||||
eventListeners.put((Class<? extends EventListener>) instance.getClass(), instance);
|
||||
}
|
||||
|
||||
public synchronized void unregisterListener(Class<? extends EventListener> listenerClass) {
|
||||
@@ -51,7 +62,6 @@ public final class EventManager extends DefaultMethodsOverrider {
|
||||
}
|
||||
}
|
||||
|
||||
// Clean up empty entries
|
||||
registeredListener.entrySet().removeIf(e ->
|
||||
e.getValue().values().stream().allMatch(Map::isEmpty));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user