mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-08 17:37:42 +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.List;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
import java.util.function.Function;
|
|
||||||
import java.util.function.Predicate;
|
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, EventHandler> filter) {
|
static <E extends Event, H extends EventHandler> EventNode<E> type(@NotNull EventFilter<E, H> filter) {
|
||||||
return new EventNodeImpl<>(filter.getEventType());
|
return new EventNodeImpl<>(filter);
|
||||||
}
|
|
||||||
|
|
||||||
static <E extends Event> EventNode<E> type(@NotNull Class<E> type) {
|
|
||||||
return type(EventFilter.from(type));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static EventNode<Event> all() {
|
static EventNode<Event> all() {
|
||||||
return type(EventFilter.ALL);
|
return type(EventFilter.ALL);
|
||||||
}
|
}
|
||||||
|
|
||||||
static <E extends Event> EventNodeConditional<E> conditional(@NotNull Class<E> type,
|
static <E extends Event, H extends EventHandler> EventNodeConditional<E, H> conditional(@NotNull EventFilter<E, H> filter,
|
||||||
@NotNull Predicate<E> predicate) {
|
@NotNull Predicate<E> predicate) {
|
||||||
return new EventNodeConditional<>(type, predicate);
|
return new EventNodeConditional<>(filter, predicate);
|
||||||
}
|
}
|
||||||
|
|
||||||
static <E extends Event, H extends EventHandler> EventNodeList<E, H> list(@NotNull EventFilter<E, H> filter) {
|
static <E extends Event, H extends EventHandler> EventNodeList<E, H> list(@NotNull EventFilter<E, H> filter) {
|
||||||
return new EventNodeList<>(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 call(@NotNull T event);
|
||||||
|
|
||||||
void addChild(@NotNull EventNode<? extends T> child);
|
void addChild(@NotNull EventNode<? extends T> child);
|
||||||
|
@ -1,15 +1,16 @@
|
|||||||
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.Predicate;
|
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;
|
private volatile Predicate<T> predicate;
|
||||||
|
|
||||||
protected EventNodeConditional(Class<T> type, Predicate<T> predicate) {
|
protected EventNodeConditional(EventFilter<T, H> filter, Predicate<T> predicate) {
|
||||||
super(type);
|
super(filter);
|
||||||
this.predicate = predicate;
|
this.predicate = predicate;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
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;
|
||||||
@ -8,17 +9,17 @@ 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> implements EventNode<T> {
|
class EventNodeImpl<T extends Event, H extends EventHandler> implements EventNode<T> {
|
||||||
|
|
||||||
private final String name = "debug";
|
private final String name = "debug";
|
||||||
|
|
||||||
private final Map<Class<? extends T>, List<EventListener<T>>> listenerMap = new ConcurrentHashMap<>();
|
private final Map<Class<? extends T>, List<EventListener<T>>> listenerMap = new ConcurrentHashMap<>();
|
||||||
private final List<EventNode<T>> children = new CopyOnWriteArrayList<>();
|
private final List<EventNode<T>> children = new CopyOnWriteArrayList<>();
|
||||||
|
|
||||||
protected final Class<T> type;
|
protected final EventFilter<T, H> filter;
|
||||||
|
|
||||||
protected EventNodeImpl(Class<T> type) {
|
protected EventNodeImpl(EventFilter<T, H> filter) {
|
||||||
this.type = type;
|
this.filter = filter;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected boolean condition(@NotNull T event) {
|
protected boolean condition(@NotNull T event) {
|
||||||
@ -27,7 +28,7 @@ class EventNodeImpl<T extends Event> implements EventNode<T> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void call(@NotNull T event) {
|
public void call(@NotNull T event) {
|
||||||
if (!type.isAssignableFrom(event.getClass())) {
|
if (!filter.getEventType().isAssignableFrom(event.getClass())) {
|
||||||
// Invalid event type
|
// Invalid event type
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -6,15 +6,12 @@ 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> {
|
public class EventNodeList<T extends Event, H extends EventHandler> extends EventNodeImpl<T, H> {
|
||||||
|
|
||||||
private final EventFilter<T, H> filter;
|
|
||||||
|
|
||||||
private final List<H> entries = new CopyOnWriteArrayList<>();
|
private final List<H> entries = new CopyOnWriteArrayList<>();
|
||||||
|
|
||||||
protected EventNodeList(EventFilter<T, H> filter) {
|
protected EventNodeList(EventFilter<T, H> filter) {
|
||||||
super(filter.getEventType());
|
super(filter);
|
||||||
this.filter = filter;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@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.ItemDropEvent;
|
||||||
import net.minestom.server.event.item.PickupItemEvent;
|
import net.minestom.server.event.item.PickupItemEvent;
|
||||||
import net.minestom.server.event.player.*;
|
import net.minestom.server.event.player.*;
|
||||||
import net.minestom.server.event.trait.PlayerEvent;
|
|
||||||
import net.minestom.server.instance.Chunk;
|
import net.minestom.server.instance.Chunk;
|
||||||
import net.minestom.server.instance.Instance;
|
import net.minestom.server.instance.Instance;
|
||||||
import net.minestom.server.instance.InstanceContainer;
|
import net.minestom.server.instance.InstanceContainer;
|
||||||
@ -138,7 +137,7 @@ public class PlayerInit {
|
|||||||
|
|
||||||
// EVENT REGISTERING
|
// EVENT REGISTERING
|
||||||
|
|
||||||
var node = EventNode.type(PlayerEvent.class);
|
var node = EventNode.type(EventFilter.PLAYER);
|
||||||
node.addListener(EventListener.of(PlayerTickEvent.class)
|
node.addListener(EventListener.of(PlayerTickEvent.class)
|
||||||
.handler(playerTickEvent -> System.out.println("Player tick!"))
|
.handler(playerTickEvent -> System.out.println("Player tick!"))
|
||||||
.expirationCount(2)
|
.expirationCount(2)
|
||||||
|
Loading…
Reference in New Issue
Block a user