diff --git a/src/main/java/net/minestom/server/event/EventNode.java b/src/main/java/net/minestom/server/event/EventNode.java index 5dc9a3ee4..d753dc53e 100644 --- a/src/main/java/net/minestom/server/event/EventNode.java +++ b/src/main/java/net/minestom/server/event/EventNode.java @@ -5,38 +5,27 @@ import org.jetbrains.annotations.NotNull; import java.util.List; import java.util.function.Consumer; -import java.util.function.Function; import java.util.function.Predicate; public interface EventNode { - static EventNode type(@NotNull EventFilter filter) { - return new EventNodeImpl<>(filter.getEventType()); - } - - static EventNode type(@NotNull Class type) { - return type(EventFilter.from(type)); + static EventNode type(@NotNull EventFilter filter) { + return new EventNodeImpl<>(filter); } static EventNode all() { return type(EventFilter.ALL); } - static EventNodeConditional conditional(@NotNull Class type, - @NotNull Predicate predicate) { - return new EventNodeConditional<>(type, predicate); + static EventNodeConditional conditional(@NotNull EventFilter filter, + @NotNull Predicate predicate) { + return new EventNodeConditional<>(filter, predicate); } static EventNodeList list(@NotNull EventFilter filter) { return new EventNodeList<>(filter); } - static EventNodeList list(@NotNull Class eventType, - @NotNull Class handlerType, - @NotNull Function handlerGetter) { - return list(EventFilter.from(eventType, handlerType, handlerGetter)); - } - void call(@NotNull T event); void addChild(@NotNull EventNode child); diff --git a/src/main/java/net/minestom/server/event/EventNodeConditional.java b/src/main/java/net/minestom/server/event/EventNodeConditional.java index 7ba2948e4..3f8676978 100644 --- a/src/main/java/net/minestom/server/event/EventNodeConditional.java +++ b/src/main/java/net/minestom/server/event/EventNodeConditional.java @@ -1,15 +1,16 @@ package net.minestom.server.event; +import net.minestom.server.event.handler.EventHandler; import org.jetbrains.annotations.NotNull; import java.util.function.Predicate; -public class EventNodeConditional extends EventNodeImpl { +public class EventNodeConditional extends EventNodeImpl { private volatile Predicate predicate; - protected EventNodeConditional(Class type, Predicate predicate) { - super(type); + protected EventNodeConditional(EventFilter filter, Predicate predicate) { + super(filter); 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 cc23dd219..72811af1f 100644 --- a/src/main/java/net/minestom/server/event/EventNodeImpl.java +++ b/src/main/java/net/minestom/server/event/EventNodeImpl.java @@ -1,5 +1,6 @@ package net.minestom.server.event; +import net.minestom.server.event.handler.EventHandler; import org.jetbrains.annotations.NotNull; import java.util.Collections; @@ -8,17 +9,17 @@ import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.CopyOnWriteArrayList; -class EventNodeImpl implements EventNode { +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 final EventFilter filter; - protected EventNodeImpl(Class type) { - this.type = type; + protected EventNodeImpl(EventFilter filter) { + this.filter = filter; } protected boolean condition(@NotNull T event) { @@ -27,7 +28,7 @@ class EventNodeImpl implements EventNode { @Override public void call(@NotNull T event) { - if (!type.isAssignableFrom(event.getClass())) { + if (!filter.getEventType().isAssignableFrom(event.getClass())) { // Invalid event type return; } diff --git a/src/main/java/net/minestom/server/event/EventNodeList.java b/src/main/java/net/minestom/server/event/EventNodeList.java index 680585630..b7e383509 100644 --- a/src/main/java/net/minestom/server/event/EventNodeList.java +++ b/src/main/java/net/minestom/server/event/EventNodeList.java @@ -6,15 +6,12 @@ import org.jetbrains.annotations.NotNull; import java.util.List; import java.util.concurrent.CopyOnWriteArrayList; -public class EventNodeList extends EventNodeImpl { - - private final EventFilter filter; +public class EventNodeList extends EventNodeImpl { private final List entries = new CopyOnWriteArrayList<>(); protected EventNodeList(EventFilter filter) { - super(filter.getEventType()); - this.filter = filter; + super(filter); } @Override diff --git a/src/test/java/demo/PlayerInit.java b/src/test/java/demo/PlayerInit.java index b6efcbd57..0b2800a9b 100644 --- a/src/test/java/demo/PlayerInit.java +++ b/src/test/java/demo/PlayerInit.java @@ -19,7 +19,6 @@ import net.minestom.server.event.entity.EntityAttackEvent; import net.minestom.server.event.item.ItemDropEvent; import net.minestom.server.event.item.PickupItemEvent; import net.minestom.server.event.player.*; -import net.minestom.server.event.trait.PlayerEvent; import net.minestom.server.instance.Chunk; import net.minestom.server.instance.Instance; import net.minestom.server.instance.InstanceContainer; @@ -138,7 +137,7 @@ public class PlayerInit { // EVENT REGISTERING - var node = EventNode.type(PlayerEvent.class); + var node = EventNode.type(EventFilter.PLAYER); node.addListener(EventListener.of(PlayerTickEvent.class) .handler(playerTickEvent -> System.out.println("Player tick!")) .expirationCount(2)