Skip to content

Commit

Permalink
feat(event): event listener builder
Browse files Browse the repository at this point in the history
This commit adds the option to create event listeners with filters in a builder style
  • Loading branch information
rexlManu committed Mar 20, 2024
1 parent 9d04456 commit c5e76ed
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 44 deletions.
42 changes: 0 additions & 42 deletions src/main/java/de/rexlmanu/boilerplate/event/BukkitEventBus.java

This file was deleted.

2 changes: 2 additions & 0 deletions src/main/java/de/rexlmanu/boilerplate/event/EventBus.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,7 @@ default <T extends Event> Listener subscribe(Class<T> eventType, Consumer<T> act
return this.subscribe(eventType, action, EventPriority.NORMAL);
}

<T extends Event> EventListenerBuilder<T> on(Class<T> eventType);

void unsubscribe(Listener listener);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package de.rexlmanu.boilerplate.event;

import java.util.function.Consumer;
import java.util.function.Predicate;
import org.bukkit.event.Event;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;

public interface EventListenerBuilder<T extends Event> {
EventListenerBuilder<T> priority(EventPriority priority);

EventListenerBuilder<T> ignoreCancelled(boolean ignoreCancelled);

EventListenerBuilder<T> filter(Predicate<T> predicate);

Listener subscribe(Consumer<T> action);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.google.inject.spi.InjectionListener;
import com.google.inject.spi.TypeEncounter;
import com.google.inject.spi.TypeListener;
import de.rexlmanu.boilerplate.event.impl.BukkitEventBus;
import lombok.RequiredArgsConstructor;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
Expand Down
105 changes: 105 additions & 0 deletions src/main/java/de/rexlmanu/boilerplate/event/impl/BukkitEventBus.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package de.rexlmanu.boilerplate.event.impl;

import com.google.inject.Inject;
import com.google.inject.Singleton;
import de.rexlmanu.boilerplate.event.EventBus;
import de.rexlmanu.boilerplate.event.EventListenerBuilder;
import java.util.function.Consumer;
import java.util.function.Predicate;
import lombok.RequiredArgsConstructor;
import org.bukkit.Bukkit;
import org.bukkit.event.Event;
import org.bukkit.event.EventPriority;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin;

@Singleton
@RequiredArgsConstructor(onConstructor = @__(@Inject))
public class BukkitEventBus implements EventBus {
private final JavaPlugin plugin;

@Override
public <T extends Event> Listener subscribe(
Class<T> eventType, Consumer<T> action, EventPriority priority, boolean ignoreCancelled) {
Listener eventListener = new Listener() {};
Bukkit.getPluginManager()
.registerEvent(
eventType,
eventListener,
priority,
(listener, event) -> {
if (eventType.isInstance(event)) {
action.accept(eventType.cast(event));
}
},
this.plugin,
ignoreCancelled);
return eventListener;
}

@Override
public <T extends Event> EventListenerBuilder<T> on(Class<T> eventType) {
return new EventListenerBuilderImpl<>(eventType, this.plugin);
}

@Override
public void unsubscribe(Listener listener) {
HandlerList.unregisterAll(listener);
}

@RequiredArgsConstructor
static class EventListenerBuilderImpl<T extends Event> implements EventListenerBuilder<T> {
private EventPriority priority = EventPriority.NORMAL;
private boolean ignoreCancelled = false;
private Consumer<T> action;
private Predicate<T> predicate = null;

private final Class<T> eventType;
private final JavaPlugin plugin;

@Override
public EventListenerBuilder<T> priority(EventPriority priority) {
this.priority = priority;
return this;
}

@Override
public EventListenerBuilder<T> ignoreCancelled(boolean ignoreCancelled) {
this.ignoreCancelled = ignoreCancelled;
return this;
}

@Override
public EventListenerBuilder<T> filter(Predicate<T> predicate) {
if (this.predicate == null) {
this.predicate = predicate;
} else {
this.predicate = this.predicate.and(predicate);
}
return this;
}

@Override
public Listener subscribe(Consumer<T> action) {
this.action = action;
Listener eventListener = new Listener() {};
Bukkit.getPluginManager()
.registerEvent(
this.eventType,
eventListener,
this.priority,
(listener, event) -> {
if (this.eventType.isInstance(event)) {
T castedEvent = this.eventType.cast(event);
if (this.predicate == null || this.predicate.test(castedEvent)) {
this.action.accept(castedEvent);
}
}
},
this.plugin,
this.ignoreCancelled);
return eventListener;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void loadData() {

this.logger.info("Test instance: {}", instance);

eventBus.subscribe(
this.eventBus.subscribe(
PlayerJoinEvent.class,
event -> {
event.joinMessage(
Expand All @@ -63,7 +63,12 @@ public void loadData() {
this.logger.info("{} has joined the server.", event.getPlayer().getName());
});

eventBus.subscribe(ServerLoadEvent.class, event -> this.logger.info("Server loaded."));
this.eventBus
.on(PlayerJoinEvent.class)
.filter(event -> event.getPlayer().getName().equals("rexlManu"))
.subscribe(event -> this.logger.info("rexlManu joined the server."));

this.eventBus.subscribe(ServerLoadEvent.class, event -> this.logger.info("Server loaded."));
}

@OnPluginDisable
Expand Down

0 comments on commit c5e76ed

Please sign in to comment.