Class EventBus

java.lang.Object
net.clementraynaud.skoice.common.EventBus

public class EventBus extends Object
The event bus for subscribing to and handling Skoice events.

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 Details

    • subscribe

      public <T extends SkoiceEvent> EventHandler<T> subscribe(Class<T> eventClass, Consumer<T> handler)
      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 to
      handler - the consumer that will handle the event
      Returns:
      an EventHandler that 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 an EventCallback.

      This is a convenience method for subscribing with an EventCallback instead of a Consumer. Functionally equivalent to subscribe(Class, Consumer).

      Type Parameters:
      T - the event type
      Parameters:
      eventClass - the class of the event to subscribe to
      callback - the callback that will handle the event
      Returns:
      an EventHandler that can be used to unsubscribe later
      See Also:
    • fireAsync

      public <T extends SkoiceEvent> void fireAsync(T event)
      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

      public <T extends SkoiceEvent> boolean hasListeners(Class<T> eventClass)
      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