From 390c383e82e16b18ac312cc55d8f546d3cea2c3f Mon Sep 17 00:00:00 2001 From: TheMode Date: Tue, 17 Aug 2021 02:24:21 +0200 Subject: [PATCH] Rename EventInterface, add `unregister` --- ...{EventInterface.java => EventBinding.java} | 28 +++++++++++-------- .../net/minestom/server/event/EventNode.java | 4 ++- .../minestom/server/event/EventNodeImpl.java | 27 ++++++++++++------ 3 files changed, 37 insertions(+), 22 deletions(-) rename src/main/java/net/minestom/server/event/{EventInterface.java => EventBinding.java} (65%) diff --git a/src/main/java/net/minestom/server/event/EventInterface.java b/src/main/java/net/minestom/server/event/EventBinding.java similarity index 65% rename from src/main/java/net/minestom/server/event/EventInterface.java rename to src/main/java/net/minestom/server/event/EventBinding.java index 17fadc53d..543ced590 100644 --- a/src/main/java/net/minestom/server/event/EventInterface.java +++ b/src/main/java/net/minestom/server/event/EventBinding.java @@ -9,7 +9,7 @@ import java.util.function.BiConsumer; import java.util.function.Consumer; import java.util.function.Predicate; -public interface EventInterface { +public interface EventBinding { static @NotNull FilteredBuilder filtered(@NotNull EventFilter filter, @NotNull Predicate predicate) { return new FilteredBuilder<>(filter, predicate); @@ -17,8 +17,7 @@ public interface EventInterface { @NotNull Collection> eventTypes(); - Consumer consumer(Class eventType); - + @NotNull Consumer<@NotNull E> consumer(@NotNull Class eventType); class FilteredBuilder { private final EventFilter filter; @@ -37,23 +36,28 @@ public interface EventInterface { return this; } - public @NotNull EventInterface build() { + public @NotNull EventBinding build() { final var copy = Map.copyOf(mapped); final var eventTypes = copy.keySet(); - return new EventInterface<>() { + + Map, Consumer> consumers = new HashMap<>(eventTypes.size()); + for (var eventType : eventTypes) { + final var consumer = copy.get(eventType); + consumers.put(eventType, event -> { + final T handler = filter.getHandler(event); + if (!predicate.test(handler)) return; + consumer.accept(handler, event); + }); + } + return new EventBinding<>() { @Override public @NotNull Collection> eventTypes() { return eventTypes; } @Override - public Consumer consumer(Class eventType) { - final var consumer = copy.get(eventType); - return event -> { - final T handler = filter.getHandler(event); - if (!predicate.test(handler)) return; - consumer.accept(handler, event); - }; + public @NotNull Consumer consumer(@NotNull Class eventType) { + return consumers.get(eventType); } }; } diff --git a/src/main/java/net/minestom/server/event/EventNode.java b/src/main/java/net/minestom/server/event/EventNode.java index f867a6bd9..f39e65cc0 100644 --- a/src/main/java/net/minestom/server/event/EventNode.java +++ b/src/main/java/net/minestom/server/event/EventNode.java @@ -320,5 +320,7 @@ public interface EventNode { boolean unmap(@NotNull Object value); - void registerInterface(@NotNull EventInterface eventInterface); + void register(@NotNull EventBinding binding); + + void unregister(@NotNull EventBinding binding); } diff --git a/src/main/java/net/minestom/server/event/EventNodeImpl.java b/src/main/java/net/minestom/server/event/EventNodeImpl.java index 020d469ba..efb01f53a 100644 --- a/src/main/java/net/minestom/server/event/EventNodeImpl.java +++ b/src/main/java/net/minestom/server/event/EventNodeImpl.java @@ -221,13 +221,23 @@ class EventNodeImpl implements EventNode { } @Override - public void registerInterface(@NotNull EventInterface eventInterface) { + public void register(@NotNull EventBinding binding) { synchronized (GLOBAL_CHILD_LOCK) { - for (var eventType : eventInterface.eventTypes()) { + for (var eventType : binding.eventTypes()) { var entry = getEntry((Class) eventType); - final var consumer = eventInterface.consumer(eventType); - entry.interfaces.add((EventInterface) eventInterface); - entry.interfaceConsumers.add((Consumer) consumer); + final var consumer = binding.consumer(eventType); + entry.bindingConsumers.add((Consumer) consumer); + } + } + } + + @Override + public void unregister(@NotNull EventBinding binding) { + synchronized (GLOBAL_CHILD_LOCK) { + for (var eventType : binding.eventTypes()) { + var entry = listenerMap.get(eventType); + if (entry == null) return; + entry.bindingConsumers.remove(binding.consumer(eventType)); } } } @@ -304,8 +314,7 @@ class EventNodeImpl implements EventNode { final List> filters; final List> listeners = new CopyOnWriteArrayList<>(); - final Set> interfaces = new CopyOnWriteArraySet<>(); - final Set> interfaceConsumers = new CopyOnWriteArraySet<>(); + final Set> bindingConsumers = new CopyOnWriteArraySet<>(); final Map> mappedNode = new WeakHashMap<>(); volatile int childCount; @@ -315,8 +324,8 @@ class EventNodeImpl implements EventNode { void call(T event) { // Event interfaces - if (!interfaceConsumers.isEmpty()) { - for (var consumer : interfaceConsumers) { + if (!bindingConsumers.isEmpty()) { + for (var consumer : bindingConsumers) { consumer.accept(event); } }