Allow node predicate based on tag

This commit is contained in:
TheMode 2021-06-03 21:39:38 +02:00
parent 27bdbe7275
commit 9177e94d1b
2 changed files with 17 additions and 0 deletions

View File

@ -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<T extends Event> {
return predicate(filter, (e, h) -> predicate.test(h));
}
public static <E extends Event, V extends TagReadable> EventNode<E> predicateTag(@NotNull EventFilter<E, V> filter,
@NotNull Tag<?> tag) {
return predicate(filter, (e, h) -> h.hasTag(tag));
}
public static <E extends Event, V extends TagReadable, V2> EventNode<E> predicateTag(@NotNull EventFilter<E, V> filter,
@NotNull Tag<V2> tag,
@NotNull Predicate<@Nullable V2> consumer) {
return predicate(filter, (e, h) -> consumer.test(h.getTag(tag)));
}
private final Map<Class<? extends T>, List<EventListener<T>>> listenerMap = new ConcurrentHashMap<>();
private final Map<Object, EventNode<T>> redirectionMap = new ConcurrentHashMap<>();
private final Set<EventNode<T>> children = new CopyOnWriteArraySet<>();

View File

@ -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));