diff --git a/src/main/java/net/minestom/server/event/EventNode.java b/src/main/java/net/minestom/server/event/EventNode.java index f8bf90e3c..f7b3befd2 100644 --- a/src/main/java/net/minestom/server/event/EventNode.java +++ b/src/main/java/net/minestom/server/event/EventNode.java @@ -14,48 +14,48 @@ import java.util.concurrent.CopyOnWriteArraySet; import java.util.function.BiPredicate; import java.util.function.Predicate; -public class EventNode { +public class EventNode { - public static EventNode type(@NotNull EventFilter filter) { - return new EventNode<>(filter); + public static EventNode type(@NotNull EventFilter filter) { + return new EventNode<>(filter, (e, v) -> true); } - public static EventNode all() { + public static EventNode all() { return type(EventFilter.ALL); } - public static EventNodeConditional conditional(@NotNull EventFilter filter, - @NotNull BiPredicate predicate) { - return new EventNodeConditional<>(filter, predicate); + public static EventNode predicate(@NotNull EventFilter filter, + @NotNull BiPredicate predicate) { + return new EventNode<>(filter, predicate); } - public static EventNodeConditional conditionalEvent(@NotNull EventFilter filter, - @NotNull Predicate predicate) { - return conditional(filter, (e, h) -> predicate.test(e)); + public static EventNode predicateEvent(@NotNull EventFilter filter, + @NotNull Predicate predicate) { + return predicate(filter, (e, h) -> predicate.test(e)); } - public static EventNodeConditional conditionalHandler(@NotNull EventFilter filter, - @NotNull Predicate predicate) { - return conditional(filter, (e, h) -> predicate.test(h)); + public static EventNode predicateValue(@NotNull EventFilter filter, + @NotNull Predicate predicate) { + return predicate(filter, (e, h) -> predicate.test(h)); } - private volatile String name = "unknown"; - private final Map, List>> listenerMap = new ConcurrentHashMap<>(); private final Map> redirectionMap = new ConcurrentHashMap<>(); - private final Set> children = new CopyOnWriteArraySet<>(); + private final Set> children = new CopyOnWriteArraySet<>(); - protected final EventFilter filter; + protected final EventFilter filter; + protected volatile BiPredicate predicate; + private volatile String name = "unknown"; // Tree data private static final Object GLOBAL_CHILD_LOCK = new Object(); - - private volatile EventNode parent; + private volatile EventNode parent; private final Object lock = new Object(); private final Object2IntMap> childEventMap = new Object2IntOpenHashMap<>(); - protected EventNode(EventFilter filter) { + protected EventNode(EventFilter filter, BiPredicate predicate) { this.filter = filter; + this.predicate = predicate; } /** @@ -65,7 +65,7 @@ public class EventNode { * @return true to enter the node, false otherwise */ protected boolean condition(@NotNull T event) { - return true; + return predicate.test(event, filter.getHandler(event)); } public void call(@NotNull T event) { @@ -107,9 +107,9 @@ public class EventNode { this.children.forEach(eventNode -> eventNode.call(event)); } - public void addChild(@NotNull EventNode child) { + public void addChild(@NotNull EventNode child) { synchronized (GLOBAL_CHILD_LOCK) { - final boolean result = this.children.add((EventNode) child); + final boolean result = this.children.add((EventNode) child); if (result) { child.parent = this; // Increase listener count @@ -123,7 +123,7 @@ public class EventNode { } } - public void removeChild(@NotNull EventNode child) { + public void removeChild(@NotNull EventNode child) { synchronized (GLOBAL_CHILD_LOCK) { final boolean result = this.children.remove(child); if (result) { @@ -167,7 +167,15 @@ public class EventNode { } } - public void map(@NotNull EventFilter filter, @NotNull V value, @NotNull EventNode node) { + public @NotNull BiPredicate getPredicate() { + return predicate; + } + + public void setPredicate(@NotNull BiPredicate predicate) { + this.predicate = predicate; + } + + public void map(@NotNull EventFilter filter, @NotNull V2 value, @NotNull EventNode node) { RedirectionEntry entry = new RedirectionEntry<>(); entry.filter = filter; entry.node = node; @@ -186,7 +194,7 @@ public class EventNode { this.name = name; } - public @NotNull Set<@NotNull EventNode> getChildren() { + public @NotNull Set<@NotNull EventNode> getChildren() { return Collections.unmodifiableSet(children); } @@ -210,6 +218,6 @@ public class EventNode { private static class RedirectionEntry { EventFilter filter; - EventNode node; + EventNode node; } } diff --git a/src/main/java/net/minestom/server/event/EventNodeConditional.java b/src/main/java/net/minestom/server/event/EventNodeConditional.java deleted file mode 100644 index 447eb2013..000000000 --- a/src/main/java/net/minestom/server/event/EventNodeConditional.java +++ /dev/null @@ -1,28 +0,0 @@ -package net.minestom.server.event; - -import org.jetbrains.annotations.NotNull; - -import java.util.function.BiPredicate; - -public class EventNodeConditional extends EventNode { - - private volatile BiPredicate predicate; - - protected EventNodeConditional(EventFilter filter, BiPredicate predicate) { - super(filter); - this.predicate = predicate; - } - - @Override - protected boolean condition(@NotNull T event) { - return predicate.test(event, (H) filter.getHandler(event)); - } - - public @NotNull BiPredicate getPredicate() { - return predicate; - } - - public void setPredicate(@NotNull BiPredicate predicate) { - this.predicate = predicate; - } -} diff --git a/src/test/java/demo/PlayerInit.java b/src/test/java/demo/PlayerInit.java index 44e6da961..867e35aa8 100644 --- a/src/test/java/demo/PlayerInit.java +++ b/src/test/java/demo/PlayerInit.java @@ -156,16 +156,8 @@ public class PlayerInit { node.call(new PlayerTickEvent(null)); empty.call(new PlayerTickEvent(null)); - /* - * Map a node to a single element - * - * var test = EventNode.type(EventFilter.ENTITY); - * test.addListener(EventListener.of(PlayerMoveEvent.class, (event) -> - * System.out.println("creative player moved"))); - * empty.map(EventFilter.ENTITY, entity, test); - */ - var conditional = EventNode.conditionalHandler(EventFilter.PLAYER, Player::isCreative); + var conditional = EventNode.predicateValue(EventFilter.PLAYER, Player::isCreative); conditional.addListener(EventListener.of(PlayerMoveEvent.class, (event) -> System.out.println("creative player moved"))); @@ -188,6 +180,11 @@ public class PlayerInit { if (source instanceof Player) { ((Player) source).sendMessage("You attacked something!"); } + + var test = EventNode.type(EventFilter.ENTITY); + test.addListener(EventListener.of(PlayerMoveEvent.class, (e) -> + System.out.println("Test movement"))); + empty.map(EventFilter.ENTITY, source, test); }); globalEventHandler.addEventCallback(PlayerDeathEvent.class, event -> {