mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-08 17:37:42 +01:00
Move type check inside EventNode
This commit is contained in:
parent
bc2a075b60
commit
5d1b742934
@ -10,19 +10,20 @@ import java.util.concurrent.CopyOnWriteArrayList;
|
|||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
import java.util.function.Predicate;
|
import java.util.function.Predicate;
|
||||||
|
|
||||||
public abstract class EventNode<T extends Event> {
|
public class EventNode<T extends Event> {
|
||||||
|
|
||||||
private final String name = "debug";
|
private final String name = "debug";
|
||||||
|
|
||||||
private final Map<Class<? extends T>, List<EventListener<T>>> listenerMap = new ConcurrentHashMap<>();
|
private final Map<Class<? extends T>, List<EventListener<T>>> listenerMap = new ConcurrentHashMap<>();
|
||||||
private final List<EventNode<T>> children = new CopyOnWriteArrayList<>();
|
private final List<EventNode<T>> children = new CopyOnWriteArrayList<>();
|
||||||
|
|
||||||
private static final EventNode<Event> EMPTY = new EventNode<>() {
|
protected final Class<T> type;
|
||||||
@Override
|
|
||||||
protected boolean isValid(@NotNull Event event) {
|
protected EventNode(Class<T> type) {
|
||||||
return true;
|
this.type = type;
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
private static final EventNode<Event> EMPTY = new EventNode<>(Event.class);
|
||||||
|
|
||||||
public static EventNode<Event> create() {
|
public static EventNode<Event> create() {
|
||||||
return EMPTY;
|
return EMPTY;
|
||||||
@ -37,11 +38,19 @@ public abstract class EventNode<T extends Event> {
|
|||||||
return conditional(eventType, t -> true);
|
return conditional(eventType, t -> true);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected abstract boolean isValid(@NotNull T event);
|
protected boolean condition(@NotNull T event) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
public void call(@NotNull T event) {
|
public void call(@NotNull T event) {
|
||||||
if (!isValid(event))
|
if (!type.isAssignableFrom(event.getClass())) {
|
||||||
|
// Invalid event type
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
|
if (!condition(event)) {
|
||||||
|
// Cancelled by superclass
|
||||||
|
return;
|
||||||
|
}
|
||||||
final var listeners = listenerMap.get(event.getClass());
|
final var listeners = listenerMap.get(event.getClass());
|
||||||
if (listeners != null && !listeners.isEmpty()) {
|
if (listeners != null && !listeners.isEmpty()) {
|
||||||
listeners.forEach(eventListener ->
|
listeners.forEach(eventListener ->
|
||||||
|
@ -6,18 +6,16 @@ import java.util.function.Predicate;
|
|||||||
|
|
||||||
public class EventNodeConditional<T extends Event> extends EventNode<T> {
|
public class EventNodeConditional<T extends Event> extends EventNode<T> {
|
||||||
|
|
||||||
private final Class<T> type;
|
|
||||||
private volatile Predicate<T> predicate;
|
private volatile Predicate<T> predicate;
|
||||||
|
|
||||||
protected EventNodeConditional(Class<T> type, Predicate<T> predicate) {
|
protected EventNodeConditional(Class<T> type, Predicate<T> predicate) {
|
||||||
this.type = type;
|
super(type);
|
||||||
this.predicate = predicate;
|
this.predicate = predicate;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected boolean isValid(@NotNull T event) {
|
protected boolean condition(@NotNull T event) {
|
||||||
final boolean typeCheck = type.isAssignableFrom(event.getClass());
|
return predicate.test(event);
|
||||||
return typeCheck && predicate.test(event);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public @NotNull Predicate<@NotNull T> getPredicate() {
|
public @NotNull Predicate<@NotNull T> getPredicate() {
|
||||||
|
Loading…
Reference in New Issue
Block a user