mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-06 16:37:38 +01:00
Allow filtering on more than just EventHandler
This commit is contained in:
parent
2977bb7b66
commit
6cbc31b114
@ -10,15 +10,15 @@ import org.jetbrains.annotations.Nullable;
|
||||
|
||||
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<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) {
|
||||
static <E extends Event, H> 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) {
|
||||
@ -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<>() {
|
||||
@Override
|
||||
public @Nullable H getHandler(@NotNull E event) {
|
||||
|
@ -1,6 +1,5 @@
|
||||
package net.minestom.server.event;
|
||||
|
||||
import net.minestom.server.event.handler.EventHandler;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.List;
|
||||
@ -9,7 +8,7 @@ import java.util.function.Predicate;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
@ -17,22 +16,22 @@ public interface EventNode<T extends Event> {
|
||||
return type(EventFilter.ALL);
|
||||
}
|
||||
|
||||
static <E extends Event, H extends EventHandler> EventNodeConditional<E, H> conditional(@NotNull EventFilter<E, H> filter,
|
||||
@NotNull BiPredicate<E, H> predicate) {
|
||||
static <E extends Event, H> EventNodeConditional<E, H> conditional(@NotNull EventFilter<E, H> filter,
|
||||
@NotNull BiPredicate<E, H> predicate) {
|
||||
return new EventNodeConditional<>(filter, predicate);
|
||||
}
|
||||
|
||||
static <E extends Event, H extends EventHandler> EventNodeConditional<E, H> conditionalEvent(@NotNull EventFilter<E, H> filter,
|
||||
@NotNull Predicate<E> predicate) {
|
||||
static <E extends Event, H> EventNodeConditional<E, H> conditionalEvent(@NotNull EventFilter<E, H> filter,
|
||||
@NotNull Predicate<E> predicate) {
|
||||
return conditional(filter, (e, h) -> predicate.test(e));
|
||||
}
|
||||
|
||||
static <E extends Event, H extends EventHandler> EventNodeConditional<E, H> conditionalHandler(@NotNull EventFilter<E, H> filter,
|
||||
@NotNull Predicate<H> predicate) {
|
||||
static <E extends Event, H> EventNodeConditional<E, H> conditionalHandler(@NotNull EventFilter<E, H> filter,
|
||||
@NotNull Predicate<H> predicate) {
|
||||
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);
|
||||
}
|
||||
|
||||
|
@ -1,11 +1,10 @@
|
||||
package net.minestom.server.event;
|
||||
|
||||
import net.minestom.server.event.handler.EventHandler;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
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;
|
||||
|
||||
|
@ -1,6 +1,5 @@
|
||||
package net.minestom.server.event;
|
||||
|
||||
import net.minestom.server.event.handler.EventHandler;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Collections;
|
||||
@ -9,7 +8,7 @@ import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
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";
|
||||
|
||||
|
@ -1,12 +1,11 @@
|
||||
package net.minestom.server.event;
|
||||
|
||||
import net.minestom.server.event.handler.EventHandler;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.List;
|
||||
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<>();
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user