Overview
The EventManager is the central hub for LiquidBounce’s event system. It manages event registration, dispatching, and provides reactive event flows using Kotlin coroutines.
Key Features
- Fast event dispatching using lookup tables
- Support for event hooks and listeners
- Reactive event streams via Kotlin Flows
- Priority-based event handling
- Automatic error handling and reporting
Core Methods
registerEventHook
Registers an event hook for a specific event type.
fun <T : Event> registerEventHook(
eventClass: Class<out Event>,
eventHook: EventHook<T>
): EventHook<T>
Parameters:
eventClass - The class of the event to listen for
eventHook - The hook to execute when the event is called
Returns: The registered event hook
Example:
val hook = EventManager.registerEventHook(GameTickEvent::class.java) { event ->
// Handle game tick
}
unregisterEventHook
Unregisters a previously registered event hook.
fun <T : Event> unregisterEventHook(
eventClass: Class<out Event>,
eventHook: EventHook<T>
)
callEvent
Dispatches an event to all registered listeners.
fun <T : Event> callEvent(event: T): T
Parameters:
event - The event instance to dispatch
Returns: The same event instance (useful for checking if it was cancelled)
Example:
val event = AttackEntityEvent(entity)
EventManager.callEvent(event)
if (event.isCancelled) {
// Event was cancelled by a handler
}
eventFlow
Gets a reactive flow for a specific event type.
fun <T : Event> eventFlow(eventClass: Class<T>): SharedFlow<T>
Parameters:
eventClass - The class of the event to observe
Returns: A SharedFlow that emits events after all hooks are executed
Example:
val tickFlow = EventManager.eventFlow(GameTickEvent::class.java)
// In a coroutine
ticFlow.collect { event ->
// Handle tick event
}
Convenience function:
// Using reified type parameter
val tickFlow = eventFlow<GameTickEvent>()
Event Lifecycle
- Event Creation - An event instance is created
- Event Dispatch -
callEvent() is called with the event
- Hook Execution - All registered hooks are executed in order
- Flow Emission - Event is emitted to the reactive flow
- Completion - Event’s
isCompleted flag is set to true
Error Handling
The EventManager automatically handles exceptions in event handlers:
ReportedException - Logged via ErrorHandler with detailed reporting
- Other exceptions - Logged with event and handler context
- Errors don’t prevent other handlers from executing
Best Practices
Use event flows for reactive, coroutine-based event handling. Use hooks for traditional callback-style handling.
Never perform blocking operations in event handlers as they run synchronously on the game thread.
See Also