2019-09-21 07:04:47 +00:00
|
|
|
import { event, plugin } from '@ms/api'
|
|
|
|
import { injectable, inject } from '@ms/container';
|
|
|
|
|
|
|
|
let Modifier = Java.type("java.lang.reflect.Modifier");
|
|
|
|
let Order = Java.type("org.spongepowered.api.event.Order");
|
|
|
|
let Event = Java.type("org.spongepowered.api.event.Event");
|
|
|
|
let EventListener = Java.type("org.spongepowered.api.event.EventListener");
|
|
|
|
|
|
|
|
let Sponge = Java.type("org.spongepowered.api.Sponge");
|
|
|
|
|
|
|
|
let priorityMap = {
|
|
|
|
'LOWEST': 'PRE',
|
|
|
|
'LOW': 'FIRST',
|
|
|
|
'NORMAL': 'DEFAULT',
|
|
|
|
'HIGH': 'LATE',
|
|
|
|
'HIGHEST': 'LAST',
|
|
|
|
'MONITOR': 'POST'
|
|
|
|
};
|
|
|
|
/**
|
|
|
|
* Sponge Event Impl
|
|
|
|
*/
|
|
|
|
@injectable()
|
|
|
|
export class SpongeEvent extends event.Event {
|
|
|
|
@inject(plugin.PluginInstance)
|
|
|
|
private pluginInstance: any;
|
2019-09-22 10:01:13 +00:00
|
|
|
private eventManager: any = Sponge.getEventManager();
|
2019-09-21 07:04:47 +00:00
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super('org/spongepowered/api/event');
|
|
|
|
}
|
|
|
|
|
|
|
|
isValidEvent(clazz: any): boolean {
|
|
|
|
//继承于 org.spongepowered.api.event.Event
|
|
|
|
return Event.class.isAssignableFrom(clazz) &&
|
|
|
|
// 访问符为Public
|
|
|
|
Modifier.isPublic(clazz.getModifiers()) &&
|
|
|
|
// Sponge的事件都是接口
|
|
|
|
Modifier.isAbstract(clazz.getModifiers());
|
|
|
|
}
|
|
|
|
class2Name(clazz: any) {
|
2019-09-22 10:01:13 +00:00
|
|
|
return clazz.name.substring(clazz.name.lastIndexOf(".") + 1);
|
2019-09-21 07:04:47 +00:00
|
|
|
}
|
|
|
|
register(eventCls: any, exec: Function, priority: any = 'NORMAL', ignoreCancel: boolean = true) {
|
|
|
|
var listener = new EventListener({
|
|
|
|
handle: exec
|
|
|
|
});
|
2019-09-22 10:01:13 +00:00
|
|
|
this.eventManager.registerListener(this.pluginInstance, eventCls, Order[priorityMap[priority]], listener);
|
2019-09-21 07:04:47 +00:00
|
|
|
return listener;
|
|
|
|
}
|
|
|
|
unregister(event: any, listener: any): void {
|
2019-09-22 10:01:13 +00:00
|
|
|
this.eventManager.unregisterListeners(listener);
|
2019-09-21 07:04:47 +00:00
|
|
|
}
|
|
|
|
}
|