mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-06 16:37:38 +01:00
Fix event calling
This commit is contained in:
parent
c3f52031e6
commit
bc2a075b60
@ -9,20 +9,18 @@ import java.util.function.Predicate;
|
||||
|
||||
public class EventListener<T extends Event> {
|
||||
|
||||
private final Consumer<T> combined;
|
||||
protected final Class<T> type;
|
||||
protected final Consumer<T> combined;
|
||||
|
||||
private EventListener(@NotNull Consumer<T> combined) {
|
||||
private EventListener(@NotNull Class<T> type, @NotNull Consumer<T> combined) {
|
||||
this.type = type;
|
||||
this.combined = combined;
|
||||
}
|
||||
|
||||
public static <T extends Event> EventListener.Builder<T> of(Class<T> eventType) {
|
||||
public static <T extends Event> EventListener.Builder<T> of(@NotNull Class<T> eventType) {
|
||||
return new EventListener.Builder<>(eventType);
|
||||
}
|
||||
|
||||
protected Consumer<T> getCombined() {
|
||||
return combined;
|
||||
}
|
||||
|
||||
public static class Builder<T extends Event> {
|
||||
|
||||
private final Class<T> eventType;
|
||||
@ -45,7 +43,7 @@ public class EventListener<T extends Event> {
|
||||
}
|
||||
|
||||
public EventListener<T> build() {
|
||||
return new EventListener<>(event -> {
|
||||
return new EventListener<>(eventType, event -> {
|
||||
// Filtering
|
||||
if (!filters.isEmpty()) {
|
||||
if (filters.stream().anyMatch(filter -> !filter.test(event))) {
|
||||
@ -53,7 +51,6 @@ public class EventListener<T extends Event> {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Handler
|
||||
if (handler != null) {
|
||||
handler.accept(event);
|
||||
|
@ -4,6 +4,8 @@ import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Predicate;
|
||||
@ -11,7 +13,8 @@ import java.util.function.Predicate;
|
||||
public abstract class EventNode<T extends Event> {
|
||||
|
||||
private final String name = "debug";
|
||||
private final List<EventListener<T>> listeners = new CopyOnWriteArrayList<>();
|
||||
|
||||
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<>() {
|
||||
@ -27,7 +30,7 @@ public abstract class EventNode<T extends Event> {
|
||||
|
||||
public static <E extends Event> EventNode<E> conditional(@NotNull Class<E> type,
|
||||
@NotNull Predicate<E> predicate) {
|
||||
return new EventNodeConditional<>(predicate);
|
||||
return new EventNodeConditional<>(type, predicate);
|
||||
}
|
||||
|
||||
public static <E extends Event> EventNode<E> conditional(@NotNull Class<E> eventType) {
|
||||
@ -39,7 +42,11 @@ public abstract class EventNode<T extends Event> {
|
||||
public void call(@NotNull T event) {
|
||||
if (!isValid(event))
|
||||
return;
|
||||
this.listeners.forEach(eventListener -> eventListener.getCombined().accept(event));
|
||||
final var listeners = listenerMap.get(event.getClass());
|
||||
if (listeners != null && !listeners.isEmpty()) {
|
||||
listeners.forEach(eventListener ->
|
||||
eventListener.combined.accept(event));
|
||||
}
|
||||
this.children.forEach(eventNode -> eventNode.call(event));
|
||||
}
|
||||
|
||||
@ -48,7 +55,8 @@ public abstract class EventNode<T extends Event> {
|
||||
}
|
||||
|
||||
public void addListener(@NotNull EventListener<? extends T> listener) {
|
||||
this.listeners.add((EventListener<T>) listener);
|
||||
this.listenerMap.computeIfAbsent(listener.type, aClass -> new CopyOnWriteArrayList<>())
|
||||
.add((EventListener<T>) listener);
|
||||
}
|
||||
|
||||
public <E extends T> void addListener(@NotNull Class<E> eventClass, @NotNull Consumer<@NotNull E> listener) {
|
||||
|
@ -6,22 +6,25 @@ 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(Predicate<T> predicate) {
|
||||
protected EventNodeConditional(Class<T> type, Predicate<T> predicate) {
|
||||
this.type = type;
|
||||
this.predicate = predicate;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean isValid(@NotNull T event) {
|
||||
return predicate.test(event);
|
||||
final boolean typeCheck = type.isAssignableFrom(event.getClass());
|
||||
return typeCheck && predicate.test(event);
|
||||
}
|
||||
|
||||
public Predicate<T> getPredicate() {
|
||||
public @NotNull Predicate<@NotNull T> getPredicate() {
|
||||
return predicate;
|
||||
}
|
||||
|
||||
public void setPredicate(Predicate<T> predicate) {
|
||||
public void setPredicate(@NotNull Predicate<@NotNull T> predicate) {
|
||||
this.predicate = predicate;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user