From 5e2739981531e236a10d39efc00e31e606f219c0 Mon Sep 17 00:00:00 2001 From: TheMode Date: Tue, 24 Aug 2021 11:49:44 +0200 Subject: [PATCH] Fix event propagation on self --- .../net/minestom/server/event/EventNode.java | 4 +- .../minestom/server/event/EventNodeImpl.java | 43 ++++++++----------- 2 files changed, 21 insertions(+), 26 deletions(-) diff --git a/src/main/java/net/minestom/server/event/EventNode.java b/src/main/java/net/minestom/server/event/EventNode.java index 96932a5bf..a89d64713 100644 --- a/src/main/java/net/minestom/server/event/EventNode.java +++ b/src/main/java/net/minestom/server/event/EventNode.java @@ -321,7 +321,9 @@ public interface EventNode { * * @param name The node name to filter for */ - void removeChildren(@NotNull String name); + default void removeChildren(@NotNull String name) { + removeChildren(name, getEventType()); + } /** * Directly adds a child node to this node. diff --git a/src/main/java/net/minestom/server/event/EventNodeImpl.java b/src/main/java/net/minestom/server/event/EventNodeImpl.java index ce1397512..e6179a0a7 100644 --- a/src/main/java/net/minestom/server/event/EventNodeImpl.java +++ b/src/main/java/net/minestom/server/event/EventNodeImpl.java @@ -114,11 +114,6 @@ class EventNodeImpl implements EventNode { } } - @Override - public void removeChildren(@NotNull String name) { - removeChildren(name, eventType); - } - @Override public @NotNull EventNode addChild(@NotNull EventNode child) { synchronized (GLOBAL_CHILD_LOCK) { @@ -127,7 +122,7 @@ class EventNodeImpl implements EventNode { Check.stateCondition(Objects.equals(parent, child), "Cannot have a child as parent"); if (!children.add((EventNodeImpl) childImpl)) return this; // Couldn't add the child (already present?) childImpl.parent = this; - childImpl.propagateEvents(this); + childImpl.invalidateEventsFor(this); } return this; } @@ -139,7 +134,7 @@ class EventNodeImpl implements EventNode { final boolean result = this.children.remove(childImpl); if (!result) return this; // Child not found childImpl.parent = null; - childImpl.propagateEvents(this); + childImpl.invalidateEventsFor(this); } return this; } @@ -150,7 +145,7 @@ class EventNodeImpl implements EventNode { final var eventType = listener.eventType(); ListenerEntry entry = getEntry(eventType); entry.listeners.add((EventListener) listener); - propagateEvent(parent, eventType); + invalidateEvent(eventType); } return this; } @@ -161,8 +156,7 @@ class EventNodeImpl implements EventNode { final var eventType = listener.eventType(); ListenerEntry entry = listenerMap.get(eventType); if (entry == null) return this; // There is no listener with such type - var listeners = entry.listeners; - if (listeners.remove(listener)) propagateEvent(parent, eventType); + if (entry.listeners.remove(listener)) invalidateEvent(eventType); } return this; } @@ -176,7 +170,7 @@ class EventNodeImpl implements EventNode { EventNodeImpl previous = this.mappedNodeCache.put(value, (EventNodeImpl) nodeImpl); if (previous != null) previous.parent = null; nodeImpl.parent = this; - nodeImpl.propagateEvents(this); + nodeImpl.invalidateEventsFor(this); } } @@ -187,7 +181,7 @@ class EventNodeImpl implements EventNode { if (mappedNode == null) return false; // Mapped node not found final var childImpl = (EventNodeImpl) mappedNode; childImpl.parent = null; - childImpl.propagateEvents(this); + childImpl.invalidateEventsFor(this); return true; } } @@ -198,7 +192,7 @@ class EventNodeImpl implements EventNode { for (var eventType : binding.eventTypes()) { ListenerEntry entry = getEntry((Class) eventType); final boolean added = entry.bindingConsumers.add((Consumer) binding.consumer(eventType)); - if (added) propagateEvent(parent, (Class) eventType); + if (added) invalidateEvent((Class) eventType); } } } @@ -210,7 +204,7 @@ class EventNodeImpl implements EventNode { ListenerEntry entry = listenerMap.get(eventType); if (entry == null) return; final boolean removed = entry.bindingConsumers.remove(binding.consumer(eventType)); - if (removed) propagateEvent(parent, (Class) eventType); + if (removed) invalidateEvent((Class) eventType); } } } @@ -241,18 +235,19 @@ class EventNodeImpl implements EventNode { return parent; } - private void propagateEvents(EventNodeImpl parent) { - this.listenerMap.keySet().forEach(aClass -> propagateEvent(parent, aClass)); + private void invalidateEventsFor(EventNodeImpl node) { + for (Class eventType : listenerMap.keySet()) { + node.invalidateEvent(eventType); + } } - private void propagateEvent(EventNodeImpl parent, Class eventClass) { - if (parent == null) return; + private void invalidateEvent(Class eventClass) { forTargetEvents(eventClass, type -> { - Handle parentHandle = (Handle) parent.handleMap.get(type); - if (parentHandle == null) return; - parentHandle.updated = false; - parent.propagateEvent(parent.parent, type); + Handle handle = handleMap.get(type); + if (handle != null) handle.updated = false; }); + final EventNodeImpl parent = this.parent; + if (parent != null) parent.invalidateEvent(eventClass); } private ListenerEntry getEntry(Class type) { @@ -260,9 +255,7 @@ class EventNodeImpl implements EventNode { } private static boolean equals(EventNode node, String name, Class eventType) { - final boolean nameCheck = node.getName().equals(name); - final boolean typeCheck = eventType.isAssignableFrom(((EventNodeImpl) node).eventType); - return nameCheck && typeCheck; + return node.getName().equals(name) && eventType.isAssignableFrom((node.getEventType())); } private static void forTargetEvents(Class type, Consumer> consumer) {