add EventManager
This commit is contained in:
19
build.gradle
19
build.gradle
@@ -13,15 +13,34 @@ project.version = projectVersion
|
|||||||
repositories {
|
repositories {
|
||||||
mavenLocal()
|
mavenLocal()
|
||||||
mavenCentral()
|
mavenCentral()
|
||||||
|
maven { url 'https://hub.spigotmc.org/nexus/content/groups/public' }
|
||||||
}
|
}
|
||||||
|
|
||||||
ext {
|
ext {
|
||||||
libs = [
|
libs = [
|
||||||
|
bukkit: [lib: 'org.bukkit:bukkit:1.12.2-R0.1-SNAPSHOT', exclude: [
|
||||||
|
'com.google.code.gson:gson',
|
||||||
|
'com.google.guava:guava',
|
||||||
|
'com.googlecode.json-simple:json-simple',
|
||||||
|
'commons-lang:commons-lang',
|
||||||
|
'org.yaml:snakeyaml'
|
||||||
|
]],
|
||||||
lombok: 'org.projectlombok:lombok:1.18.12'
|
lombok: 'org.projectlombok:lombok:1.18.12'
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
def compileOnly2(library) {
|
||||||
|
dependencies.compileOnly library.lib, {
|
||||||
|
library.exclude.each { String excludeLibStr ->
|
||||||
|
String[] excludeLib = excludeLibStr.split(':')
|
||||||
|
exclude group: excludeLib[0], module: excludeLib[1]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
compileOnly libs.lombok
|
compileOnly libs.lombok
|
||||||
annotationProcessor libs.lombok
|
annotationProcessor libs.lombok
|
||||||
|
|
||||||
|
compileOnly2 libs.bukkit
|
||||||
}
|
}
|
||||||
68
src/main/java/ghast/EventManager.java
Normal file
68
src/main/java/ghast/EventManager.java
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
package ghast;
|
||||||
|
|
||||||
|
import lombok.AccessLevel;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.Setter;
|
||||||
|
import lombok.experimental.UtilityClass;
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.event.Event;
|
||||||
|
import org.bukkit.event.EventPriority;
|
||||||
|
import org.bukkit.event.Listener;
|
||||||
|
import org.bukkit.plugin.Plugin;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.function.BooleanSupplier;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
|
@UtilityClass
|
||||||
|
@SuppressWarnings("unused")
|
||||||
|
public class EventManager {
|
||||||
|
|
||||||
|
public Builder createContext(Plugin plugin) {
|
||||||
|
return new Builder(plugin);
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
|
||||||
|
public static class Builder {
|
||||||
|
|
||||||
|
private static final BooleanSupplier EMPTY_FILTER = () -> true;
|
||||||
|
|
||||||
|
private final EventContext eventContext = new EventContext();
|
||||||
|
private final Plugin plugin;
|
||||||
|
|
||||||
|
public Builder filter(BooleanSupplier filter) {
|
||||||
|
eventContext.setFilter(filter != null ? filter : EMPTY_FILTER);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public <T extends Event> Builder onEvent(Class<T> eventType, EventPriority eventPriority, Consumer<T> consumer) {
|
||||||
|
eventContext.getEventMap().put(eventType, consumer);
|
||||||
|
Bukkit.getPluginManager().registerEvent(eventType, eventContext, eventPriority,
|
||||||
|
eventContext::eventExecute, plugin);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public <T extends Event> Builder onEvent(Class<T> eventType, Consumer<T> consumer) {
|
||||||
|
return onEvent(eventType, EventPriority.NORMAL, consumer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
|
||||||
|
@Getter(AccessLevel.PRIVATE)
|
||||||
|
@Setter(AccessLevel.PRIVATE)
|
||||||
|
private static class EventContext implements Listener {
|
||||||
|
|
||||||
|
private final Map<Class<? extends Event>, Consumer<?>> eventMap = new HashMap<>();
|
||||||
|
private BooleanSupplier filter;
|
||||||
|
|
||||||
|
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||||
|
private void eventExecute(Listener listener, Event event) {
|
||||||
|
Consumer consumer = eventMap.get(event.getClass());
|
||||||
|
if (consumer != null && filter.getAsBoolean()) {
|
||||||
|
consumer.accept(event);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user