mirror of https://github.com/Minestom/Minestom.git
Initial EventContext prototype
Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
parent
2720d9ada2
commit
b99fa8b5d1
|
@ -0,0 +1,19 @@
|
|||
package net.minestom.server.event;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public interface EventContext<E extends Event> {
|
||||
<T> @NotNull T filter(@NotNull EventFilter<? super E, T> filter);
|
||||
|
||||
void mutate(@NotNull E event);
|
||||
|
||||
@NotNull EventNode<? super E> currentNode();
|
||||
|
||||
int executionCount();
|
||||
|
||||
static <E extends Event> EventContext<E> filters(Map<EventFilter<? super E, ?>, ?> filters) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
}
|
|
@ -10,6 +10,7 @@ import org.jetbrains.annotations.Nullable;
|
|||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.BiPredicate;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Predicate;
|
||||
|
@ -358,4 +359,14 @@ public sealed interface EventNode<T extends Event> permits EventNodeImpl {
|
|||
|
||||
@ApiStatus.Experimental
|
||||
void unregister(@NotNull EventBinding<? extends T> binding);
|
||||
|
||||
default <E extends T> E call(@NotNull EventContext<E> context, @NotNull E event) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Contract(value = "_, _ -> this")
|
||||
default <E extends Event> @NotNull EventNode<T> addListener(@NotNull Class<E> eventType,
|
||||
@NotNull BiConsumer<EventContext<E>, E> listener) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,52 @@
|
|||
package net.minestom.server.event;
|
||||
|
||||
import net.minestom.server.coordinate.Pos;
|
||||
import net.minestom.server.event.trait.ItemEvent;
|
||||
import net.minestom.server.item.ItemStack;
|
||||
import net.minestom.server.item.Material;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class EventRecordTest {
|
||||
|
||||
record ActionAt(Pos position) implements ItemEvent {
|
||||
@Override
|
||||
public @NotNull ItemStack getItemStack() {
|
||||
// FIXME: Will be removed, as events will not be responsible for the filters anymore
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCall() {
|
||||
var node = EventNode.all("main");
|
||||
ItemStack item = ItemStack.of(Material.DIAMOND);
|
||||
var action = new ActionAt(Pos.ZERO);
|
||||
var mutated = new ActionAt(new Pos(1, 1, 1));
|
||||
node.addListener(ActionAt.class, (context, event) -> {
|
||||
assertEquals(action, event);
|
||||
|
||||
ItemStack filter = context.filter(EventFilter.ITEM);
|
||||
assertEquals(item, filter);
|
||||
assertEquals(0, context.executionCount());
|
||||
assertEquals(node, context.currentNode());
|
||||
context.mutate(mutated);
|
||||
});
|
||||
ActionAt result = node.call(EventContext.filters(Map.of(EventFilter.ITEM, item)), action);
|
||||
assertEquals(mutated, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void currentNode() {
|
||||
var node = EventNode.all("main");
|
||||
node.addListener(ActionAt.class, (context, event) -> {
|
||||
var current = context.currentNode();
|
||||
assertEquals(node, current);
|
||||
});
|
||||
node.call(EventContext.filters(Map.of()), new ActionAt(Pos.ZERO));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue