Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/mooziii/alert

🚨Alert is a really simple and blazing fast event listening api
https://github.com/mooziii/alert

events listening

Last synced: 12 days ago
JSON representation

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

Awesome Lists containing this project

README

        

# 🚨 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)

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

#### Gradle (Groovy)

```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

```xml

me.obsilabor
alert
1.0.8

```
*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:

```kotlin
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:

```kotlin
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:

```kotlin
class RabbitJumpListener {

init {
subscribeToEvent {
//TODO: Do something cool :)
}
}
}
```

*To prioritize subscriptions, you can change the priority parameter.*

```kotlin
class RabbitJumpListener {

init {
subscribeToEvent(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:

```java
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:

```java
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.

```java
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*

```java
public class RabbitJumpListener {

@Subscribe(priority = EventPriority.HIGHEST)
public void onRabbitJump(RabbitJumpEvent event) {
//TODO: Do something cool :)
}
}
```