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

Link copied to clipboard
abstract class CancellableEvent : Event

An event that can be cancelled

Link copied to clipboard
interface Event

Interface for events. Instances of this interface can be triggered by invoking the fire function.

Link copied to clipboard
interface EventListener

Interface for event listeners. Implementations can register event handlers using the on function.

Link copied to clipboard

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

Link copied to clipboard
inline fun <E : Event> EventListener.on(priority: Event.Priority = Event.Priority.MID, noinline block: (E) -> Unit)

Registers an event listener for the specified event class.