diff --git a/src/main/java/net/minestom/server/event/EventNode.java b/src/main/java/net/minestom/server/event/EventNode.java index 9cd4c8ae2..26a00a011 100644 --- a/src/main/java/net/minestom/server/event/EventNode.java +++ b/src/main/java/net/minestom/server/event/EventNode.java @@ -182,21 +182,36 @@ public interface EventNode { } /** - * Executes the given event on this node. The event must pass all conditions before - * it will be forwarded to the listeners. - *

- * Calling an event on a node will execute all child nodes, however, an event may be - * called anywhere on the event graph and it will propagate down from there only. + * Calls an event starting from this node. * - * @param event the event to execute + * @param event the event to call */ default void call(@NotNull T event) { + //noinspection unchecked call(event, getHandle((Class) event.getClass())); } - void call(@NotNull E event, ListenerHandle handle); + /** + * Calls an event starting from this node. + *

+ * The event handle can be retrieved using {@link #getHandle(Class)} + * and is useful to avoid map lookups for high-frequency events. + * + * @param event the event to call + * @param handle the event handle linked to this node + * @param the event type + * @throws IllegalArgumentException if {@param handle}'s owner is not {@code this} + */ + void call(@NotNull E event, @NotNull ListenerHandle handle); - ListenerHandle getHandle(Class handleType); + /** + * Gets the handle of an event type. + * + * @param handleType the handle type + * @param the event type + * @return the handle linked to {@param handleType} + */ + @NotNull ListenerHandle getHandle(@NotNull Class handleType); /** * Execute a cancellable event with a callback to execute if the event is successful. diff --git a/src/main/java/net/minestom/server/event/EventNodeImpl.java b/src/main/java/net/minestom/server/event/EventNodeImpl.java index e5fe1d960..09edc9f94 100644 --- a/src/main/java/net/minestom/server/event/EventNodeImpl.java +++ b/src/main/java/net/minestom/server/event/EventNodeImpl.java @@ -38,9 +38,9 @@ class EventNodeImpl implements EventNode { } @Override - public void call(@NotNull E event, ListenerHandle handle) { + public void call(@NotNull E event, @NotNull ListenerHandle handle) { var castedHandle = (Handle) handle; - Check.stateCondition(castedHandle.node != this, "Invalid handle owner"); + Check.argCondition(castedHandle.node != this, "Invalid handle owner"); List> listeners = castedHandle.listeners; if (!castedHandle.updated) { synchronized (GLOBAL_CHILD_LOCK) { @@ -56,7 +56,8 @@ class EventNodeImpl implements EventNode { } @Override - public ListenerHandle getHandle(Class handleType) { + public @NotNull ListenerHandle getHandle(@NotNull Class handleType) { + //noinspection unchecked return (ListenerHandle) handleMap.computeIfAbsent(handleType, aClass -> new Handle<>(this, (Class) aClass)); }