Template
mirror of
https://github.com/LOOHP/Limbo.git
synced 2026-09-06 11:48:54 +00:00
reupload
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
package com.loohp.limbo.events;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.loohp.limbo.plugins.LimboPlugin;
|
||||
|
||||
public class EventsManager {
|
||||
|
||||
private List<ListenerPair> listeners;
|
||||
|
||||
public EventsManager() {
|
||||
listeners = new ArrayList<>();
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return event;
|
||||
}
|
||||
|
||||
public void registerEvents(LimboPlugin plugin, Listener listener) {
|
||||
listeners.add(new ListenerPair(plugin, listener));
|
||||
}
|
||||
|
||||
public void unregisterAllListeners(LimboPlugin plugin) {
|
||||
listeners.removeIf(each -> each.plugin.equals(plugin));
|
||||
}
|
||||
|
||||
protected static class ListenerPair {
|
||||
public LimboPlugin plugin;
|
||||
public Listener listener;
|
||||
|
||||
public ListenerPair(LimboPlugin plugin, Listener listener) {
|
||||
this.plugin = plugin;
|
||||
this.listener = listener;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user