Package dev.polv.taleapi.event
Class Event<T>
java.lang.Object
dev.polv.taleapi.event.Event<T>
- Type Parameters:
T- the callback functional interface type
A type-safe event holder that manages listener registration and invocation.
Each event type should have its own static Event<T> instance.
Listeners are functional interfaces that get combined into a single invoker.
Example Usage
// Define an event callback interface
@FunctionalInterface
public interface MyCallback {
EventResult onSomething(String data);
Event<MyCallback> EVENT = Event.create(callbacks -> data -> {
for (var callback : callbacks) {
EventResult result = callback.onSomething(data);
if (result.shouldStop())
return result;
}
return EventResult.PASS;
});
}
// Register a listener
MyCallback.EVENT.register(data -> {
System.out.println("Received: " + data);
return EventResult.PASS;
});
// Fire the event
EventResult result = MyCallback.EVENT.invoker().onSomething("hello");
-
Method Summary
Modifier and TypeMethodDescriptionvoidRemoves all registered listeners.static <T> Event<T>Creates a new Event with priority support.Returns a snapshot of all registered listeners in priority order (HIGHEST to LOWEST).<R> CompletableFuture<R>invokeAsync(Executor executor, Function<T, R> action) Fires the event asynchronously using the provided executor.static <T> CompletableFuture<EventResult>invokeAsync(Iterator<T> iterator, Function<T, CompletableFuture<EventResult>> invoker) Helper to chain async callback processing in priority order.invoker()Returns the combined invoker for all registered listeners.intvoidregister(EventPriority priority, T listener) Registers a listener with the specified priority.voidRegisters a listener withEventPriority.NORMALpriority.booleanunregister(T listener) Unregisters a listener from all priority levels.
-
Method Details
-
create
Creates a new Event with priority support.- Type Parameters:
T- the callback type- Parameters:
invokerFactory- function that combines a list of listeners into a single invokeremptyInvoker- the invoker to return when no listeners are registered- Returns:
- a new Event instance
-
invokeAsync
public static <T> CompletableFuture<EventResult> invokeAsync(Iterator<T> iterator, Function<T, CompletableFuture<EventResult>> invoker) Helper to chain async callback processing in priority order.This processes callbacks sequentially, awaiting async results before proceeding to the next callback. Stops early if any callback returns a result where
EventResult.shouldStop()is true.Example Usage:
Event<PlayerJoinCallback> EVENT = Event.create( callbacks -> player -> Event.invokeAsync( callbacks.iterator(), callback -> callback.onPlayerJoin(player)), player -> EventResult.pass());- Type Parameters:
T- the callback type- Parameters:
iterator- iterator over the callbacks to invokeinvoker- function that invokes a callback and returns a future result- Returns:
- a CompletableFuture that completes with the final EventResult
-
register
Registers a listener withEventPriority.NORMALpriority.- Parameters:
listener- the listener to register- Throws:
NullPointerException- if listener is null
-
register
Registers a listener with the specified priority.- Parameters:
priority- the execution prioritylistener- the listener to register- Throws:
NullPointerException- if priority or listener is null
-
unregister
Unregisters a listener from all priority levels.- Parameters:
listener- the listener to remove- Returns:
trueif the listener was found and removed
-
invoker
Returns the combined invoker for all registered listeners.The invoker executes listeners in priority order (HIGHEST to LOWEST).
- Returns:
- the invoker that will call all registered listeners
-
invokeAsync
Fires the event asynchronously using the provided executor.This is a convenience method for async event handling. The returned CompletableFuture completes with the invoker result.
- Type Parameters:
R- the return type of the event callback- Parameters:
executor- the executor to run the event onaction- a function that calls the invoker and returns its result- Returns:
- a CompletableFuture that completes with the event result
-
listenerCount
public int listenerCount()- Returns:
- the number of registered listeners across all priorities
-
getListeners
Returns a snapshot of all registered listeners in priority order (HIGHEST to LOWEST).This is useful for async event implementations that need direct access to callbacks.
- Returns:
- an unmodifiable list of listeners
-
clearListeners
public void clearListeners()Removes all registered listeners.
-