Cache Event Methods, Polish Last PR

This commit is contained in:
LOOHP
2021-08-19 20:50:45 +08:00
parent a0f592d8fe
commit 7d8208272f
18 changed files with 270 additions and 118 deletions
@@ -10,5 +10,7 @@ import java.lang.annotation.Target;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface EventHandler {
EventPriority priority() default EventPriority.NORMAL;
}
@@ -1,6 +1,7 @@
package com.loohp.limbo.events;
public enum EventPriority {
LOWEST(0),
LOW(1),
NORMAL(2),
@@ -34,4 +35,5 @@ public enum EventPriority {
}
return array;
}
}
@@ -3,33 +3,31 @@ package com.loohp.limbo.events;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import com.loohp.limbo.plugins.LimboPlugin;
public class EventsManager {
private List<ListenerPair> listeners;
private Map<Listener, RegisteredCachedListener> cachedListeners;
public EventsManager() {
listeners = new ArrayList<>();
cachedListeners = new ConcurrentHashMap<>();
}
public <T extends Event> T callEvent(T event) {
for (EventPriority priority : EventPriority.getPrioritiesInOrder()) {
for (ListenerPair entry : listeners) {
Listener listener = entry.listener;
for (Method method : listener.getClass().getMethods()) {
if (method.isAnnotationPresent(EventHandler.class)) {
if (method.getAnnotation(EventHandler.class).priority().equals(priority)) {
if (method.getParameterCount() == 1 && method.getParameterTypes()[0].equals(event.getClass())) {
try {
method.invoke(listener, event);
} catch (Exception e) {
System.err.println("Error while passing " + event.getClass().getCanonicalName() + " to the plugin \"" + entry.plugin.getName() + "\"");
e.printStackTrace();
}
}
}
for (Entry<Listener, RegisteredCachedListener> entry : cachedListeners.entrySet()) {
for (Method method : entry.getValue().getListeners(event.getClass(), priority)) {
try {
method.invoke(entry.getKey(), event);
} catch (Exception e) {
System.err.println("Error while passing " + event.getClass().getCanonicalName() + " to the plugin \"" + entry.getValue().getPlugin().getName() + "\"");
e.printStackTrace();
}
}
}
@@ -39,10 +37,18 @@ public class EventsManager {
public void registerEvents(LimboPlugin plugin, Listener listener) {
listeners.add(new ListenerPair(plugin, listener));
cachedListeners.put(listener, new RegisteredCachedListener(plugin, listener));
}
public void unregisterAllListeners(LimboPlugin plugin) {
listeners.removeIf(each -> each.plugin.equals(plugin));
listeners.removeIf(each -> {
if (each.plugin.equals(plugin)) {
cachedListeners.remove(each.listener);
return true;
} else {
return false;
}
});
}
protected static class ListenerPair {
@@ -0,0 +1,47 @@
package com.loohp.limbo.events;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import com.loohp.limbo.plugins.LimboPlugin;
public class RegisteredCachedListener {
private LimboPlugin plugin;
private Map<Class<? extends Event>, Map<EventPriority, List<Method>>> listeners;
@SuppressWarnings("unchecked")
public RegisteredCachedListener(LimboPlugin plugin, Listener listener) {
this.plugin = plugin;
this.listeners = new ConcurrentHashMap<>();
for (Method method : listener.getClass().getMethods()) {
if (method.isAnnotationPresent(EventHandler.class) && method.getParameterCount() == 1 && Event.class.isAssignableFrom(method.getParameterTypes()[0])) {
Class<? extends Event> eventClass = (Class<? extends Event>) method.getParameterTypes()[0];
listeners.putIfAbsent(eventClass, new ConcurrentHashMap<>());
Map<EventPriority, List<Method>> mapping = listeners.get(eventClass);
EventPriority priority = method.getAnnotation(EventHandler.class).priority();
mapping.putIfAbsent(priority, new ArrayList<>());
List<Method> list = mapping.get(priority);
list.add(method);
}
}
}
public LimboPlugin getPlugin() {
return plugin;
}
public List<Method> getListeners(Class<? extends Event> eventClass, EventPriority priority) {
Map<EventPriority, List<Method>> mapping = listeners.get(eventClass);
if (mapping == null) {
return Collections.emptyList();
}
List<Method> list = mapping.get(priority);
return list == null ? Collections.emptyList() : Collections.unmodifiableList(list);
}
}