Class EventBus
The event bus provides a simple publish-subscribe pattern for listening to various Skoice events. All event handlers are executed asynchronously on a dedicated event thread.
Access the event bus instance through Skoice.eventBus().
Example usage:
EventBus eventBus = Skoice.eventBus();
// Subscribe to player connecting to proximity chat
EventHandler<PlayerProximityConnectEvent> handler = eventBus.subscribe(
PlayerProximityConnectEvent.class,
event -> {
UUID playerId = event.getMinecraftId();
String discordId = event.getDiscordId();
// Handle the event
}
);
// Later, unsubscribe if needed
handler.unsubscribe();
- See Also:
-
Method Summary
Modifier and TypeMethodDescription<T extends SkoiceEvent>
voidfireAsync(T event) Fires an event asynchronously to all registered listeners.<T extends SkoiceEvent>
booleanhasListeners(Class<T> eventClass) Checks if any listeners are subscribed to a specific event type.<T extends SkoiceEvent>
EventHandler<T> Subscribes to a specific event type with a handler function.<T extends SkoiceEvent>
EventHandler<T> subscribeCallback(Class<T> eventClass, EventCallback<T> callback) Subscribes to a specific event type with anEventCallback.
-
Method Details
-
subscribe
Subscribes to a specific event type with a handler function.The handler will be invoked asynchronously whenever an event of the specified type is fired. Multiple handlers can be subscribed to the same event type.
Example:
eventBus.subscribe(PlayerProximityConnectEvent.class, event -> { System.out.println("Player " + event.getMinecraftId() + " connected!"); });- Type Parameters:
T- the event type- Parameters:
eventClass- the class of the event to subscribe tohandler- the consumer that will handle the event- Returns:
- an
EventHandlerthat can be used to unsubscribe later
-
subscribeCallback
public <T extends SkoiceEvent> EventHandler<T> subscribeCallback(Class<T> eventClass, EventCallback<T> callback) Subscribes to a specific event type with anEventCallback.This is a convenience method for subscribing with an
EventCallbackinstead of aConsumer. Functionally equivalent tosubscribe(Class, Consumer).- Type Parameters:
T- the event type- Parameters:
eventClass- the class of the event to subscribe tocallback- the callback that will handle the event- Returns:
- an
EventHandlerthat can be used to unsubscribe later - See Also:
-
fireAsync
Fires an event asynchronously to all registered listeners.This method is primarily for internal use by Skoice. All registered handlers for the event type will be invoked asynchronously on the event thread.
If any handler throws an exception, it will be caught and printed, but will not prevent other handlers from executing.
- Type Parameters:
T- the event type- Parameters:
event- the event to fire
-
hasListeners
Checks if any listeners are subscribed to a specific event type.- Type Parameters:
T- the event type- Parameters:
eventClass- the event class to check- Returns:
- true if at least one listener is subscribed to this event type, false otherwise
-