From bc2a075b605d94b1502bbe02f263d9743ba7ebd4 Mon Sep 17 00:00:00 2001 From: TheMode Date: Wed, 2 Jun 2021 11:08:48 +0200 Subject: [PATCH] Fix event calling --- .../net/minestom/server/event/EventListener.java | 15 ++++++--------- .../net/minestom/server/event/EventNode.java | 16 ++++++++++++---- .../server/event/EventNodeConditional.java | 11 +++++++---- 3 files changed, 25 insertions(+), 17 deletions(-) diff --git a/src/main/java/net/minestom/server/event/EventListener.java b/src/main/java/net/minestom/server/event/EventListener.java index 24cf4b2cf..03c6ab9e4 100644 --- a/src/main/java/net/minestom/server/event/EventListener.java +++ b/src/main/java/net/minestom/server/event/EventListener.java @@ -9,20 +9,18 @@ import java.util.function.Predicate; public class EventListener { - private final Consumer combined; + protected final Class type; + protected final Consumer combined; - private EventListener(@NotNull Consumer combined) { + private EventListener(@NotNull Class type, @NotNull Consumer combined) { + this.type = type; this.combined = combined; } - public static EventListener.Builder of(Class eventType) { + public static EventListener.Builder of(@NotNull Class eventType) { return new EventListener.Builder<>(eventType); } - protected Consumer getCombined() { - return combined; - } - public static class Builder { private final Class eventType; @@ -45,7 +43,7 @@ public class EventListener { } public EventListener 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 { return; } } - // Handler if (handler != null) { handler.accept(event); diff --git a/src/main/java/net/minestom/server/event/EventNode.java b/src/main/java/net/minestom/server/event/EventNode.java index 09c4462bc..c21c03512 100644 --- a/src/main/java/net/minestom/server/event/EventNode.java +++ b/src/main/java/net/minestom/server/event/EventNode.java @@ -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 { private final String name = "debug"; - private final List> listeners = new CopyOnWriteArrayList<>(); + + private final Map, List>> listenerMap = new ConcurrentHashMap<>(); private final List> children = new CopyOnWriteArrayList<>(); private static final EventNode EMPTY = new EventNode<>() { @@ -27,7 +30,7 @@ public abstract class EventNode { public static EventNode conditional(@NotNull Class type, @NotNull Predicate predicate) { - return new EventNodeConditional<>(predicate); + return new EventNodeConditional<>(type, predicate); } public static EventNode conditional(@NotNull Class eventType) { @@ -39,7 +42,11 @@ public abstract class EventNode { 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 { } public void addListener(@NotNull EventListener listener) { - this.listeners.add((EventListener) listener); + this.listenerMap.computeIfAbsent(listener.type, aClass -> new CopyOnWriteArrayList<>()) + .add((EventListener) listener); } public void addListener(@NotNull Class eventClass, @NotNull Consumer<@NotNull E> listener) { diff --git a/src/main/java/net/minestom/server/event/EventNodeConditional.java b/src/main/java/net/minestom/server/event/EventNodeConditional.java index ec37e4eec..5bd70c085 100644 --- a/src/main/java/net/minestom/server/event/EventNodeConditional.java +++ b/src/main/java/net/minestom/server/event/EventNodeConditional.java @@ -6,22 +6,25 @@ import java.util.function.Predicate; public class EventNodeConditional extends EventNode { + private final Class type; private volatile Predicate predicate; - protected EventNodeConditional(Predicate predicate) { + protected EventNodeConditional(Class type, Predicate 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 getPredicate() { + public @NotNull Predicate<@NotNull T> getPredicate() { return predicate; } - public void setPredicate(Predicate predicate) { + public void setPredicate(@NotNull Predicate<@NotNull T> predicate) { this.predicate = predicate; } }