mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-06 16:37:38 +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.Predicate;
|
||||
|
||||
public abstract class EventNode<T extends Event> {
|
||||
public class EventNode<T extends Event> {
|
||||
|
||||
private final String name = "debug";
|
||||
|
||||
private final Map<Class<? extends T>, List<EventListener<T>>> listenerMap = new ConcurrentHashMap<>();
|
||||
private final List<EventNode<T>> children = new CopyOnWriteArrayList<>();
|
||||
|
||||
private static final EventNode<Event> EMPTY = new EventNode<>() {
|
||||
@Override
|
||||
protected boolean isValid(@NotNull Event event) {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
protected final Class<T> type;
|
||||
|
||||
protected EventNode(Class<T> type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
private static final EventNode<Event> EMPTY = new EventNode<>(Event.class);
|
||||
|
||||
public static EventNode<Event> create() {
|
||||
return EMPTY;
|
||||
@ -37,11 +38,19 @@ public abstract class EventNode<T extends Event> {
|
||||
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) {
|
||||
if (!isValid(event))
|
||||
if (!type.isAssignableFrom(event.getClass())) {
|
||||
// Invalid event type
|
||||
return;
|
||||
}
|
||||
if (!condition(event)) {
|
||||
// Cancelled by superclass
|
||||
return;
|
||||
}
|
||||
final var listeners = listenerMap.get(event.getClass());
|
||||
if (listeners != null && !listeners.isEmpty()) {
|
||||
listeners.forEach(eventListener ->
|
||||
|
@ -6,18 +6,16 @@ import java.util.function.Predicate;
|
||||
|
||||
public class EventNodeConditional<T extends Event> extends EventNode<T> {
|
||||
|
||||
private final Class<T> type;
|
||||
private volatile Predicate<T> predicate;
|
||||
|
||||
protected EventNodeConditional(Class<T> type, Predicate<T> predicate) {
|
||||
this.type = type;
|
||||
super(type);
|
||||
this.predicate = predicate;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean isValid(@NotNull T event) {
|
||||
final boolean typeCheck = type.isAssignableFrom(event.getClass());
|
||||
return typeCheck && predicate.test(event);
|
||||
protected boolean condition(@NotNull T event) {
|
||||
return predicate.test(event);
|
||||
}
|
||||
|
||||
public @NotNull Predicate<@NotNull T> getPredicate() {
|
||||
|
Loading…
Reference in New Issue
Block a user