mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-02 22:47:49 +01:00
Fix event propagation on self
This commit is contained in:
parent
27a54af64d
commit
5e27399815
@ -321,7 +321,9 @@ public interface EventNode<T extends Event> {
|
|||||||
*
|
*
|
||||||
* @param name The node name to filter for
|
* @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.
|
* Directly adds a child node to this node.
|
||||||
|
@ -114,11 +114,6 @@ class EventNodeImpl<T extends Event> implements EventNode<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void removeChildren(@NotNull String name) {
|
|
||||||
removeChildren(name, eventType);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public @NotNull EventNode<T> addChild(@NotNull EventNode<? extends T> child) {
|
public @NotNull EventNode<T> addChild(@NotNull EventNode<? extends T> child) {
|
||||||
synchronized (GLOBAL_CHILD_LOCK) {
|
synchronized (GLOBAL_CHILD_LOCK) {
|
||||||
@ -127,7 +122,7 @@ class EventNodeImpl<T extends Event> implements EventNode<T> {
|
|||||||
Check.stateCondition(Objects.equals(parent, child), "Cannot have a child as parent");
|
Check.stateCondition(Objects.equals(parent, child), "Cannot have a child as parent");
|
||||||
if (!children.add((EventNodeImpl<T>) childImpl)) return this; // Couldn't add the child (already present?)
|
if (!children.add((EventNodeImpl<T>) childImpl)) return this; // Couldn't add the child (already present?)
|
||||||
childImpl.parent = this;
|
childImpl.parent = this;
|
||||||
childImpl.propagateEvents(this);
|
childImpl.invalidateEventsFor(this);
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@ -139,7 +134,7 @@ class EventNodeImpl<T extends Event> implements EventNode<T> {
|
|||||||
final boolean result = this.children.remove(childImpl);
|
final boolean result = this.children.remove(childImpl);
|
||||||
if (!result) return this; // Child not found
|
if (!result) return this; // Child not found
|
||||||
childImpl.parent = null;
|
childImpl.parent = null;
|
||||||
childImpl.propagateEvents(this);
|
childImpl.invalidateEventsFor(this);
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@ -150,7 +145,7 @@ class EventNodeImpl<T extends Event> implements EventNode<T> {
|
|||||||
final var eventType = listener.eventType();
|
final var eventType = listener.eventType();
|
||||||
ListenerEntry<T> entry = getEntry(eventType);
|
ListenerEntry<T> entry = getEntry(eventType);
|
||||||
entry.listeners.add((EventListener<T>) listener);
|
entry.listeners.add((EventListener<T>) listener);
|
||||||
propagateEvent(parent, eventType);
|
invalidateEvent(eventType);
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@ -161,8 +156,7 @@ class EventNodeImpl<T extends Event> implements EventNode<T> {
|
|||||||
final var eventType = listener.eventType();
|
final var eventType = listener.eventType();
|
||||||
ListenerEntry<T> entry = listenerMap.get(eventType);
|
ListenerEntry<T> entry = listenerMap.get(eventType);
|
||||||
if (entry == null) return this; // There is no listener with such type
|
if (entry == null) return this; // There is no listener with such type
|
||||||
var listeners = entry.listeners;
|
if (entry.listeners.remove(listener)) invalidateEvent(eventType);
|
||||||
if (listeners.remove(listener)) propagateEvent(parent, eventType);
|
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@ -176,7 +170,7 @@ class EventNodeImpl<T extends Event> implements EventNode<T> {
|
|||||||
EventNodeImpl<T> previous = this.mappedNodeCache.put(value, (EventNodeImpl<T>) nodeImpl);
|
EventNodeImpl<T> previous = this.mappedNodeCache.put(value, (EventNodeImpl<T>) nodeImpl);
|
||||||
if (previous != null) previous.parent = null;
|
if (previous != null) previous.parent = null;
|
||||||
nodeImpl.parent = this;
|
nodeImpl.parent = this;
|
||||||
nodeImpl.propagateEvents(this);
|
nodeImpl.invalidateEventsFor(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -187,7 +181,7 @@ class EventNodeImpl<T extends Event> implements EventNode<T> {
|
|||||||
if (mappedNode == null) return false; // Mapped node not found
|
if (mappedNode == null) return false; // Mapped node not found
|
||||||
final var childImpl = (EventNodeImpl<? extends T>) mappedNode;
|
final var childImpl = (EventNodeImpl<? extends T>) mappedNode;
|
||||||
childImpl.parent = null;
|
childImpl.parent = null;
|
||||||
childImpl.propagateEvents(this);
|
childImpl.invalidateEventsFor(this);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -198,7 +192,7 @@ class EventNodeImpl<T extends Event> implements EventNode<T> {
|
|||||||
for (var eventType : binding.eventTypes()) {
|
for (var eventType : binding.eventTypes()) {
|
||||||
ListenerEntry<T> entry = getEntry((Class<? extends T>) eventType);
|
ListenerEntry<T> entry = getEntry((Class<? extends T>) eventType);
|
||||||
final boolean added = entry.bindingConsumers.add((Consumer<T>) binding.consumer(eventType));
|
final boolean added = entry.bindingConsumers.add((Consumer<T>) binding.consumer(eventType));
|
||||||
if (added) propagateEvent(parent, (Class<? extends T>) eventType);
|
if (added) invalidateEvent((Class<? extends T>) eventType);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -210,7 +204,7 @@ class EventNodeImpl<T extends Event> implements EventNode<T> {
|
|||||||
ListenerEntry<T> entry = listenerMap.get(eventType);
|
ListenerEntry<T> entry = listenerMap.get(eventType);
|
||||||
if (entry == null) return;
|
if (entry == null) return;
|
||||||
final boolean removed = entry.bindingConsumers.remove(binding.consumer(eventType));
|
final boolean removed = entry.bindingConsumers.remove(binding.consumer(eventType));
|
||||||
if (removed) propagateEvent(parent, (Class<? extends T>) eventType);
|
if (removed) invalidateEvent((Class<? extends T>) eventType);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -241,18 +235,19 @@ class EventNodeImpl<T extends Event> implements EventNode<T> {
|
|||||||
return parent;
|
return parent;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void propagateEvents(EventNodeImpl<? super T> parent) {
|
private void invalidateEventsFor(EventNodeImpl<? super T> node) {
|
||||||
this.listenerMap.keySet().forEach(aClass -> propagateEvent(parent, aClass));
|
for (Class<? extends T> eventType : listenerMap.keySet()) {
|
||||||
|
node.invalidateEvent(eventType);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void propagateEvent(EventNodeImpl parent, Class<? extends T> eventClass) {
|
private void invalidateEvent(Class<? extends T> eventClass) {
|
||||||
if (parent == null) return;
|
|
||||||
forTargetEvents(eventClass, type -> {
|
forTargetEvents(eventClass, type -> {
|
||||||
Handle<? super T> parentHandle = (Handle<? super T>) parent.handleMap.get(type);
|
Handle<? super T> handle = handleMap.get(type);
|
||||||
if (parentHandle == null) return;
|
if (handle != null) handle.updated = false;
|
||||||
parentHandle.updated = false;
|
|
||||||
parent.propagateEvent(parent.parent, type);
|
|
||||||
});
|
});
|
||||||
|
final EventNodeImpl<? super T> parent = this.parent;
|
||||||
|
if (parent != null) parent.invalidateEvent(eventClass);
|
||||||
}
|
}
|
||||||
|
|
||||||
private ListenerEntry<T> getEntry(Class<? extends T> type) {
|
private ListenerEntry<T> getEntry(Class<? extends T> type) {
|
||||||
@ -260,9 +255,7 @@ class EventNodeImpl<T extends Event> implements EventNode<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static boolean equals(EventNode<?> node, String name, Class<?> eventType) {
|
private static boolean equals(EventNode<?> node, String name, Class<?> eventType) {
|
||||||
final boolean nameCheck = node.getName().equals(name);
|
return node.getName().equals(name) && eventType.isAssignableFrom((node.getEventType()));
|
||||||
final boolean typeCheck = eventType.isAssignableFrom(((EventNodeImpl<?>) node).eventType);
|
|
||||||
return nameCheck && typeCheck;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void forTargetEvents(Class<?> type, Consumer<Class<?>> consumer) {
|
private static void forTargetEvents(Class<?> type, Consumer<Class<?>> consumer) {
|
||||||
|
Loading…
Reference in New Issue
Block a user