UNPKG

yoni-mcscripts-lib

Version:

为 Minecraft Script API 中的部分接口创建了 wrapper,并提供简单的事件管理器和任务管理器,另附有一些便于代码编写的一些小工具。

54 lines (53 loc) 1.72 kB
import { CommandQueueExecuteStatus } from "./CommandQueueExecuteStatus.js"; /** * contains command queue infos */ export class CommandQueue { sender; command; #status = CommandQueueExecuteStatus.executing; get status() { return this.#status; } #result; get result() { if (this.status === CommandQueueExecuteStatus.executing) throw new Error("command executing have not been done"); return this.#result; } resolveResult(commandResult) { if (this.#status === CommandQueueExecuteStatus.executing) { this.#status = CommandQueueExecuteStatus.done; this.#result = commandResult; this.#resolveCallbacks.forEach(cb => cb(commandResult)); this.#fulfilledCallbacks.forEach(cb => cb(commandResult)); } } reject(err) { if (this.#status === CommandQueueExecuteStatus.executing) { this.#status = CommandQueueExecuteStatus.failed; this.#result = err; this.#fulfilledCallbacks.forEach(cb => cb(err)); this.#rejectCallbacks.forEach(cb => cb(err)); } } constructor(sender, command) { if (typeof sender?.runCommand !== "function") { throw new TypeError("sender cannot runCommand()"); } this.sender = sender; this.command = String(command); } #fulfilledCallbacks = []; #resolveCallbacks = []; #rejectCallbacks = []; onresolve(callback) { this.#resolveCallbacks.push(callback); } onreject(callback) { this.#rejectCallbacks.push(callback); } onfulfilled(callback) { this.#fulfilledCallbacks.push(callback); } }