yoni-mcscripts-lib
Version:
为 Minecraft Script API 中的部分接口创建了 wrapper,并提供简单的事件管理器和任务管理器,另附有一些便于代码编写的一些小工具。
58 lines (57 loc) • 1.92 kB
JavaScript
import { MinecraftSystem as system } from "../basis.js";
import { CommandList } from "./CommandList.js";
export class CommandExecutor {
static #logger;
static log(...data) {
if (CommandExecutor.#logger)
CommandExecutor.#logger.trace(...data);
else
system.run(async function initLogger() {
const { Logger } = await import("../util/Logger");
CommandExecutor.#logger = new Logger("CommandExecutor");
CommandExecutor.log(...data);
});
}
executingCommand = null;
commandList = new CommandList();
constructor(autoStart) {
if (arguments.length > 0)
if (autoStart)
this.start();
}
start() {
if (this.#scheduleId !== null)
throw new Error("executor already started");
this.#scheduleId = system.runInterval(this.#run.bind(this));
}
#scheduleId = null;
stop() {
if (this.#scheduleId !== null) {
system.clearRun(this.#scheduleId);
this.#scheduleId = null;
}
}
add(priv, command) {
this.commandList.add(priv, command);
}
#run() {
let executeQueueCount = 0;
while (this.commandList.hasNext()) {
let commandQueue = this.commandList.next();
executeQueueCount += 1;
let commandResult;
const { sender, command } = commandQueue;
try {
commandResult = sender.runCommand(command);
}
catch (err) { //鬼知道什么原因导致的错误,直接扔回去
commandQueue.reject(err);
}
//执行完成
//也可能没执行成功
//不过反正再调用一次也不影响(目前)
commandQueue.resolveResult(commandResult);
executeQueueCount += 1;
}
}
}