From 4f127b2e0ed0fcb80a0fe93b2140df41d2eba9c4 Mon Sep 17 00:00:00 2001 From: TheMode Date: Wed, 2 Jun 2021 21:08:10 +0200 Subject: [PATCH] Separate conditional event from conditional handler --- .../java/net/minestom/server/event/EventNode.java | 13 ++++++++++++- .../minestom/server/event/EventNodeConditional.java | 12 ++++++------ .../net/minestom/server/event/EventNodeImpl.java | 6 ++++++ src/test/java/demo/PlayerInit.java | 9 ++++++--- 4 files changed, 30 insertions(+), 10 deletions(-) diff --git a/src/main/java/net/minestom/server/event/EventNode.java b/src/main/java/net/minestom/server/event/EventNode.java index d7dc8dcbc..67d6358b1 100644 --- a/src/main/java/net/minestom/server/event/EventNode.java +++ b/src/main/java/net/minestom/server/event/EventNode.java @@ -4,6 +4,7 @@ import net.minestom.server.event.handler.EventHandler; import org.jetbrains.annotations.NotNull; import java.util.List; +import java.util.function.BiPredicate; import java.util.function.Predicate; public interface EventNode { @@ -17,10 +18,20 @@ public interface EventNode { } static EventNodeConditional conditional(@NotNull EventFilter filter, - @NotNull Predicate predicate) { + @NotNull BiPredicate predicate) { return new EventNodeConditional<>(filter, predicate); } + static EventNodeConditional conditionalEvent(@NotNull EventFilter filter, + @NotNull Predicate predicate) { + return conditional(filter, (e, h) -> predicate.test(e)); + } + + static EventNodeConditional conditionalHandler(@NotNull EventFilter filter, + @NotNull Predicate predicate) { + return conditional(filter, (e, h) -> predicate.test(h)); + } + static EventNodeList list(@NotNull EventFilter filter) { return new EventNodeList<>(filter); } diff --git a/src/main/java/net/minestom/server/event/EventNodeConditional.java b/src/main/java/net/minestom/server/event/EventNodeConditional.java index 3f8676978..308a0a4a1 100644 --- a/src/main/java/net/minestom/server/event/EventNodeConditional.java +++ b/src/main/java/net/minestom/server/event/EventNodeConditional.java @@ -3,27 +3,27 @@ package net.minestom.server.event; import net.minestom.server.event.handler.EventHandler; import org.jetbrains.annotations.NotNull; -import java.util.function.Predicate; +import java.util.function.BiPredicate; public class EventNodeConditional extends EventNodeImpl { - private volatile Predicate predicate; + private volatile BiPredicate predicate; - protected EventNodeConditional(EventFilter filter, Predicate predicate) { + protected EventNodeConditional(EventFilter filter, BiPredicate predicate) { super(filter); this.predicate = predicate; } @Override protected boolean condition(@NotNull T event) { - return predicate.test(event); + return predicate.test(event, filter.getHandler(event)); } - public @NotNull Predicate<@NotNull T> getPredicate() { + public @NotNull BiPredicate getPredicate() { return predicate; } - public void setPredicate(@NotNull Predicate<@NotNull T> predicate) { + public void setPredicate(@NotNull BiPredicate predicate) { this.predicate = predicate; } } diff --git a/src/main/java/net/minestom/server/event/EventNodeImpl.java b/src/main/java/net/minestom/server/event/EventNodeImpl.java index 72811af1f..e6b25b983 100644 --- a/src/main/java/net/minestom/server/event/EventNodeImpl.java +++ b/src/main/java/net/minestom/server/event/EventNodeImpl.java @@ -22,6 +22,12 @@ class EventNodeImpl implements EventNod this.filter = filter; } + /** + * Condition to enter the node. + * + * @param event the called event + * @return true to enter the node, false otherwise + */ protected boolean condition(@NotNull T event) { return true; } diff --git a/src/test/java/demo/PlayerInit.java b/src/test/java/demo/PlayerInit.java index 125858c99..589624ef0 100644 --- a/src/test/java/demo/PlayerInit.java +++ b/src/test/java/demo/PlayerInit.java @@ -147,11 +147,14 @@ public class PlayerInit { empty.addListener(EventListener.of(PlayerMoveEvent.class, (event) -> { })); - var list = EventNode.list(EventFilter.PLAYER); - list.addListener(EventListener.of(PlayerMoveEvent.class, playerMoveEvent -> { - System.out.println("move"); + var conditional = EventNode.conditionalHandler(EventFilter.PLAYER, Player::isCreative); + conditional.addListener(EventListener.of(PlayerMoveEvent.class, (event) -> { + System.out.println("creative player moved"); })); + var list = EventNode.list(EventFilter.PLAYER); + list.addListener(EventListener.of(PlayerMoveEvent.class, playerMoveEvent -> System.out.println("move"))); + GlobalEventHandler globalEventHandler = MinecraftServer.getGlobalEventHandler(); globalEventHandler.addEventCallback(EntityAttackEvent.class, event -> { final Entity source = event.getEntity();