2020-05-02 16:14:47 +00:00
|
|
|
import { event } from '@ccms/api'
|
|
|
|
import { provideSingleton, postConstruct } from '@ccms/container'
|
|
|
|
import * as reflect from '@ccms/common/dist/reflect'
|
2020-01-15 08:43:00 +00:00
|
|
|
|
|
|
|
const Bungee: net.md_5.bungee.api.ProxyServer = base.getInstance().getProxy();
|
|
|
|
const Event = Java.type("net.md_5.bungee.api.plugin.Event");
|
|
|
|
const Modifier = Java.type("java.lang.reflect.Modifier");
|
2020-01-16 09:19:10 +00:00
|
|
|
const ProxyClass = Java.type(base.getProxyClass().name);
|
|
|
|
const ProxyMethod = reflect.on(base.getProxyClass()).method("method");
|
|
|
|
const HashMap = Java.type('java.util.HashMap');
|
2020-01-17 03:09:16 +00:00
|
|
|
const ReflectMethodArray = Java.type('java.lang.reflect.Method[]');
|
2020-01-16 09:19:10 +00:00
|
|
|
|
2020-01-17 03:09:16 +00:00
|
|
|
const Byte = Java.type('java.lang.Byte');
|
2020-01-16 09:19:10 +00:00
|
|
|
const EventPriority = [];
|
2020-01-17 03:09:16 +00:00
|
|
|
EventPriority[event.EventPriority.LOWEST] = Byte.valueOf(-64);
|
|
|
|
EventPriority[event.EventPriority.LOW] = -Byte.valueOf(32);
|
|
|
|
EventPriority[event.EventPriority.NORMAL] = Byte.valueOf(0);
|
|
|
|
EventPriority[event.EventPriority.HIGH] = Byte.valueOf(32);
|
|
|
|
EventPriority[event.EventPriority.HIGHEST] = Byte.valueOf(64);
|
2020-01-15 08:43:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Bungee Event Impl
|
|
|
|
*/
|
2020-02-26 02:15:33 +00:00
|
|
|
@provideSingleton(event.Event)
|
2020-01-15 08:43:00 +00:00
|
|
|
export class BungeeEvent extends event.Event {
|
|
|
|
private pluginManager = Bungee.getPluginManager()
|
|
|
|
|
2020-01-16 09:19:10 +00:00
|
|
|
// EventBus
|
|
|
|
private eventBus: any;
|
|
|
|
// private final Lock lock = new ReentrantLock();
|
|
|
|
private lock: any;
|
|
|
|
// private final Map<Class<?>, Map<Byte, Map<Object, Method[]>>> byListenerAndPriority = new HashMap<>();
|
|
|
|
private byListenerAndPriority: any;
|
|
|
|
// bakeHandlers(Class<?> eventClass)
|
|
|
|
private bakeHandlers: any;
|
|
|
|
|
2020-01-15 08:43:00 +00:00
|
|
|
constructor() {
|
|
|
|
super('net/md_5/bungee/api/event');
|
|
|
|
}
|
|
|
|
|
2020-01-16 09:19:10 +00:00
|
|
|
@postConstruct()
|
|
|
|
init() {
|
|
|
|
this.eventBus = reflect.on(this.pluginManager).get('eventBus').get();
|
|
|
|
this.lock = reflect.on(this.eventBus).get('lock').get()
|
|
|
|
this.byListenerAndPriority = reflect.on(this.eventBus).get('byListenerAndPriority').get();
|
|
|
|
this.bakeHandlers = reflect.accessible(reflect.on(this.eventBus).method("bakeHandlers"));
|
|
|
|
}
|
|
|
|
|
2020-01-15 08:43:00 +00:00
|
|
|
isValidEvent(clazz: any): boolean {
|
|
|
|
//继承于 net.md_5.bungee.api.plugin.Event
|
|
|
|
return Event.class.isAssignableFrom(clazz) &&
|
|
|
|
// 访问符为Public
|
|
|
|
Modifier.isPublic(clazz.getModifiers()) &&
|
|
|
|
// 不是抽象类
|
|
|
|
!Modifier.isAbstract(clazz.getModifiers());
|
|
|
|
}
|
2020-01-16 09:19:10 +00:00
|
|
|
|
2020-01-15 08:43:00 +00:00
|
|
|
register(eventCls: any, exec: Function, priority: event.EventPriority = event.EventPriority.NORMAL, ignoreCancel: boolean = true) {
|
2020-01-16 09:19:10 +00:00
|
|
|
this.lock.lock()
|
|
|
|
try {
|
|
|
|
// private final Map<类, Map<优先级, Map<监听器, 方法[]>>> byListenerAndPriority = new HashMap<>();
|
|
|
|
// this.byListenerAndPriority.put(eventCls)
|
|
|
|
// Map<优先级, Map<监听器, 方法[]>>
|
|
|
|
let prioritiesMap = this.byListenerAndPriority.get(eventCls);
|
|
|
|
if (prioritiesMap == null) {
|
|
|
|
prioritiesMap = new HashMap();
|
|
|
|
this.byListenerAndPriority.put(eventCls, prioritiesMap);
|
|
|
|
}
|
|
|
|
// Map<监听器, 方法[]>
|
|
|
|
let currentPriorityMap = prioritiesMap.get(EventPriority[priority]);
|
|
|
|
if (currentPriorityMap == null) {
|
|
|
|
currentPriorityMap = new HashMap();
|
|
|
|
prioritiesMap.put(EventPriority[priority], currentPriorityMap);
|
|
|
|
}
|
|
|
|
let listener = new ProxyClass(ScriptEngineContextHolder.getEngine(), "exec(args)", { exec, priority: EventPriority[priority] })
|
|
|
|
// 方法[]
|
2020-01-17 03:09:16 +00:00
|
|
|
let methods = new ReflectMethodArray(1);
|
|
|
|
methods[0] = ProxyMethod;
|
|
|
|
currentPriorityMap.put(listener, methods);
|
2020-01-16 09:19:10 +00:00
|
|
|
this.bakeHandlers.invoke(this.eventBus, eventCls);
|
|
|
|
return listener;
|
|
|
|
} catch (ex) {
|
2020-01-17 03:09:16 +00:00
|
|
|
console.ex(ex)
|
|
|
|
} finally {
|
2020-01-16 09:19:10 +00:00
|
|
|
this.lock.unlock()
|
|
|
|
}
|
2020-01-15 08:43:00 +00:00
|
|
|
}
|
2020-01-16 09:19:10 +00:00
|
|
|
|
|
|
|
unregister(eventCls: any, listener: any): void {
|
|
|
|
this.lock.lock()
|
|
|
|
try {
|
|
|
|
// private final Map<类, Map<优先级, Map<监听器, 方法[]>>> byListenerAndPriority = new HashMap<>();
|
|
|
|
// Map<优先级, Map<监听器, 方法[]>>
|
|
|
|
let prioritiesMap = this.byListenerAndPriority.get(eventCls);
|
|
|
|
if (prioritiesMap != null) {
|
2020-01-17 03:09:16 +00:00
|
|
|
let bindings = reflect.on(listener).get("bindings").get();
|
2020-02-13 20:02:37 +00:00
|
|
|
let priority = bindings["priority"];
|
2020-01-16 09:19:10 +00:00
|
|
|
// Map<监听器, 方法[]>
|
|
|
|
let currentPriorityMap = prioritiesMap.get(priority);
|
|
|
|
if (currentPriorityMap != null) {
|
|
|
|
currentPriorityMap.remove(listener);
|
|
|
|
if (currentPriorityMap.isEmpty()) {
|
|
|
|
prioritiesMap.remove(priority);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (prioritiesMap.isEmpty()) {
|
|
|
|
this.byListenerAndPriority.remove(eventCls);
|
|
|
|
}
|
|
|
|
this.bakeHandlers.invoke(this.eventBus, eventCls);
|
|
|
|
}
|
|
|
|
} catch (ex) {
|
2020-01-17 03:09:16 +00:00
|
|
|
console.ex(ex)
|
|
|
|
} finally {
|
2020-01-16 09:19:10 +00:00
|
|
|
this.lock.unlock()
|
|
|
|
}
|
2020-01-15 08:43:00 +00:00
|
|
|
}
|
|
|
|
}
|