yoni-mcscripts-lib
Version:
为 Minecraft Script API 中的部分接口创建了 wrapper,并提供简单的事件管理器和任务管理器,另附有一些便于代码编写的一些小工具。
23 lines (22 loc) • 951 B
JavaScript
import { getListenerData } from "./EventListener.js";
import { EventPriority } from "../EventPriority.js";
export function EventHandler(options) {
function EventHandlerMethodDecorator(target, propKey, desc) {
const onEvent = target[propKey];
const data = getListenerData(target);
const listeners = data.registeredListeners;
if (!data)
throw new TypeError("no EventListener data found, try decorate the class with @EventListener");
const handler = {
onEvent: function (event) {
listeners.forEach(listener => {
Reflect.apply(onEvent, listener, [event]);
});
},
ignoreCancelled: options.ignoreCancelled ?? true,
options: options.options
};
data.handlerEntries.push([options.event, options.priority ?? EventPriority.NORMAL, handler]);
}
return EventHandlerMethodDecorator;
}