-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit adds the option to create event listeners with filters in a builder style
- Loading branch information
Showing
6 changed files
with
132 additions
and
44 deletions.
There are no files selected for viewing
42 changes: 0 additions & 42 deletions
42
src/main/java/de/rexlmanu/boilerplate/event/BukkitEventBus.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
src/main/java/de/rexlmanu/boilerplate/event/EventListenerBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
src/main/java/de/rexlmanu/boilerplate/event/impl/BukkitEventBus.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters