More feature parity with C# EventBus
This commit is contained in:
parent
783de1e9c0
commit
e8646ee518
5 changed files with 124 additions and 10 deletions
|
@ -1,10 +1,57 @@
|
|||
package dev.zontreck.eventsbus;
|
||||
|
||||
public class Bus {
|
||||
public static Bus Main = new Bus("Main Event Bus");
|
||||
public final String BusName;
|
||||
import org.checkerframework.common.reflection.qual.GetClass;
|
||||
|
||||
public Bus(String name) {
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class Bus {
|
||||
/**
|
||||
* This bus disallows registering of instances, and will only invoke static events.
|
||||
*/
|
||||
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);
|
||||
public final String BusName;
|
||||
public final boolean UsesInstances;
|
||||
|
||||
public Bus(String name, boolean useInstances) {
|
||||
BusName = name;
|
||||
UsesInstances = useInstances;
|
||||
}
|
||||
|
||||
/**
|
||||
* Posts an event to the event message bus.
|
||||
*
|
||||
* @param event The event to be posted to all classes
|
||||
* @return True if the event was cancelled.
|
||||
*/
|
||||
public boolean Post(Event event) {
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public Map<Class<?>, EventContainer> static_events = new HashMap<Class<?>, EventContainer>();
|
||||
public Map<Class<?>, EventContainer> instanced_events = new HashMap<Class<?>, 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.
|
||||
}
|
||||
|
||||
// Register static handlers.
|
||||
Arrays.stream(clazz.getMethods())
|
||||
.filter(x->x.isAnnotationPresent(Subscribe.class))
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,14 +1,14 @@
|
|||
package dev.zontreck.eventsbus;
|
||||
|
||||
@Priority(Level = PriorityLevel.LOWEST)
|
||||
|
||||
public class Event {
|
||||
private boolean cancelled = false;
|
||||
|
||||
/**
|
||||
* Checks if the event can be cancelled.
|
||||
*
|
||||
* @see Cancellable
|
||||
* @return True if the cancellation annotation is present.
|
||||
* @see Cancellable
|
||||
*/
|
||||
public boolean IsCancellable() {
|
||||
Class<?> Current = this.getClass();
|
||||
|
|
52
src/main/java/dev/zontreck/eventsbus/EventContainer.java
Normal file
52
src/main/java/dev/zontreck/eventsbus/EventContainer.java
Normal file
|
@ -0,0 +1,52 @@
|
|||
package dev.zontreck.eventsbus;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
public class EventContainer {
|
||||
public Class<?> clazz;
|
||||
public Object instance;
|
||||
|
||||
/**
|
||||
* The method that gets invoked, either statically, or via the instance.
|
||||
*/
|
||||
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.
|
||||
*/
|
||||
public boolean IsSingleshot;
|
||||
|
||||
/**
|
||||
* The current method's priority level
|
||||
*/
|
||||
public PriorityLevel Level;
|
||||
|
||||
/**
|
||||
* Invokes the event
|
||||
*
|
||||
* @param EventArg The event instance to pass to the subscribed function
|
||||
* @param level Current priority level on the call loop. Will refuse to invoke if the priority level mismatches.
|
||||
* @return True if the event was single shot and should be deregistered
|
||||
* @throws InvocationTargetException
|
||||
* @throws IllegalAccessException
|
||||
*/
|
||||
public boolean invoke(Event EventArg, PriorityLevel level) throws InvocationTargetException, IllegalAccessException {
|
||||
if (Level != level) return false;
|
||||
|
||||
if (instance == null) {
|
||||
method.invoke(null, EventArg);
|
||||
} else {
|
||||
method.invoke(instance, EventArg);
|
||||
}
|
||||
|
||||
return IsSingleshot;
|
||||
}
|
||||
}
|
|
@ -1,9 +1,12 @@
|
|||
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 Priority {
|
||||
PriorityLevel Level();
|
||||
}
|
||||
|
|
12
src/main/java/dev/zontreck/eventsbus/Subscribe.java
Normal file
12
src/main/java/dev/zontreck/eventsbus/Subscribe.java
Normal file
|
@ -0,0 +1,12 @@
|
|||
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 Subscribe {
|
||||
}
|
Loading…
Reference in a new issue