From c3f52031e610ae8cfdb2855577098c48301169b9 Mon Sep 17 00:00:00 2001 From: TheMode Date: Wed, 2 Jun 2021 10:52:20 +0200 Subject: [PATCH] Added EventNodeConditional --- .../net/minestom/server/event/EventNode.java | 29 +++++++++---------- .../server/event/EventNodeConditional.java | 27 +++++++++++++++++ 2 files changed, 40 insertions(+), 16 deletions(-) create mode 100644 src/main/java/net/minestom/server/event/EventNodeConditional.java diff --git a/src/main/java/net/minestom/server/event/EventNode.java b/src/main/java/net/minestom/server/event/EventNode.java index 4b177ca20..09c4462bc 100644 --- a/src/main/java/net/minestom/server/event/EventNode.java +++ b/src/main/java/net/minestom/server/event/EventNode.java @@ -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 { +public abstract class EventNode { private final String name = "debug"; private final List> listeners = new CopyOnWriteArrayList<>(); private final List> children = new CopyOnWriteArrayList<>(); - private final Predicate condition = t -> true; - private EventNode() { - } + private static final EventNode EMPTY = new EventNode<>() { + @Override + protected boolean isValid(@NotNull Event event) { + return true; + } + }; public static EventNode create() { - return new EventNode<>(); + return EMPTY; } public static EventNode conditional(@NotNull Class type, @NotNull Predicate predicate) { - return new EventNode<>(); + return new EventNodeConditional<>(predicate); } public static EventNode conditional(@NotNull Class eventType) { return conditional(eventType, t -> true); } - public static EventNode unique(@NotNull Class 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 { return name; } - public @NotNull List<@NotNull EventNode> getChildren() { + public @NotNull List<@NotNull EventNode> getChildren() { return Collections.unmodifiableList(children); } - - public @NotNull Predicate<@NotNull T> getCondition() { - return condition; - } } diff --git a/src/main/java/net/minestom/server/event/EventNodeConditional.java b/src/main/java/net/minestom/server/event/EventNodeConditional.java new file mode 100644 index 000000000..ec37e4eec --- /dev/null +++ b/src/main/java/net/minestom/server/event/EventNodeConditional.java @@ -0,0 +1,27 @@ +package net.minestom.server.event; + +import org.jetbrains.annotations.NotNull; + +import java.util.function.Predicate; + +public class EventNodeConditional extends EventNode { + + private volatile Predicate predicate; + + protected EventNodeConditional(Predicate predicate) { + this.predicate = predicate; + } + + @Override + protected boolean isValid(@NotNull T event) { + return predicate.test(event); + } + + public Predicate getPredicate() { + return predicate; + } + + public void setPredicate(Predicate predicate) { + this.predicate = predicate; + } +}