diff --git a/src/main/java/net/minestom/server/event/EventNode.java b/src/main/java/net/minestom/server/event/EventNode.java index 902f48ea6..fd59584ee 100644 --- a/src/main/java/net/minestom/server/event/EventNode.java +++ b/src/main/java/net/minestom/server/event/EventNode.java @@ -3,90 +3,43 @@ package net.minestom.server.event; import net.minestom.server.event.handler.EventHandler; 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.Function; import java.util.function.Predicate; -public class EventNode { +public interface EventNode { - private final String name = "debug"; - - private final Map, List>> listenerMap = new ConcurrentHashMap<>(); - private final List> children = new CopyOnWriteArrayList<>(); - - protected final Class type; - - protected EventNode(Class type) { - this.type = type; + static EventNode create(@NotNull Class type) { + return new EventNodeImpl<>(type); } - public static EventNode create(@NotNull Class type) { - return new EventNode<>(type); - } - - public static EventNode create() { + static EventNode create() { return create(Event.class); } - public static EventNode conditional(@NotNull Class type, - @NotNull Predicate predicate) { + static EventNode conditional(@NotNull Class type, + @NotNull Predicate predicate) { return new EventNodeConditional<>(type, predicate); } - public static EventNode map(@NotNull Class eventType, - @NotNull Class handlerType, - @NotNull Function handlerGetter) { + static EventNode map(@NotNull Class eventType, + @NotNull Class handlerType, + @NotNull Function handlerGetter) { return new EventNodeMap<>(eventType, handlerGetter); } - protected boolean condition(@NotNull T event) { - return true; - } + void call(@NotNull T event); - public void call(@NotNull T 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(listener -> { - final EventListener.Result result = listener.executor.apply(event); - if (result == EventListener.Result.EXPIRED) { - listeners.remove(listener); - } - }); - } - this.children.forEach(eventNode -> eventNode.call(event)); - } + void addChild(@NotNull EventNode child); - public void addChild(@NotNull EventNode child) { - this.children.add((EventNode) child); - } + void addListener(@NotNull EventListener listener); - public void addListener(@NotNull EventListener listener) { - this.listenerMap.computeIfAbsent(listener.type, aClass -> new CopyOnWriteArrayList<>()) - .add((EventListener) listener); - } - - public void addListener(@NotNull Class eventClass, @NotNull Consumer<@NotNull E> listener) { + default void addListener(@NotNull Class eventClass, @NotNull Consumer<@NotNull E> listener) { addListener(EventListener.of(eventClass).handler(listener).build()); } - public @NotNull String getName() { - return name; - } + @NotNull String getName(); - public @NotNull List<@NotNull EventNode> getChildren() { - return Collections.unmodifiableList(children); - } + @NotNull List<@NotNull EventNode> getChildren(); } diff --git a/src/main/java/net/minestom/server/event/EventNodeConditional.java b/src/main/java/net/minestom/server/event/EventNodeConditional.java index fb3982bc9..7ba2948e4 100644 --- a/src/main/java/net/minestom/server/event/EventNodeConditional.java +++ b/src/main/java/net/minestom/server/event/EventNodeConditional.java @@ -4,7 +4,7 @@ import org.jetbrains.annotations.NotNull; import java.util.function.Predicate; -public class EventNodeConditional extends EventNode { +public class EventNodeConditional extends EventNodeImpl { private volatile Predicate predicate; diff --git a/src/main/java/net/minestom/server/event/EventNodeImpl.java b/src/main/java/net/minestom/server/event/EventNodeImpl.java new file mode 100644 index 000000000..2e0add8b8 --- /dev/null +++ b/src/main/java/net/minestom/server/event/EventNodeImpl.java @@ -0,0 +1,70 @@ +package net.minestom.server.event; + +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; + +class EventNodeImpl implements EventNode { + + private final String name = "debug"; + + private final Map, List>> listenerMap = new ConcurrentHashMap<>(); + private final List> children = new CopyOnWriteArrayList<>(); + + protected final Class type; + + protected EventNodeImpl(Class type) { + this.type = type; + } + + protected boolean condition(@NotNull T event) { + return true; + } + + @Override + public void call(@NotNull T 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(listener -> { + final EventListener.Result result = listener.executor.apply(event); + if (result == EventListener.Result.EXPIRED) { + listeners.remove(listener); + } + }); + } + this.children.forEach(eventNode -> eventNode.call(event)); + } + + @Override + public void addChild(@NotNull EventNode child) { + this.children.add((EventNode) child); + } + + @Override + public void addListener(@NotNull EventListener listener) { + this.listenerMap.computeIfAbsent(listener.type, aClass -> new CopyOnWriteArrayList<>()) + .add((EventListener) listener); + } + + @Override + public @NotNull String getName() { + return name; + } + + @Override + public @NotNull List<@NotNull EventNode> getChildren() { + return Collections.unmodifiableList(children); + } +} diff --git a/src/main/java/net/minestom/server/event/EventNodeMap.java b/src/main/java/net/minestom/server/event/EventNodeMap.java index 4ca91c3d2..31881a816 100644 --- a/src/main/java/net/minestom/server/event/EventNodeMap.java +++ b/src/main/java/net/minestom/server/event/EventNodeMap.java @@ -7,7 +7,7 @@ import java.util.List; import java.util.concurrent.CopyOnWriteArrayList; import java.util.function.Function; -public class EventNodeMap extends EventNode { +public class EventNodeMap extends EventNodeImpl { private final Function handlerGetter;