Finish implementing EventBus register function
This commit is contained in:
parent
e8646ee518
commit
d1c79aac63
3 changed files with 70 additions and 32 deletions
|
@ -2,20 +2,17 @@ package dev.zontreck.eventsbus;
|
|||
|
||||
import org.checkerframework.common.reflection.qual.GetClass;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Bus {
|
||||
/**
|
||||
* This bus disallows registering of instances, and will only invoke static events.
|
||||
* The main event bus!
|
||||
*/
|
||||
private static Bus Static = new Bus("Main Event Bus", false);
|
||||
/**
|
||||
* This bus requires registering of instances. Events fired here do not call static events.
|
||||
*/
|
||||
private static Bus Directed = new Bus("Direct Event Bus", true);
|
||||
private static Bus Main = new Bus("Main Event Bus", false);
|
||||
|
||||
public final String BusName;
|
||||
public final boolean UsesInstances;
|
||||
|
||||
|
@ -35,23 +32,60 @@ public class Bus {
|
|||
return false;
|
||||
}
|
||||
|
||||
public Map<Class<?>, EventContainer> static_events = new HashMap<Class<?>, EventContainer>();
|
||||
public Map<Class<?>, EventContainer> instanced_events = new HashMap<Class<?>, EventContainer>();
|
||||
public Map<Class<?>, List<EventContainer>> static_events = new HashMap<Class<?>, List<EventContainer>>();
|
||||
public Map<Class<?>, List<EventContainer>> instanced_events = new HashMap<Class<?>, List<EventContainer>>();
|
||||
|
||||
public static <T> void Register(Class<T> clazz, T instance)
|
||||
{
|
||||
EventContainer container = new EventContainer();
|
||||
if(instance == null)
|
||||
{
|
||||
// Will not register the instanced handlers
|
||||
}
|
||||
else {
|
||||
// Will register instanced handlers.
|
||||
public static <T> void Register(Class<T> clazz, T instance) {
|
||||
|
||||
List<Method> nonStaticMethods = Arrays.stream(clazz.getMethods())
|
||||
.filter(x -> x.isAnnotationPresent(Subscribe.class))
|
||||
.filter(x -> x.getModifiers() != Modifier.STATIC)
|
||||
.toList();
|
||||
|
||||
List<Method> staticMethods = Arrays.stream(clazz.getMethods())
|
||||
.filter(x -> x.isAnnotationPresent(Subscribe.class))
|
||||
.filter(x -> x.getModifiers() == Modifier.STATIC)
|
||||
.toList();
|
||||
|
||||
// Register the non-static methods if applicable
|
||||
if (instance != null) {
|
||||
for (Method m :
|
||||
nonStaticMethods) {
|
||||
EventContainer container = new EventContainer();
|
||||
container.instance = instance;
|
||||
container.method = m;
|
||||
container.clazz = clazz;
|
||||
if (m.isAnnotationPresent(Priority.class))
|
||||
container.Level = m.getAnnotation(Priority.class).Level();
|
||||
else container.Level = PriorityLevel.LOWEST;
|
||||
|
||||
container.IsSingleshot = m.isAnnotationPresent(SingleshotEvent.class);
|
||||
|
||||
if (Main.instanced_events.containsKey(clazz))
|
||||
Main.instanced_events.get(clazz).add(container);
|
||||
else {
|
||||
Main.instanced_events.put(clazz, new ArrayList<>());
|
||||
Main.instanced_events.get(clazz).add(container);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Register static handlers.
|
||||
Arrays.stream(clazz.getMethods())
|
||||
.filter(x->x.isAnnotationPresent(Subscribe.class))
|
||||
for (Method m : staticMethods) {
|
||||
EventContainer container = new EventContainer();
|
||||
container.instance = null;
|
||||
container.clazz = clazz;
|
||||
if (m.isAnnotationPresent((Priority.class)))
|
||||
container.Level = m.getAnnotation(Priority.class).Level();
|
||||
else container.Level = PriorityLevel.LOWEST;
|
||||
|
||||
container.IsSingleshot = m.isAnnotationPresent(SingleshotEvent.class);
|
||||
|
||||
if (Main.static_events.containsKey(clazz))
|
||||
Main.static_events.get(clazz).add(container);
|
||||
else {
|
||||
Main.static_events.put(clazz, new ArrayList<>());
|
||||
Main.static_events.get(clazz).add(container);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,13 +12,6 @@ public class EventContainer {
|
|||
*/
|
||||
public Method method;
|
||||
|
||||
/**
|
||||
* The event that gets called!
|
||||
*
|
||||
* @see Event
|
||||
*/
|
||||
public Event event;
|
||||
|
||||
/**
|
||||
* Indicates whether an event gets removed from the register after being invoked once.
|
||||
*/
|
||||
|
@ -40,7 +33,7 @@ public class EventContainer {
|
|||
*/
|
||||
public boolean invoke(Event EventArg, PriorityLevel level) throws InvocationTargetException, IllegalAccessException {
|
||||
if (Level != level) return false;
|
||||
|
||||
|
||||
if (instance == null) {
|
||||
method.invoke(null, EventArg);
|
||||
} else {
|
||||
|
|
11
src/main/java/dev/zontreck/eventsbus/SingleshotEvent.java
Normal file
11
src/main/java/dev/zontreck/eventsbus/SingleshotEvent.java
Normal file
|
@ -0,0 +1,11 @@
|
|||
package dev.zontreck.eventsbus;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Retention(value = RetentionPolicy.RUNTIME)
|
||||
@Target(value = ElementType.METHOD)
|
||||
public @interface SingleshotEvent {
|
||||
}
|
Loading…
Reference in a new issue