mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-06 16:37:38 +01:00
Added EventNodeConditional
This commit is contained in:
parent
4f7192b6c7
commit
c3f52031e6
@ -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,35 +8,37 @@ import java.util.concurrent.CopyOnWriteArrayList;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
public class EventNode<T extends Event> {
|
||||
public abstract class EventNode<T extends Event> {
|
||||
|
||||
private final String name = "debug";
|
||||
private final List<EventListener<T>> listeners = new CopyOnWriteArrayList<>();
|
||||
private final List<EventNode<T>> children = new CopyOnWriteArrayList<>();
|
||||
private final Predicate<T> condition = t -> true;
|
||||
|
||||
private EventNode() {
|
||||
}
|
||||
private static final EventNode<Event> EMPTY = new EventNode<>() {
|
||||
@Override
|
||||
protected boolean isValid(@NotNull Event event) {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
public static EventNode<Event> create() {
|
||||
return new EventNode<>();
|
||||
return EMPTY;
|
||||
}
|
||||
|
||||
public static <E extends Event> EventNode<E> conditional(@NotNull Class<E> type,
|
||||
@NotNull Predicate<E> predicate) {
|
||||
return new EventNode<>();
|
||||
return new EventNodeConditional<>(predicate);
|
||||
}
|
||||
|
||||
public static <E extends Event> EventNode<E> conditional(@NotNull Class<E> eventType) {
|
||||
return conditional(eventType, t -> true);
|
||||
}
|
||||
|
||||
public static <E extends Event> EventNode<E> unique(@NotNull Class<E> eventType,
|
||||
@NotNull EventHandler handler) {
|
||||
return new EventNode<>();
|
||||
}
|
||||
protected abstract boolean isValid(@NotNull T event);
|
||||
|
||||
public void call(@NotNull T event) {
|
||||
if (!isValid(event))
|
||||
return;
|
||||
this.listeners.forEach(eventListener -> eventListener.getCombined().accept(event));
|
||||
this.children.forEach(eventNode -> eventNode.call(event));
|
||||
}
|
||||
@ -58,11 +59,7 @@ public class EventNode<T extends Event> {
|
||||
return name;
|
||||
}
|
||||
|
||||
public @NotNull List<@NotNull EventNode<? extends T>> getChildren() {
|
||||
public @NotNull List<@NotNull EventNode<T>> getChildren() {
|
||||
return Collections.unmodifiableList(children);
|
||||
}
|
||||
|
||||
public @NotNull Predicate<@NotNull T> getCondition() {
|
||||
return condition;
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,27 @@
|
||||
package net.minestom.server.event;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.function.Predicate;
|
||||
|
||||
public class EventNodeConditional<T extends Event> extends EventNode<T> {
|
||||
|
||||
private volatile Predicate<T> predicate;
|
||||
|
||||
protected EventNodeConditional(Predicate<T> predicate) {
|
||||
this.predicate = predicate;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean isValid(@NotNull T event) {
|
||||
return predicate.test(event);
|
||||
}
|
||||
|
||||
public Predicate<T> getPredicate() {
|
||||
return predicate;
|
||||
}
|
||||
|
||||
public void setPredicate(Predicate<T> predicate) {
|
||||
this.predicate = predicate;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user