Skip to content
/ alert Public

🚨Alert is a really simple and blazing fast event listening api

License

Notifications You must be signed in to change notification settings

mooziii/alert

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

43 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🚨 alert

Alert is a really simple, easy to use and blazing fast event listening utility.

Tutorial

Add the dependency

The project is available on maven central. Just add the dependency like this:

Gradle (Kotlin)

implementation("me.obsilabor:alert:1.0.8")

Gradle (Groovy)

implementation 'me.obsilabor:alert:1.0.8'

To shade the dependency into your jar, you probably want to use the shadow gradle plugin

Maven

<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

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()
}

Listen to a event

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

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();
}

Listen to a event

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 :)
    }
}

About

🚨Alert is a really simple and blazing fast event listening api

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published