2020-02-26 02:15:33 +00:00
|
|
|
import { event, plugin } from '@ms/api'
|
|
|
|
import { inject, provideSingleton } from '@ms/container';
|
2019-09-19 10:58:01 +00:00
|
|
|
import * as reflect from '@ms/common/dist/reflect'
|
|
|
|
|
2019-09-27 01:49:45 +00:00
|
|
|
const Bukkit = Java.type("org.bukkit.Bukkit");
|
|
|
|
const Event = Java.type("org.bukkit.event.Event");
|
|
|
|
const Modifier = Java.type("java.lang.reflect.Modifier");
|
|
|
|
const Listener = Java.type("org.bukkit.event.Listener");
|
|
|
|
const EventPriority = Java.type("org.bukkit.event.EventPriority");
|
|
|
|
const EventExecutor = Java.type("org.bukkit.plugin.EventExecutor");
|
2019-09-19 10:58:01 +00:00
|
|
|
|
2020-02-26 02:15:33 +00:00
|
|
|
@provideSingleton(event.Event)
|
2019-09-19 10:58:01 +00:00
|
|
|
export class BukkitEvent extends event.Event {
|
|
|
|
@inject(plugin.PluginInstance)
|
|
|
|
private pluginInstance: any
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super('org/bukkit/event');
|
|
|
|
}
|
|
|
|
|
2019-09-27 01:49:45 +00:00
|
|
|
getJarFile(resource: string) {
|
|
|
|
return super.getJarFile('org/bukkit/Bukkit.class')
|
|
|
|
}
|
2019-09-19 10:58:01 +00:00
|
|
|
isValidEvent(clazz: any): boolean {
|
|
|
|
// 继承于 org.bukkit.event.Event
|
|
|
|
return Event.class.isAssignableFrom(clazz) &&
|
|
|
|
// 访问符为Public
|
|
|
|
Modifier.isPublic(clazz.getModifiers()) &&
|
|
|
|
// 不是抽象类
|
|
|
|
!Modifier.isAbstract(clazz.getModifiers());
|
|
|
|
}
|
2020-01-15 08:48:18 +00:00
|
|
|
register(eventCls: any, exec: Function, priority: event.EventPriority, ignoreCancel: boolean) {
|
2019-09-27 01:49:45 +00:00
|
|
|
let listener = new Listener({});
|
2019-09-19 10:58:01 +00:00
|
|
|
Bukkit.pluginManager.registerEvent(
|
|
|
|
eventCls,
|
|
|
|
listener,
|
|
|
|
EventPriority[priority],
|
|
|
|
new EventExecutor({
|
|
|
|
execute: exec
|
|
|
|
}),
|
|
|
|
this.pluginInstance,
|
|
|
|
ignoreCancel);
|
|
|
|
return listener;
|
|
|
|
}
|
|
|
|
unregister(event: any, listener: any): void {
|
|
|
|
reflect.on(event).call('getHandlerList').get().unregister(listener);
|
|
|
|
}
|
|
|
|
}
|