mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-06 16:37:38 +01:00
Force the use of EventFilter
This commit is contained in:
parent
e670a0a40d
commit
15ae5fd3c1
@ -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<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 type(EventFilter.from(type));
|
||||
static <E extends Event, H extends EventHandler> EventNode<E> type(@NotNull EventFilter<E, H> filter) {
|
||||
return new EventNodeImpl<>(filter);
|
||||
}
|
||||
|
||||
static EventNode<Event> all() {
|
||||
return type(EventFilter.ALL);
|
||||
}
|
||||
|
||||
static <E extends Event> EventNodeConditional<E> conditional(@NotNull Class<E> type,
|
||||
@NotNull Predicate<E> predicate) {
|
||||
return new EventNodeConditional<>(type, predicate);
|
||||
static <E extends Event, H extends EventHandler> EventNodeConditional<E, H> conditional(@NotNull EventFilter<E, H> filter,
|
||||
@NotNull Predicate<E> predicate) {
|
||||
return new EventNodeConditional<>(filter, 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,
|
||||
@NotNull Class<H> handlerType,
|
||||
@NotNull Function<E, H> handlerGetter) {
|
||||
return list(EventFilter.from(eventType, handlerType, handlerGetter));
|
||||
}
|
||||
|
||||
void call(@NotNull T event);
|
||||
|
||||
void addChild(@NotNull EventNode<? extends T> child);
|
||||
|
@ -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<T extends Event> extends EventNodeImpl<T> {
|
||||
public class EventNodeConditional<T extends Event, H extends EventHandler> extends EventNodeImpl<T, H> {
|
||||
|
||||
private volatile Predicate<T> predicate;
|
||||
|
||||
protected EventNodeConditional(Class<T> type, Predicate<T> predicate) {
|
||||
super(type);
|
||||
protected EventNodeConditional(EventFilter<T, H> filter, Predicate<T> predicate) {
|
||||
super(filter);
|
||||
this.predicate = predicate;
|
||||
}
|
||||
|
||||
|
@ -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<T extends Event> implements EventNode<T> {
|
||||
class EventNodeImpl<T extends Event, H extends EventHandler> implements EventNode<T> {
|
||||
|
||||
private final String name = "debug";
|
||||
|
||||
private final Map<Class<? extends T>, List<EventListener<T>>> listenerMap = new ConcurrentHashMap<>();
|
||||
private final List<EventNode<T>> children = new CopyOnWriteArrayList<>();
|
||||
|
||||
protected final Class<T> type;
|
||||
protected final EventFilter<T, H> filter;
|
||||
|
||||
protected EventNodeImpl(Class<T> type) {
|
||||
this.type = type;
|
||||
protected EventNodeImpl(EventFilter<T, H> filter) {
|
||||
this.filter = filter;
|
||||
}
|
||||
|
||||
protected boolean condition(@NotNull T event) {
|
||||
@ -27,7 +28,7 @@ class EventNodeImpl<T extends Event> implements EventNode<T> {
|
||||
|
||||
@Override
|
||||
public void call(@NotNull T event) {
|
||||
if (!type.isAssignableFrom(event.getClass())) {
|
||||
if (!filter.getEventType().isAssignableFrom(event.getClass())) {
|
||||
// Invalid event type
|
||||
return;
|
||||
}
|
||||
|
@ -6,15 +6,12 @@ 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> {
|
||||
|
||||
private final EventFilter<T, H> filter;
|
||||
public class EventNodeList<T extends Event, H extends EventHandler> extends EventNodeImpl<T, H> {
|
||||
|
||||
private final List<H> entries = new CopyOnWriteArrayList<>();
|
||||
|
||||
protected EventNodeList(EventFilter<T, H> filter) {
|
||||
super(filter.getEventType());
|
||||
this.filter = filter;
|
||||
super(filter);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user