mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-13 19:51: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);
|
||||
if (handle != null) handle.call(event);
|
||||
};
|
||||
if (filterList.length == 1) {
|
||||
final var firstFilter = filterList[0];
|
||||
// Common case where there is only one filter
|
||||
return event -> mapper.accept(firstFilter, event);
|
||||
} else if (filterList.length == 2) {
|
||||
final var firstFilter = filterList[0];
|
||||
final var secondFilter = filterList[1];
|
||||
return event -> {
|
||||
mapper.accept(firstFilter, event);
|
||||
mapper.accept(secondFilter, event);
|
||||
// Specialize the consumer depending on the number of filters to avoid looping
|
||||
return switch (filterList.length) {
|
||||
case 1 -> event -> mapper.accept(filterList[0], event);
|
||||
case 2 -> event -> {
|
||||
mapper.accept(filterList[0], event);
|
||||
mapper.accept(filterList[1], event);
|
||||
};
|
||||
} else {
|
||||
return event -> {
|
||||
case 3 -> event -> {
|
||||
mapper.accept(filterList[0], event);
|
||||
mapper.accept(filterList[1], event);
|
||||
mapper.accept(filterList[2], event);
|
||||
};
|
||||
default -> event -> {
|
||||
for (var filter : filterList) {
|
||||
mapper.accept(filter, event);
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
void callListener(@NotNull EventListener<E> listener, E event) {
|
||||
|
@ -190,4 +190,34 @@ public class EventNodeTest {
|
||||
node.call(new ItemTestEvent(ItemStack.of(Material.DIAMOND)));
|
||||
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