Allow filtering on more than just EventHandler

This commit is contained in:
TheMode 2021-06-02 21:18:18 +02:00
parent 2977bb7b66
commit 6cbc31b114
5 changed files with 16 additions and 20 deletions

View File

@ -10,15 +10,15 @@ import org.jetbrains.annotations.Nullable;
import java.util.function.Function; import java.util.function.Function;
public interface EventFilter<E extends Event, H extends EventHandler> { public interface EventFilter<E extends Event, H> {
EventFilter<Event, EventHandler> ALL = from(Event.class); EventFilter<Event, EventHandler> ALL = from(Event.class);
EventFilter<EntityEvent, Entity> ENTITY = from(EntityEvent.class, Entity.class, EntityEvent::getEntity); EventFilter<EntityEvent, Entity> ENTITY = from(EntityEvent.class, Entity.class, EntityEvent::getEntity);
EventFilter<PlayerEvent, Player> PLAYER = from(PlayerEvent.class, Player.class, PlayerEvent::getPlayer); 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, static <E extends Event, H> EventFilter<E, H> from(@NotNull Class<E> eventType,
@NotNull Class<H> handlerType, @NotNull Class<H> handlerType,
@NotNull Function<E, H> handlerGetter) { @NotNull Function<E, H> handlerGetter) {
return new EventFilter<>() { return new EventFilter<>() {
@Override @Override
public @Nullable H getHandler(@NotNull E event) { public @Nullable H getHandler(@NotNull E event) {
@ -32,7 +32,7 @@ public interface EventFilter<E extends Event, H extends EventHandler> {
}; };
} }
static <E extends Event, H extends EventHandler> EventFilter<E, H> from(@NotNull Class<E> type) { static <E extends Event, H> EventFilter<E, H> from(@NotNull Class<E> type) {
return new EventFilter<>() { return new EventFilter<>() {
@Override @Override
public @Nullable H getHandler(@NotNull E event) { public @Nullable H getHandler(@NotNull E event) {

View File

@ -1,6 +1,5 @@
package net.minestom.server.event; package net.minestom.server.event;
import net.minestom.server.event.handler.EventHandler;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.List; import java.util.List;
@ -9,7 +8,7 @@ import java.util.function.Predicate;
public interface EventNode<T extends Event> { public interface EventNode<T extends Event> {
static <E extends Event> EventNode<E> type(@NotNull EventFilter<E, ? extends EventHandler> filter) { static <E extends Event> EventNode<E> type(@NotNull EventFilter<E, ?> filter) {
return new EventNodeImpl<>(filter); return new EventNodeImpl<>(filter);
} }
@ -17,22 +16,22 @@ public interface EventNode<T extends Event> {
return type(EventFilter.ALL); return type(EventFilter.ALL);
} }
static <E extends Event, H extends EventHandler> EventNodeConditional<E, H> conditional(@NotNull EventFilter<E, H> filter, static <E extends Event, H> EventNodeConditional<E, H> conditional(@NotNull EventFilter<E, H> filter,
@NotNull BiPredicate<E, H> predicate) { @NotNull BiPredicate<E, H> predicate) {
return new EventNodeConditional<>(filter, predicate); return new EventNodeConditional<>(filter, predicate);
} }
static <E extends Event, H extends EventHandler> EventNodeConditional<E, H> conditionalEvent(@NotNull EventFilter<E, H> filter, static <E extends Event, H> EventNodeConditional<E, H> conditionalEvent(@NotNull EventFilter<E, H> filter,
@NotNull Predicate<E> predicate) { @NotNull Predicate<E> predicate) {
return conditional(filter, (e, h) -> predicate.test(e)); return conditional(filter, (e, h) -> predicate.test(e));
} }
static <E extends Event, H extends EventHandler> EventNodeConditional<E, H> conditionalHandler(@NotNull EventFilter<E, H> filter, static <E extends Event, H> EventNodeConditional<E, H> conditionalHandler(@NotNull EventFilter<E, H> filter,
@NotNull Predicate<H> predicate) { @NotNull Predicate<H> predicate) {
return conditional(filter, (e, h) -> predicate.test(h)); return conditional(filter, (e, h) -> predicate.test(h));
} }
static <E extends Event, H extends EventHandler> EventNodeList<E, H> list(@NotNull EventFilter<E, H> filter) { static <E extends Event, H> EventNodeList<E, H> list(@NotNull EventFilter<E, H> filter) {
return new EventNodeList<>(filter); return new EventNodeList<>(filter);
} }

View File

@ -1,11 +1,10 @@
package net.minestom.server.event; package net.minestom.server.event;
import net.minestom.server.event.handler.EventHandler;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.function.BiPredicate; import java.util.function.BiPredicate;
public class EventNodeConditional<T extends Event, H extends EventHandler> extends EventNodeImpl<T, H> { public class EventNodeConditional<T extends Event, H> extends EventNodeImpl<T, H> {
private volatile BiPredicate<T, H> predicate; private volatile BiPredicate<T, H> predicate;

View File

@ -1,6 +1,5 @@
package net.minestom.server.event; package net.minestom.server.event;
import net.minestom.server.event.handler.EventHandler;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.Collections; import java.util.Collections;
@ -9,7 +8,7 @@ import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.CopyOnWriteArrayList;
class EventNodeImpl<T extends Event, H extends EventHandler> implements EventNode<T> { class EventNodeImpl<T extends Event, H> implements EventNode<T> {
private final String name = "debug"; private final String name = "debug";

View File

@ -1,12 +1,11 @@
package net.minestom.server.event; package net.minestom.server.event;
import net.minestom.server.event.handler.EventHandler;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.List; import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.CopyOnWriteArrayList;
public class EventNodeList<T extends Event, H extends EventHandler> extends EventNodeImpl<T, H> { public class EventNodeList<T extends Event, H> extends EventNodeImpl<T, H> {
private final List<H> entries = new CopyOnWriteArrayList<>(); private final List<H> entries = new CopyOnWriteArrayList<>();