Extracted base EventLoop class
This commit is contained in:
41
event-loop/src/main/java/mc/core/events/BaseEventLoop.java
Normal file
41
event-loop/src/main/java/mc/core/events/BaseEventLoop.java
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
package mc.core.events;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import java.lang.reflect.Modifier;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
public abstract class BaseEventLoop implements EventLoop {
|
||||||
|
@Override
|
||||||
|
public void addEventHandler(Object object) {
|
||||||
|
for (Method method : object.getClass().getDeclaredMethods()) {
|
||||||
|
EventHandler annotation = method.getAnnotation(EventHandler.class);
|
||||||
|
if (annotation == null)
|
||||||
|
continue; // We are not interested in methods without @EventHandler annotation
|
||||||
|
|
||||||
|
if (!Modifier.isPublic(method.getModifiers())) {
|
||||||
|
log.error("Unable to register {} as an EventHandler. Method must have a 'private' access modifier.", method.toString());
|
||||||
|
continue;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
if (method.getParameterCount() != 1) {
|
||||||
|
log.error("Unable to register {} as an EventHandler. Method must have exactly one argument.", method.toString());
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
Class<?> firstParamType = method.getParameterTypes()[0];
|
||||||
|
if (!Event.class.isAssignableFrom(firstParamType)) {
|
||||||
|
log.error("Unable to register {} as an EventHandler. First parameter type must implement 'Event' interface.", method.toString());
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked") Class<? extends Event> eventType = (Class<? extends Event>) firstParamType;
|
||||||
|
|
||||||
|
registerMethod(object, method, annotation, eventType);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected abstract void registerMethod(Object object, Method method, EventHandler annotation, Class<? extends Event> eventType);
|
||||||
|
}
|
||||||
@@ -10,7 +10,7 @@ import java.lang.reflect.Modifier;
|
|||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class SimpleEventLoop implements EventLoop {
|
public class SimpleEventLoop extends BaseEventLoop {
|
||||||
private Map<Class<? extends Event>, List<ExecutorLink>> handlers = new HashMap<>();
|
private Map<Class<? extends Event>, List<ExecutorLink>> handlers = new HashMap<>();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -27,39 +27,12 @@ public class SimpleEventLoop implements EventLoop {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
protected void registerMethod(Object object, Method method, EventHandler annotation, Class<? extends Event> eventType) {
|
||||||
public void addEventHandler(Object object) {
|
List<SimpleEventLoop.ExecutorLink> eventHandlers = handlers.computeIfAbsent(eventType, s -> new ArrayList<>());
|
||||||
for (Method method : object.getClass().getDeclaredMethods()) {
|
eventHandlers.add(new SimpleEventLoop.ExecutorLink(annotation.priority().getValue(), annotation.ignoreCancelled(), method, object));
|
||||||
EventHandler annotation = method.getAnnotation(EventHandler.class);
|
eventHandlers.sort(Comparator.comparingInt(o -> o.priority));
|
||||||
if (annotation == null)
|
|
||||||
continue; // We are not interested in methods without @EventHandler annotation
|
|
||||||
|
|
||||||
if (!Modifier.isPublic(method.getModifiers())) {
|
|
||||||
log.error("Unable to register {} as an EventHandler. Method must have a 'private' access modifier.", method.toString());
|
|
||||||
continue;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
if (method.getParameterCount() != 1) {
|
|
||||||
log.error("Unable to register {} as an EventHandler. Method must have exactly one argument.", method.toString());
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
Class<?> firstParamType = method.getParameterTypes()[0];
|
|
||||||
if (!Event.class.isAssignableFrom(firstParamType)) {
|
|
||||||
log.error("Unable to register {} as an EventHandler. First parameter type must implement 'Event' interface.", method.toString());
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
@SuppressWarnings("unchecked") Class<? extends Event> eventType = (Class<? extends Event>) firstParamType;
|
|
||||||
|
|
||||||
List<ExecutorLink> eventHandlers = handlers.computeIfAbsent(eventType, s -> new ArrayList<>());
|
|
||||||
eventHandlers.add(new ExecutorLink(annotation.priority().getValue(), annotation.ignoreCancelled(), method, object));
|
|
||||||
eventHandlers.sort(Comparator.comparingInt(o -> o.priority));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class describes
|
* This class describes
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user