ms/packages/nukkit/src/event.ts
MiaoWoo ca79161505 refactor: rename scope to @ccms
Signed-off-by: MiaoWoo <admin@yumc.pw>
2020-05-03 00:14:47 +08:00

48 lines
1.6 KiB
TypeScript

import { event, plugin } from '@ccms/api'
import { inject, provideSingleton } from '@ccms/container'
const Nukkit: cn.nukkit.Server = base.getInstance().getServer();
const Event = Java.type("cn.nukkit.event.Event");
const Modifier = Java.type("java.lang.reflect.Modifier");
const Listener = Java.type("cn.nukkit.event.Listener");
const EventPriority = Java.type("cn.nukkit.event.EventPriority");
const EventExecutor = Java.type("cn.nukkit.plugin.EventExecutor");
@provideSingleton(event.Event)
export class NukkitEvent extends event.Event {
@inject(plugin.PluginInstance)
private pluginInstance: any
constructor() {
super('cn/nukkit/event');
}
getJarFile(resource: string) {
return super.getJarFile('cn/nukkit/Nukkit.class')
}
isValidEvent(clazz: any): boolean {
// 继承于 org.bukkit.event.Event
return Event.class.isAssignableFrom(clazz) &&
// 访问符为Public
Modifier.isPublic(clazz.getModifiers()) &&
// 不是抽象类
!Modifier.isAbstract(clazz.getModifiers());
}
register(eventCls: any, exec: Function, priority: event.EventPriority, ignoreCancel: boolean) {
let listener = new Listener({});
Nukkit.getPluginManager().registerEvent(
eventCls,
listener,
EventPriority[priority],
new EventExecutor({
execute: exec
}),
this.pluginInstance,
ignoreCancel);
return listener;
}
unregister(event: any, listener: any): void {
event.getMethod('getHandlers').invoke(null).unregister(listener);
}
}