From 7aee71719675000789b9b9775e77412590dd5010 Mon Sep 17 00:00:00 2001 From: TheMode Date: Wed, 2 Jun 2021 20:23:50 +0200 Subject: [PATCH] Add EventFilter --- .../server/event/CancellableEvent.java | 3 +- .../minestom/server/event/EventFilter.java | 52 +++++++++++++++++++ .../minestom/server/event/EventListener.java | 7 ++- .../net/minestom/server/event/EventNode.java | 18 +++++-- .../minestom/server/event/EventNodeList.java | 11 ++-- src/test/java/demo/PlayerInit.java | 5 +- 6 files changed, 82 insertions(+), 14 deletions(-) create mode 100644 src/main/java/net/minestom/server/event/EventFilter.java diff --git a/src/main/java/net/minestom/server/event/CancellableEvent.java b/src/main/java/net/minestom/server/event/CancellableEvent.java index 8ae37b32b..ef821f834 100644 --- a/src/main/java/net/minestom/server/event/CancellableEvent.java +++ b/src/main/java/net/minestom/server/event/CancellableEvent.java @@ -3,7 +3,7 @@ package net.minestom.server.event; /** * Represents an {@link Event} which can be cancelled. */ -public interface CancellableEvent extends Event{ +public interface CancellableEvent extends Event { /** * Gets if the {@link Event} should be cancelled or not. @@ -18,5 +18,4 @@ public interface CancellableEvent extends Event{ * @param cancel true if the event should be cancelled, false otherwise */ void setCancelled(boolean cancel); - } diff --git a/src/main/java/net/minestom/server/event/EventFilter.java b/src/main/java/net/minestom/server/event/EventFilter.java new file mode 100644 index 000000000..d3a9577b7 --- /dev/null +++ b/src/main/java/net/minestom/server/event/EventFilter.java @@ -0,0 +1,52 @@ +package net.minestom.server.event; + +import net.minestom.server.entity.Entity; +import net.minestom.server.entity.Player; +import net.minestom.server.event.handler.EventHandler; +import net.minestom.server.event.trait.EntityEvent; +import net.minestom.server.event.trait.PlayerEvent; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.function.Function; + +public interface EventFilter { + + EventFilter ALL = from(Event.class); + EventFilter ENTITY = from(EntityEvent.class, Entity.class, EntityEvent::getEntity); + EventFilter PLAYER = from(PlayerEvent.class, Player.class, PlayerEvent::getPlayer); + + static EventFilter from(@NotNull Class eventType, + @NotNull Class handlerType, + @NotNull Function handlerGetter) { + return new EventFilter<>() { + @Override + public @Nullable H getHandler(@NotNull E event) { + return handlerGetter.apply(event); + } + + @Override + public @NotNull Class getEventType() { + return eventType; + } + }; + } + + static EventFilter from(@NotNull Class type) { + return new EventFilter<>() { + @Override + public @Nullable H getHandler(@NotNull E event) { + return null; + } + + @Override + public @NotNull Class getEventType() { + return type; + } + }; + } + + @Nullable H getHandler(@NotNull E event); + + @NotNull Class getEventType(); +} diff --git a/src/main/java/net/minestom/server/event/EventListener.java b/src/main/java/net/minestom/server/event/EventListener.java index b960d788e..56beca997 100644 --- a/src/main/java/net/minestom/server/event/EventListener.java +++ b/src/main/java/net/minestom/server/event/EventListener.java @@ -28,7 +28,7 @@ public class EventListener { private final Class eventType; - private List> filters = new ArrayList<>(); + private final List> filters = new ArrayList<>(); private int expirationCount; private UpdateOption expirationTime; private Consumer handler; @@ -47,6 +47,11 @@ public class EventListener { return this; } + public EventListener.Builder expirationTime(UpdateOption expirationTime) { + this.expirationTime = expirationTime; + return this; + } + public EventListener.Builder handler(Consumer handler) { this.handler = handler; return this; diff --git a/src/main/java/net/minestom/server/event/EventNode.java b/src/main/java/net/minestom/server/event/EventNode.java index 21ce0f70e..9a6aae551 100644 --- a/src/main/java/net/minestom/server/event/EventNode.java +++ b/src/main/java/net/minestom/server/event/EventNode.java @@ -10,12 +10,16 @@ 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 new EventNodeImpl<>(type); + return type(EventFilter.from(type)); } static EventNode all() { - return type(Event.class); + return type(EventFilter.ALL); } static EventNodeConditional conditional(@NotNull Class type, @@ -23,10 +27,18 @@ public interface EventNode { return new EventNodeConditional<>(type, predicate); } + static EventNodeList list(@NotNull EventFilter filter) { + return new EventNodeList<>(filter); + } + + static EventNodeList list(@NotNull Class eventType, EventFilter parser) { + return (EventNodeList) list(parser); + } + static EventNodeList list(@NotNull Class eventType, @NotNull Class handlerType, @NotNull Function handlerGetter) { - return new EventNodeList<>(eventType, handlerGetter); + return list(EventFilter.from(eventType, handlerType, handlerGetter)); } void call(@NotNull T event); diff --git a/src/main/java/net/minestom/server/event/EventNodeList.java b/src/main/java/net/minestom/server/event/EventNodeList.java index 55d39f283..680585630 100644 --- a/src/main/java/net/minestom/server/event/EventNodeList.java +++ b/src/main/java/net/minestom/server/event/EventNodeList.java @@ -5,22 +5,21 @@ import org.jetbrains.annotations.NotNull; import java.util.List; import java.util.concurrent.CopyOnWriteArrayList; -import java.util.function.Function; public class EventNodeList extends EventNodeImpl { - private final Function handlerGetter; + private final EventFilter filter; private final List entries = new CopyOnWriteArrayList<>(); - protected EventNodeList(Class eventType, Function handlerGetter) { - super(eventType); - this.handlerGetter = handlerGetter; + protected EventNodeList(EventFilter filter) { + super(filter.getEventType()); + this.filter = filter; } @Override protected boolean condition(@NotNull T event) { - final var eventHandler = handlerGetter.apply(event); + final var eventHandler = filter.getHandler(event); return entries.contains(eventHandler); } diff --git a/src/test/java/demo/PlayerInit.java b/src/test/java/demo/PlayerInit.java index 012964aca..b6efcbd57 100644 --- a/src/test/java/demo/PlayerInit.java +++ b/src/test/java/demo/PlayerInit.java @@ -11,6 +11,7 @@ import net.minestom.server.entity.GameMode; import net.minestom.server.entity.ItemEntity; import net.minestom.server.entity.Player; import net.minestom.server.entity.damage.DamageType; +import net.minestom.server.event.EventFilter; import net.minestom.server.event.EventListener; import net.minestom.server.event.EventNode; import net.minestom.server.event.GlobalEventHandler; @@ -147,8 +148,8 @@ public class PlayerInit { empty.addListener(PlayerMoveEvent.class, (event) -> { }); - var map = EventNode.list(PlayerEvent.class, Player.class, PlayerEvent::getPlayer); - map.addListener(PlayerMoveEvent.class, playerMoveEvent -> { + var list = EventNode.list(EventFilter.PLAYER); + list.addListener(PlayerMoveEvent.class, playerMoveEvent -> { System.out.println("move"); });