mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-28 19:11:45 +01:00
Fix EventNode bug where listeners aren't firing (#2529)
* add new test for RecursiveEvents * fix recursive event calling * implement by looping
This commit is contained in:
parent
c1a3ee6aca
commit
0d47d97417
@ -292,10 +292,21 @@ non-sealed class EventNodeImpl<T extends Event> implements EventNode<T> {
|
||||
aClass -> new Handle<>((Class<T>) aClass));
|
||||
handle.invalidate();
|
||||
});
|
||||
invalidateRecursiveSuperclasses(eventClass);
|
||||
final EventNodeImpl<? super T> parent = this.parent;
|
||||
if (parent != null) parent.invalidateEvent(eventClass);
|
||||
}
|
||||
|
||||
private void invalidateRecursiveSuperclasses(@NotNull Class<?> eventClass) {
|
||||
if (RecursiveEvent.class.isAssignableFrom(eventClass)) {
|
||||
for (var cls : this.handleMap.keySet()) {
|
||||
if (eventClass.isAssignableFrom(cls)) {
|
||||
this.handleMap.get(cls).invalidate();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private ListenerEntry<T> getEntry(Class<? extends T> type) {
|
||||
return listenerMap.computeIfAbsent(type, aClass -> new ListenerEntry<>());
|
||||
}
|
||||
|
@ -124,6 +124,33 @@ public class EventNodeTest {
|
||||
assertTrue(result1.get(), "Recursive1 should be called due to the RecursiveEvent interface");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRecursiveChild() {
|
||||
var called1 = new AtomicBoolean(false);
|
||||
var called2 = new AtomicBoolean(false);
|
||||
var child1 = EventNode.all("child1");
|
||||
var child2 = EventNode.all("child2");
|
||||
child1.addListener(Recursive1.class, event -> called1.set(true));
|
||||
child2.addListener(Recursive1.class, event -> called2.set(true));
|
||||
|
||||
var node = EventNode.all("main");
|
||||
node.addChild(child1);
|
||||
|
||||
node.call(new Recursive2());
|
||||
|
||||
assertTrue(called1.get());
|
||||
assertFalse(called2.get());
|
||||
called1.set(false);
|
||||
|
||||
node.removeChild(child1);
|
||||
node.addChild(child2);
|
||||
|
||||
node.call(new Recursive2());
|
||||
|
||||
assertFalse(called1.get());
|
||||
assertTrue(called2.get());
|
||||
}
|
||||
|
||||
// FIXME: nodes are currently unable to retrieve sub handles
|
||||
//@Test
|
||||
//public void recursiveSuper() {
|
||||
|
Loading…
Reference in New Issue
Block a user