mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-14 12:11:27 +01:00
Add test for node mapping
This commit is contained in:
parent
f1a4f002c3
commit
68f19e3dae
@ -409,24 +409,24 @@ non-sealed class EventNodeImpl<T extends Event> implements EventNode<T> {
|
|||||||
final Handle<E> handle = handlers.get(handler);
|
final Handle<E> handle = handlers.get(handler);
|
||||||
if (handle != null) handle.call(event);
|
if (handle != null) handle.call(event);
|
||||||
};
|
};
|
||||||
if (filterList.length == 1) {
|
// Specialize the consumer depending on the number of filters to avoid looping
|
||||||
final var firstFilter = filterList[0];
|
return switch (filterList.length) {
|
||||||
// Common case where there is only one filter
|
case 1 -> event -> mapper.accept(filterList[0], event);
|
||||||
return event -> mapper.accept(firstFilter, event);
|
case 2 -> event -> {
|
||||||
} else if (filterList.length == 2) {
|
mapper.accept(filterList[0], event);
|
||||||
final var firstFilter = filterList[0];
|
mapper.accept(filterList[1], event);
|
||||||
final var secondFilter = filterList[1];
|
|
||||||
return event -> {
|
|
||||||
mapper.accept(firstFilter, event);
|
|
||||||
mapper.accept(secondFilter, event);
|
|
||||||
};
|
};
|
||||||
} else {
|
case 3 -> event -> {
|
||||||
return event -> {
|
mapper.accept(filterList[0], event);
|
||||||
|
mapper.accept(filterList[1], event);
|
||||||
|
mapper.accept(filterList[2], event);
|
||||||
|
};
|
||||||
|
default -> event -> {
|
||||||
for (var filter : filterList) {
|
for (var filter : filterList) {
|
||||||
mapper.accept(filter, event);
|
mapper.accept(filter, event);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
void callListener(@NotNull EventListener<E> listener, E event) {
|
void callListener(@NotNull EventListener<E> listener, E event) {
|
||||||
|
@ -190,4 +190,34 @@ public class EventNodeTest {
|
|||||||
node.call(new ItemTestEvent(ItemStack.of(Material.DIAMOND)));
|
node.call(new ItemTestEvent(ItemStack.of(Material.DIAMOND)));
|
||||||
assertFalse(result.get());
|
assertFalse(result.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testMap() {
|
||||||
|
record ItemTestEvent(ItemStack item) implements ItemEvent {
|
||||||
|
@Override
|
||||||
|
public @NotNull ItemStack getItemStack() {
|
||||||
|
return item;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var item = ItemStack.of(Material.DIAMOND);
|
||||||
|
var node = EventNode.all("main");
|
||||||
|
|
||||||
|
AtomicBoolean result = new AtomicBoolean(false);
|
||||||
|
var itemNode = EventNode.type("item_node", EventFilter.ITEM);
|
||||||
|
itemNode.addListener(ItemTestEvent.class, event -> result.set(true));
|
||||||
|
assertDoesNotThrow(() -> node.map(itemNode, item));
|
||||||
|
|
||||||
|
node.call(new ItemTestEvent(item));
|
||||||
|
assertTrue(result.get());
|
||||||
|
|
||||||
|
result.set(false);
|
||||||
|
node.call(new ItemTestEvent(ItemStack.of(Material.GOLD_INGOT)));
|
||||||
|
assertFalse(result.get());
|
||||||
|
|
||||||
|
result.set(false);
|
||||||
|
node.unmap(item);
|
||||||
|
node.call(new ItemTestEvent(item));
|
||||||
|
assertFalse(result.get());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user