-
Notifications
You must be signed in to change notification settings - Fork 0
Events
To interact with events, you need to create a listener.
import fr.valentin.lib.vallib.event.Listener
class EventListener: Listener() {
}To intercept an event, you need to add a function with the annotation @EventHandler.
And define, in the first argument, your event targeted class :
import fr.valentin.lib.vallib.event.Listener
import fr.valentin.lib.vallib.event.EventHandler
class EventListener: Listener() {
@EventHandler
fun onMyCustomEvent(event: MyCustomTargetedEvent) {
println("Event called !")
}
}After that, you need to register this listener in the EventRegister object:
EventRegister.registerListener(EventListener())It exists 2 types of event:
- Classic
- Cancellable
To creates a classic event, make a class and implement Event:
class BasicEvent: EventCongrat's you have your first event !
You can add some information if you want:
data class BasicEvent(val cause: String, val src: String): EventA cancellable event can prevent some actions.
To creates a classic event, make a class and implement Event and Cancellable:
class BasicEvent: Event, Cancellable {
override var cancel: Boolean = false
}Cancellable adds val cancel: Boolean in the event.
You can change this value when the event is in a listener.
To run an event, you need to call the run event function :
EventRegister.runEvent(MyCustomTargetedEvent())EventRegister.runEvent returns true if the event can pass.
You can intercept it like that:
val file = File("./data.mdb")
if(EventRegister.runEvent(CreateFileEvent(file))) {
if(file.exists()) {
file.createNewFile()
}
} else {
println("Event cancelled")
}If you want to execute every super class event of your object, you can add a second parameter :
EventRegister.runEvent(SuperBasicEvent("./data.mdb"), true)In this case, if you have an event structure like that, it runs every super class event handlers :
open class BasicEvent: Event, Cancellable {
override var cancel: Boolean = false
}
data class SuperBasicEvent(val src: String): BasicEvent()It runs :
@EventHandler
fun onMyCustomSuperBasicEvent(event: SuperBasicEvent) {
println("Event called !")
}
@EventHandler
fun onMyCustomBasicEvent(event: BasicEvent) {
println("Super class event called too !")
}