Platform: EventManager - Dani-error/velar GitHub Wiki

🧠 EventManager

Source Code


πŸ“˜ What is NPCEventManager?

NPCEventManager is the core system for handling all velar NPC-related events.
It allows you to post, register, and unsubscribe event handlers with priority ordering and built-in error handling.


πŸ”§ Interface Methods

Method Description
<E : NPCEvent> post(event: E): E Dispatches an event to all registered handlers.
<E : NPCEvent> registerEventHandler(eventType, consumer) Registers a handler with default priority.
<E : NPCEvent> registerEventHandler(eventType, consumer, priority) Registers with explicit priority.
unregisterEventHandlerIf(predicate) Removes handlers that match a condition.

πŸ—οΈ Default Implementation

The default event manager includes:

  • Thread-safe internal subscription map.
  • Priority-based sorting.
  • Debug logging using PlatformLogger when exceptions occur.
  • Integrated cancellation support via CancellableNPCEvent.

Create a default one like this:

val eventManager = NPCEventManager.createDefault(debug = true, logger = myLogger)

You can create your own custom event manager by implementing the NPCEventManager interface to fit your platform’s specific needs.


πŸ“Œ Custom Usage Example

Registering a handler for a custom event:

eventManager.registerEventHandler(MyEvent::class.java) { event ->
    println("NPC interacted with ${event.npc.entityId}")
}

Removing handlers conditionally:

eventManager.unregisterEventHandlerIf {
    it.eventType == MyEvent::class.java
}

🧩 How It Works Internally

  1. Events are passed through the NPC's internal handler.
  2. Subscribed handlers are matched by event type.
  3. Sorted by priority and executed unless the event is cancelled.
  4. Errors are logged if debug is enabled.

πŸ”¦ Events

Velar provides some events to catch when certain actions are executed. Note that all events shown below are not platform specific. Event listeners must be registered into the event manager provided by the platform and not, for example, into the Bukkit event system.

Event listeners can be called from any thread, so there is no guarantee that events happen on a specific thread (such as the main server thread). For example, if you need to access the Bukkit API, make sure that you manually execute the corresponding code on the main server thread.

Event Class Name Description
AttackNPCEvent Fired when a player hits (attacks) a NPC
InteractNPCEvent Fired when the player interacts with a NPC. Note that due to Minecraft weirdness this event is always called for both player hands at the moment.
ShowNPCEvent.Pre Fired before a NPC is spawned for a player. This event can be cancelled to prevent this action.
ShowNPCEvent.Post Fired after a NPC was successfully spawned to a player.
HideNPCEvent.Pre Fired before a NPC is despawned for a player. This event can be cancelled to prevent this action.
HideNPCEvent.Post Fired after a NPC was successfully despawned for a player.

Velar added platform-specific extensions for convenience, such as NPCEvent.<platform>NPC() or PlayerNPCEvent.<platform>Player() to get typed instances. For example, in Bukkit you can call event.bukkitNPC() or event.bukkitPlayer() and it'll return typed player/NPC objects.

Example:

class AttackEventConsumer : NPCEventConsumer<AttackNPCEvent> {  
    override fun handle(event: AttackNPCEvent) {
        val npc = event.bukkitNPC()
        val player = event.bukkitPlayer()
 
        // handle event ...
    }
}
⚠️ **GitHub.com Fallback** ⚠️