From 523c9b512a1b2123bd658543fd73956877427625 Mon Sep 17 00:00:00 2001 From: TheMode Date: Tue, 17 Aug 2021 02:33:48 +0200 Subject: [PATCH] More cleanup --- .../net/minestom/server/event/EventNode.java | 4 ++- .../minestom/server/event/EventNodeImpl.java | 35 +++++++++---------- 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/src/main/java/net/minestom/server/event/EventNode.java b/src/main/java/net/minestom/server/event/EventNode.java index f39e65cc0..6eb4187bb 100644 --- a/src/main/java/net/minestom/server/event/EventNode.java +++ b/src/main/java/net/minestom/server/event/EventNode.java @@ -311,7 +311,9 @@ public interface EventNode { @NotNull EventNode addListener(@NotNull EventListener listener); @Contract(value = "_, _ -> this") - @NotNull EventNode addListener(@NotNull Class eventType, @NotNull Consumer<@NotNull E> listener); + default @NotNull EventNode addListener(@NotNull Class eventType, @NotNull Consumer<@NotNull E> listener) { + return addListener(EventListener.of(eventType, listener)); + } @Contract(value = "_ -> this") @NotNull EventNode removeListener(@NotNull EventListener listener); diff --git a/src/main/java/net/minestom/server/event/EventNodeImpl.java b/src/main/java/net/minestom/server/event/EventNodeImpl.java index efb01f53a..fc82a4a18 100644 --- a/src/main/java/net/minestom/server/event/EventNodeImpl.java +++ b/src/main/java/net/minestom/server/event/EventNodeImpl.java @@ -88,8 +88,8 @@ class EventNodeImpl implements EventNode { @Override public void replaceChildren(@NotNull String name, @NotNull Class eventType, @NotNull EventNode eventNode) { - if (children.isEmpty()) return; synchronized (GLOBAL_CHILD_LOCK) { + if (children.isEmpty()) return; for (EventNode child : children) { if (equals(child, name, eventType)) { removeChild(child); @@ -103,8 +103,8 @@ class EventNodeImpl implements EventNode { @Override public void removeChildren(@NotNull String name, @NotNull Class eventType) { - if (children.isEmpty()) return; synchronized (GLOBAL_CHILD_LOCK) { + if (children.isEmpty()) return; for (EventNode child : children) { if (equals(child, name, eventType)) { removeChild(child); @@ -166,11 +166,6 @@ class EventNodeImpl implements EventNode { return this; } - @Override - public @NotNull EventNode addListener(@NotNull Class eventType, @NotNull Consumer<@NotNull E> listener) { - return addListener(EventListener.of(eventType, listener)); - } - @Override public @NotNull EventNode removeListener(@NotNull EventListener listener) { synchronized (GLOBAL_CHILD_LOCK) { @@ -191,7 +186,6 @@ class EventNodeImpl implements EventNode { @Override public void map(@NotNull EventNode node, @NotNull Object value) { final var nodeImpl = (EventNodeImpl) node; - final var nodeType = nodeImpl.eventType; final var valueType = value.getClass(); synchronized (GLOBAL_CHILD_LOCK) { nodeImpl.listenerMap.forEach((type, listenerEntry) -> { @@ -200,7 +194,7 @@ class EventNodeImpl implements EventNode { final var handlerType = eventFilter.handlerType(); return handlerType != null && handlerType.isAssignableFrom(valueType); }); - Check.stateCondition(!correct, "The node filter {0} is not compatible with type {1}", nodeType, valueType); + Check.stateCondition(!correct, "The node filter {0} is not compatible with type {1}", nodeImpl.eventType, valueType); synchronized (mappedNodeCache) { entry.mappedNode.put(value, (EventNode) nodeImpl); mappedNodeCache.put(value, entry); @@ -225,8 +219,7 @@ class EventNodeImpl implements EventNode { synchronized (GLOBAL_CHILD_LOCK) { for (var eventType : binding.eventTypes()) { var entry = getEntry((Class) eventType); - final var consumer = binding.consumer(eventType); - entry.bindingConsumers.add((Consumer) consumer); + entry.bindingConsumers.add((Consumer) binding.consumer(eventType)); } } } @@ -294,7 +287,7 @@ class EventNodeImpl implements EventNode { } private ListenerEntry getEntry(Class type) { - return listenerMap.computeIfAbsent(type, aClass -> new ListenerEntry<>((Class) aClass)); + return listenerMap.computeIfAbsent(type, aClass -> new ListenerEntry<>(this, (Class) aClass)); } private static boolean equals(EventNode node, String name, Class eventType) { @@ -312,13 +305,15 @@ class EventNodeImpl implements EventNode { private static final AtomicIntegerFieldUpdater CHILD_UPDATER = AtomicIntegerFieldUpdater.newUpdater(ListenerEntry.class, "childCount"); + final EventNodeImpl node; final List> filters; final List> listeners = new CopyOnWriteArrayList<>(); final Set> bindingConsumers = new CopyOnWriteArraySet<>(); final Map> mappedNode = new WeakHashMap<>(); volatile int childCount; - ListenerEntry(Class eventType) { + ListenerEntry(EventNodeImpl node, Class eventType) { + this.node = node; this.filters = FILTERS.stream().filter(eventFilter -> eventFilter.eventType().isAssignableFrom(eventType)).collect(Collectors.toList()); } @@ -331,11 +326,15 @@ class EventNodeImpl implements EventNode { } // Mapped listeners if (!mappedNode.isEmpty()) { - // Check mapped listeners for each individual event handler - for (var filter : filters) { - final var handler = filter.castHandler(event); - final var map = mappedNode.get(handler); - if (map != null) map.call(event); + synchronized (node.mappedNodeCache) { + if (!mappedNode.isEmpty()) { + // Check mapped listeners for each individual event handler + for (var filter : filters) { + final var handler = filter.castHandler(event); + final var map = mappedNode.get(handler); + if (map != null) map.call(event); + } + } } } // Basic listeners