Package-level declarations
The dev.lyzev.api.event
package is a core component of the Piko event management library. It provides the necessary interfaces and classes to create, register, and manage events and event listeners.
Usage Example
Creating an Event
To create a custom event, extend the CancellableEvent
class (or Event
if it doesn't need to be cancellable).
class TestEvent(val a: Int) : CancellableEvent()
Implementing an Event Listener
Implement the EventListener
interface and register event handlers using the on
function.
class TestEventListener : EventListener {
var handle = true
init {
on<TestEvent>(Event.Priority.HIGH) { event ->
if (event.a == 5)
event.isCancelled = true
println("TestEvent: ${event.a}")
}
}
override val shouldHandleEvents
get() = handle
}
Triggering an Event
Create an instance of the event and call the fire
method to trigger it.
val event = TestEvent(5)
event.fire()
This will execute all registered listeners for the TestEvent
class, respecting their priority and cancellation status.
Types
An event that can be cancelled
Interface for event listeners. Implementations can register event handlers using the on function.
The event manager is responsible for registering and triggering events. It allows for event listeners to be registered for specific event classes. When an event is triggered, all registered listeners for the event class are executed. Event listeners are executed in order of their priority, with higher priority listeners
Functions
Registers an event listener for the specified event class.