Loop v3 implementation started
This commit is contained in:
@@ -13,7 +13,7 @@ public abstract class BaseEventLoop implements EventLoop {
|
||||
return true;
|
||||
|
||||
if (!Modifier.isPublic(method.getModifiers())) {
|
||||
log.error("Unable to register {} as an EventHandler. Method must have a 'private' access modifier.", method.toString());
|
||||
log.error("Unable to register {} as an EventHandler. Method must have a 'public' access modifier.", method.toString());
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
@@ -10,4 +10,8 @@ public @interface EventHandler {
|
||||
EventPriority priority() default EventPriority.NORMAL;
|
||||
|
||||
boolean ignoreCancelled() default false;
|
||||
|
||||
boolean pluginSynchronize() default true;
|
||||
|
||||
LockableResource[] lock() default {};
|
||||
}
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
package mc.core.events;
|
||||
|
||||
public enum LockableResource {
|
||||
PLAYER,
|
||||
WORLD;
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
package mc.core.events.cachelike;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class EventHandlerBase {
|
||||
private List<PreprocessorContext> contextList;
|
||||
|
||||
protected void push(PreprocessorContext context){
|
||||
contextList.add(context);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
package mc.core.events.cachelike;
|
||||
|
||||
public @interface Preprocessor {
|
||||
int index();
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
package mc.core.events.cachelike;
|
||||
|
||||
import com.google.common.base.Function;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public abstract class PreprocessorContext<E> {
|
||||
private List<Function<E, Number>> fetchers = new ArrayList<>();
|
||||
|
||||
protected void push(Function<E, Number> fetcher) {
|
||||
fetchers.add(fetcher);
|
||||
}
|
||||
|
||||
protected abstract void init() ;
|
||||
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
package mc.core.events.cachelike;
|
||||
|
||||
import mc.core.events.LoginEvent;
|
||||
|
||||
public class SampleHandler extends EventHandlerBase {
|
||||
|
||||
public SampleHandler() {
|
||||
push(new PreprocessorContext() {
|
||||
@Override
|
||||
protected void init() {
|
||||
System.out.println("I am context #0!");
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
@Preprocessor(index = 0) // Map constructor #0 to this event handler
|
||||
public void onLogin(LoginEvent event){
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package mc.core.events.v3;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import mc.core.events.Event;
|
||||
import mc.core.events.EventHandler;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.*;
|
||||
|
||||
@Slf4j
|
||||
public class FullAsyncEventLoop {
|
||||
Map<Class<? extends Event>, List<RegisteredEventHandler>> handlers = new HashMap<>();
|
||||
|
||||
public void addEventHandler(Plugin plugin, Object object) {
|
||||
Map<Method, EventHandler> candidates = getEventHandlerCandidates(object);
|
||||
|
||||
for (Map.Entry<Method, EventHandler> pair : candidates.entrySet()) {
|
||||
@SuppressWarnings("unchecked") Class<? extends Event> eventType = (Class<? extends Event>) pair.getKey().getParameterTypes()[0];
|
||||
List<RegisteredEventHandler> handlers = this.handlers.computeIfAbsent(eventType, e -> new ArrayList<>());
|
||||
handlers.add(new RegisteredEventHandler(plugin, object, pair.getKey(), pair.getValue().lock(), pair.getValue().pluginSynchronize(), pair.getValue().priority().getValue(), pair.getValue().ignoreCancelled()));
|
||||
handlers.sort(Comparator.comparingInt(RegisteredEventHandler::getPriority));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
private Map<Method, EventHandler> getEventHandlerCandidates(Object object) {
|
||||
Map<Method, EventHandler> candidates;
|
||||
candidates = new HashMap<>();
|
||||
for (Method method : object.getClass().getDeclaredMethods()) {
|
||||
EventHandler annotation = method.getAnnotation(EventHandler.class);
|
||||
if (annotation == null)
|
||||
continue;
|
||||
|
||||
if (!Modifier.isPublic(method.getModifiers())) {
|
||||
log.error("Unable to register {} as an EventHandler. Method must have a 'public' 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;
|
||||
}
|
||||
|
||||
candidates.put(method, annotation);
|
||||
}
|
||||
return candidates;
|
||||
}
|
||||
|
||||
}
|
||||
4
event-loop/src/main/java/mc/core/events/v3/Plugin.java
Normal file
4
event-loop/src/main/java/mc/core/events/v3/Plugin.java
Normal file
@@ -0,0 +1,4 @@
|
||||
package mc.core.events.v3;
|
||||
|
||||
public interface Plugin {
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package mc.core.events.v3;
|
||||
|
||||
public class QueueManager {
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package mc.core.events.v3;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import mc.core.events.LockableResource;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
@Getter
|
||||
public class RegisteredEventHandler {
|
||||
private final Plugin plugin;
|
||||
private final Object object;
|
||||
private final Method method;
|
||||
private final LockableResource[] lock;
|
||||
private final boolean pluginSynchronize;
|
||||
private final int priority;
|
||||
private final boolean ignoreCancelled;
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user