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

https://github.com/conclube/eventbuilder

Functional event listener builder for bukkit events.
https://github.com/conclube/eventbuilder

bukkit hacktoberfest java minecraft spigot

Last synced: about 1 year ago
JSON representation

Functional event listener builder for bukkit events.

Awesome Lists containing this project

README

          

# EventBuilder
Functional event listener builder for bukkit events.

## Setup
To use this, you will have to shade it into your jar.

```gradle
repositories {
mavenCentral()
maven {url 'http://repo.bristermitten.me/repository/maven-releases/'}
}

dependencies {
implementation 'me.conclure:event-builder:1.1.1'
}
```

## Usage

### Get Started
You will start everything with the class `EventBuilders`.
Then call `EventBuilders.create(Class)` which will give you an `EventBuilder`.

```java
EventBuilder eventBuilder = EventBuilders.create(PlayerJoinEvent.class);
```

**NOTE**: You may not use event classes which doesn't have the static method `#getHandlerList()`.


To assign an action to the builder, simply call `EventBuilder#execute(Consumer)`.

```java
eventBuilder.execute(event -> event.setJoinMessage("Someone joined."));
```


Then we can get the an `EventHandler` by calling `EventBuilder#build()` that won't allow any modifications to it's actions.
You may now set the event priority by `EventHandler#eventPriority(EventPriority)`
and if handler should ignore cancelled by `EventHandler#ignoreCancelled(boolean)`.

```java
EventHandler eventHandler = eventBuilder.build()
.ignoreCancelled(true)
.eventPriority(EventPriority.MONITOR);
```


You can then register the event handler by calling `EventHandler#register(Plugin)` which will give you an `EventSubscription`.
Once you have gotten the subscription you won't be able to modify anything. The plugin instance should be your own.

```java
EventSubscription eventSubscription = eventHandler.register(myPluginInstance);
```


If you prefer, you may also chain all the methods as a builder pattern.

```java
EventSubscription eventSubscription = EventBuilders.create(PlayerJoinEvent.class)
.execute(event -> event.setJoinMessage("Someone joined."))
.build()
.ignoreCancelled(true)
.eventPriority(EventPriority.MONITOR)
.register(myPluginInstance);
```

**NOTE**: You can skip calling `#build()` and instead call `#register(Plugin)` directly,
that will set ignoreCancelled to false and eventPriority to NORMAL.


It's recommended to unregister the subscription when your plugin is disabling `JavaPlugin#onDisable()`.

```java

@Override
public void onDisable() {
eventSubscription.unregister();
}

```

### Advanced
Now let's look into more functionalites you can utilize.
Firstly you can use a `Predicate` as a filter.
This will cause all actions declared under the filter to not execute if the filter doesn't return true.

```java
EventBuilder eventBuilder = EventBuilders.create(PlayerJoinEvent.class)
.filter(event -> !event.isAsynchronous())
.execute(event -> event.setJoinMessage("Someone joined.")); //Will only run if the event isn't async
```


There is also `EventBuilder#unregisterIf(Predicate)` which instead of filtering will unregister the event subscription if the predicate is true.

```java
eventBuilder.unregisterIf(Event::isAsynchronous);
```

**NOTE**: Any actions declared underneath it will still run that time.


You can also utilize `EventBuilder#executeIf(Predicate,Consumer)`.
The consumer will only run if the predicate is true. The predicate won't apply to any other actions else than the consumer declared after it
albeit filters above it will still apply.

```java
//Will only run if the event is async
eventBuilder.executeIf(event -> event.isAsynchronous(), event.setJoinMessage("Nobody joined."));
```

```java
eventBuilder.filter(event -> event.getPlayer().hasPlayerBefore())

//Will only run if the event is async and if the player has played before
.executeIf(event -> event.isAsynchronous(), event.setJoinMessage("Nobody joined."));
```

```java
eventBuilder.filter(event -> event.getPlayer().hasPlayedBefore())

//Will only run if the event is async and if the player has played before
.executeIf(event -> event.isAsynchronous(), event.setJoinMessage("Nobody joined."))

//Will only run if the player has played before
.execute(event -> event.setJoinMessage("An OG player joined."));
```

## Contributions
This project is open for any pull requests that has reasonable changes.