From 4f7192b6c7653bad16cda03e2b84830ca8c84bfa Mon Sep 17 00:00:00 2001 From: TheMode Date: Wed, 2 Jun 2021 10:34:01 +0200 Subject: [PATCH] Can now call an event from anywhere in the tree --- .../minestom/server/event/EventListener.java | 28 ++++--------------- .../net/minestom/server/event/EventNode.java | 13 ++++++--- .../minestom/server/event/ListenerAttach.java | 10 ------- 3 files changed, 14 insertions(+), 37 deletions(-) delete mode 100644 src/main/java/net/minestom/server/event/ListenerAttach.java diff --git a/src/main/java/net/minestom/server/event/EventListener.java b/src/main/java/net/minestom/server/event/EventListener.java index 2c5379802..24cf4b2cf 100644 --- a/src/main/java/net/minestom/server/event/EventListener.java +++ b/src/main/java/net/minestom/server/event/EventListener.java @@ -1,23 +1,17 @@ package net.minestom.server.event; -import net.minestom.server.event.handler.EventHandler; import org.jetbrains.annotations.NotNull; import java.util.ArrayList; import java.util.List; -import java.util.Set; -import java.util.concurrent.CopyOnWriteArraySet; import java.util.function.Consumer; import java.util.function.Predicate; -public class EventListener implements ListenerAttach { +public class EventListener { - private final Class eventType; - private final Set attach = new CopyOnWriteArraySet<>(); private final Consumer combined; - private EventListener(Class eventType, @NotNull Consumer combined) { - this.eventType = eventType; + private EventListener(@NotNull Consumer combined) { this.combined = combined; } @@ -25,20 +19,8 @@ public class EventListener implements ListenerAttach { return new EventListener.Builder<>(eventType); } - @Override - public void attachTo(@NotNull EventHandler handler) { - final boolean success = this.attach.add(handler); - if (success) { - handler.addEventCallback(eventType, combined::accept); - } - } - - @Override - public void detachFrom(@NotNull EventHandler handler) { - final boolean success = this.attach.remove(handler); - if (success) { - handler.removeEventCallback(eventType, combined::accept); - } + protected Consumer getCombined() { + return combined; } public static class Builder { @@ -63,7 +45,7 @@ public class EventListener implements ListenerAttach { } public EventListener build() { - return new EventListener<>(eventType, event -> { + return new EventListener<>(event -> { // Filtering if (!filters.isEmpty()) { if (filters.stream().anyMatch(filter -> !filter.test(event))) { diff --git a/src/main/java/net/minestom/server/event/EventNode.java b/src/main/java/net/minestom/server/event/EventNode.java index 674c7f6b0..4b177ca20 100644 --- a/src/main/java/net/minestom/server/event/EventNode.java +++ b/src/main/java/net/minestom/server/event/EventNode.java @@ -12,8 +12,8 @@ import java.util.function.Predicate; public class EventNode { private final String name = "debug"; - private final List> listeners = new CopyOnWriteArrayList<>(); - private final List> children = new CopyOnWriteArrayList<>(); + private final List> listeners = new CopyOnWriteArrayList<>(); + private final List> children = new CopyOnWriteArrayList<>(); private final Predicate condition = t -> true; private EventNode() { @@ -37,12 +37,17 @@ public class EventNode { return new EventNode<>(); } + public void call(@NotNull T event) { + this.listeners.forEach(eventListener -> eventListener.getCombined().accept(event)); + this.children.forEach(eventNode -> eventNode.call(event)); + } + public void addChild(@NotNull EventNode child) { - this.children.add(child); + this.children.add((EventNode) child); } public void addListener(@NotNull EventListener listener) { - this.listeners.add(listener); + this.listeners.add((EventListener) listener); } public void addListener(@NotNull Class eventClass, @NotNull Consumer<@NotNull E> listener) { diff --git a/src/main/java/net/minestom/server/event/ListenerAttach.java b/src/main/java/net/minestom/server/event/ListenerAttach.java deleted file mode 100644 index 7f3122db4..000000000 --- a/src/main/java/net/minestom/server/event/ListenerAttach.java +++ /dev/null @@ -1,10 +0,0 @@ -package net.minestom.server.event; - -import net.minestom.server.event.handler.EventHandler; -import org.jetbrains.annotations.NotNull; - -public interface ListenerAttach { - void attachTo(@NotNull EventHandler handler); - - void detachFrom(@NotNull EventHandler handler); -}