From 9177e94d1bc7f22a27fb8c8428cf02a40128ed3a Mon Sep 17 00:00:00 2001 From: TheMode Date: Thu, 3 Jun 2021 21:39:38 +0200 Subject: [PATCH] Allow node predicate based on tag --- .../java/net/minestom/server/event/EventNode.java | 14 ++++++++++++++ src/test/java/demo/PlayerInit.java | 3 +++ 2 files changed, 17 insertions(+) diff --git a/src/main/java/net/minestom/server/event/EventNode.java b/src/main/java/net/minestom/server/event/EventNode.java index 8d564ad28..25d06e988 100644 --- a/src/main/java/net/minestom/server/event/EventNode.java +++ b/src/main/java/net/minestom/server/event/EventNode.java @@ -2,8 +2,11 @@ package net.minestom.server.event; import it.unimi.dsi.fastutil.objects.Object2IntMap; import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap; +import net.minestom.server.tag.Tag; +import net.minestom.server.tag.TagReadable; import net.minestom.server.utils.validate.Check; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.util.*; import java.util.concurrent.ConcurrentHashMap; @@ -38,6 +41,17 @@ public class EventNode { return predicate(filter, (e, h) -> predicate.test(h)); } + public static EventNode predicateTag(@NotNull EventFilter filter, + @NotNull Tag tag) { + return predicate(filter, (e, h) -> h.hasTag(tag)); + } + + public static EventNode predicateTag(@NotNull EventFilter filter, + @NotNull Tag tag, + @NotNull Predicate<@Nullable V2> consumer) { + return predicate(filter, (e, h) -> consumer.test(h.getTag(tag))); + } + private final Map, List>> listenerMap = new ConcurrentHashMap<>(); private final Map> redirectionMap = new ConcurrentHashMap<>(); private final Set> children = new CopyOnWriteArraySet<>(); diff --git a/src/test/java/demo/PlayerInit.java b/src/test/java/demo/PlayerInit.java index 3f4657242..672fa7285 100644 --- a/src/test/java/demo/PlayerInit.java +++ b/src/test/java/demo/PlayerInit.java @@ -35,6 +35,7 @@ import net.minestom.server.item.metadata.CompassMeta; import net.minestom.server.monitoring.BenchmarkManager; import net.minestom.server.monitoring.TickMonitor; import net.minestom.server.network.ConnectionManager; +import net.minestom.server.tag.Tag; import net.minestom.server.utils.MathUtils; import net.minestom.server.utils.Position; import net.minestom.server.utils.Vector; @@ -152,6 +153,8 @@ public class PlayerInit { var conditional = EventNode.predicateValue(EventFilter.PLAYER, Player::isCreative) .addListener(PlayerMoveEvent.class, (event) -> System.out.println("creative player moved")); + var tagNode = EventNode.predicateTag(EventFilter.ITEM, Tag.String("tag"), s -> s != null && !s.isEmpty()); + node.addChild(conditional); node.call(new PlayerTickEvent(null));