Alert is a really simple, easy to use and blazing fast event listening utility.
The project is available on maven central. Just add the dependency like this:
implementation("me.obsilabor:alert:1.0.8")
implementation 'me.obsilabor:alert:1.0.8'
To shade the dependency into your jar, you probably want to use the shadow gradle plugin
<dependency>
<groupId>me.obsilabor</groupId>
<artifactId>alert</artifactId>
<version>1.0.8</version>
</dependency>
For maven you probably have to use any plugin that does the same as shadow, I have no idea how maven works
Kotlin Tutorial
Create a event by extending from Event
(or if the event should be cancellable extend from Cancellable
)
Example:
class RabbitJumpEvent(val rabbit: Rabbit) : Cancellable() {}
Trigger the event using EventManager.callEvent
. If you want to procces the event e.g. if it can be cancelled, you must store the event as a variable.
Example:
fun handleRabbitJumping(rabbit: Rabbit) {
val event = EventManager.callEvent(RabbitJumpEvent(this))
if(event.isCancelled) {
return
}
rabbit.jump()
}
Create a listener just by creating a new class and in the init method, you can use the listen function just like in this example:
class RabbitJumpListener {
init {
subscribeToEvent<RabbitJumpEvent> {
//TODO: Do something cool :)
}
}
}
To prioritize subscriptions, you can change the priority parameter.
class RabbitJumpListener {
init {
subscribeToEvent<RabbitJumpEvent>(priority = EventPriority.HIGHEST) {
//TODO: Do something cool :)
}
}
}
Triggering events in kotlin is just the same as in java
Java Tutorial
Create a event by extending from Event
(or if the event should be cancellable extend from Cancellable
)
Example:
public class RabbitJumpEvent extends Cancellable {
private final Rabbit rabbit;
public RabbitJumpEvent(Rabbit rabbit) {
this.rabbit = rabbit;
}
public Rabbit getRabbit() {
return this.rabbit;
}
}
Trigger the event using EventManager.callEvent
. If you want to procces the event e.g. if it can be cancelled, you must store the event as a variable.
Example:
public void handleRabbitJumping(Rabbit rabbit) {
RabbitJumpEvent event = EventManager.callEvent(event);
if(event.isCancelled()) {
return;
}
rabbit.jump();
}
Create a listener just by creating a new class and a method annotated with @Subscribe. Add the event you want to listen to as an parameter.
public class RabbitJumpListener {
@Subscribe
public void onRabbitJump(RabbitJumpEvent event) {
//TODO: Do something cool :)
}
}
To prioritize subscriptions, just set the priority value of the @Subscribe annotation
public class RabbitJumpListener {
@Subscribe(priority = EventPriority.HIGHEST)
public void onRabbitJump(RabbitJumpEvent event) {
//TODO: Do something cool :)
}
}