Loop v3 implementation started
This commit is contained in:
15
event-loop/build.gradle
Normal file
15
event-loop/build.gradle
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
group 'mc'
|
||||||
|
version '1.0-SNAPSHOT'
|
||||||
|
|
||||||
|
dependencies {
|
||||||
|
/* Core */
|
||||||
|
compile_excludeCopy project(':core')
|
||||||
|
|
||||||
|
testCompile group: 'org.slf4j', name: 'slf4j-simple', version: '1.6.1'
|
||||||
|
testCompile group: 'com.carrotsearch', name: 'junit-benchmarks', version: '0.7.0'
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
test {
|
||||||
|
exclude "ru/core/events/*Benchmark.class"
|
||||||
|
}
|
||||||
@@ -13,7 +13,7 @@ public abstract class BaseEventLoop implements EventLoop {
|
|||||||
return true;
|
return true;
|
||||||
|
|
||||||
if (!Modifier.isPublic(method.getModifiers())) {
|
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;
|
return true;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,4 +10,8 @@ public @interface EventHandler {
|
|||||||
EventPriority priority() default EventPriority.NORMAL;
|
EventPriority priority() default EventPriority.NORMAL;
|
||||||
|
|
||||||
boolean ignoreCancelled() default false;
|
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