Add EventFilter

This commit is contained in:
TheMode 2021-06-02 20:23:50 +02:00
parent aa5a182788
commit 7aee717196
6 changed files with 82 additions and 14 deletions

View File

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

View File

@ -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<E extends Event, H extends EventHandler> {
EventFilter<Event, EventHandler> ALL = from(Event.class);
EventFilter<EntityEvent, Entity> ENTITY = from(EntityEvent.class, Entity.class, EntityEvent::getEntity);
EventFilter<PlayerEvent, Player> PLAYER = from(PlayerEvent.class, Player.class, PlayerEvent::getPlayer);
static <E extends Event, H extends EventHandler> EventFilter<E, H> from(@NotNull Class<E> eventType,
@NotNull Class<H> handlerType,
@NotNull Function<E, H> handlerGetter) {
return new EventFilter<>() {
@Override
public @Nullable H getHandler(@NotNull E event) {
return handlerGetter.apply(event);
}
@Override
public @NotNull Class<E> getEventType() {
return eventType;
}
};
}
static <E extends Event, H extends EventHandler> EventFilter<E, H> from(@NotNull Class<E> type) {
return new EventFilter<>() {
@Override
public @Nullable H getHandler(@NotNull E event) {
return null;
}
@Override
public @NotNull Class<E> getEventType() {
return type;
}
};
}
@Nullable H getHandler(@NotNull E event);
@NotNull Class<E> getEventType();
}

View File

@ -28,7 +28,7 @@ public class EventListener<T extends Event> {
private final Class<T> eventType;
private List<Predicate<T>> filters = new ArrayList<>();
private final List<Predicate<T>> filters = new ArrayList<>();
private int expirationCount;
private UpdateOption expirationTime;
private Consumer<T> handler;
@ -47,6 +47,11 @@ public class EventListener<T extends Event> {
return this;
}
public EventListener.Builder<T> expirationTime(UpdateOption expirationTime) {
this.expirationTime = expirationTime;
return this;
}
public EventListener.Builder<T> handler(Consumer<T> handler) {
this.handler = handler;
return this;

View File

@ -10,12 +10,16 @@ import java.util.function.Predicate;
public interface EventNode<T extends Event> {
static <E extends Event> EventNode<E> type(@NotNull EventFilter<E, EventHandler> filter) {
return new EventNodeImpl<>(filter.getEventType());
}
static <E extends Event> EventNode<E> type(@NotNull Class<E> type) {
return new EventNodeImpl<>(type);
return type(EventFilter.from(type));
}
static EventNode<Event> all() {
return type(Event.class);
return type(EventFilter.ALL);
}
static <E extends Event> EventNodeConditional<E> conditional(@NotNull Class<E> type,
@ -23,10 +27,18 @@ public interface EventNode<T extends Event> {
return new EventNodeConditional<>(type, predicate);
}
static <E extends Event, H extends EventHandler> EventNodeList<E, H> list(@NotNull EventFilter<E, H> filter) {
return new EventNodeList<>(filter);
}
static <E extends Event, H extends EventHandler> EventNodeList<E, H> list(@NotNull Class<E> eventType, EventFilter<? super E, H> parser) {
return (EventNodeList<E, H>) list(parser);
}
static <E extends Event, H extends EventHandler> EventNodeList<E, H> list(@NotNull Class<E> eventType,
@NotNull Class<H> handlerType,
@NotNull Function<E, H> handlerGetter) {
return new EventNodeList<>(eventType, handlerGetter);
return list(EventFilter.from(eventType, handlerType, handlerGetter));
}
void call(@NotNull T event);

View File

@ -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<T extends Event, H extends EventHandler> extends EventNodeImpl<T> {
private final Function<T, H> handlerGetter;
private final EventFilter<T, H> filter;
private final List<H> entries = new CopyOnWriteArrayList<>();
protected EventNodeList(Class<T> eventType, Function<T, H> handlerGetter) {
super(eventType);
this.handlerGetter = handlerGetter;
protected EventNodeList(EventFilter<T, H> 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);
}

View File

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