yoni-mcscripts-lib
Version:
为 Minecraft Script API 中的部分接口创建了 wrapper,并提供简单的事件管理器和任务管理器,另附有一些便于代码编写的一些小工具。
63 lines (58 loc) • 2.15 kB
text/typescript
import { MinecraftSystem as system } from "../basis.js";
import { CommandQueue } from "./CommandQueue.js";
import { CommandPriority } from "./CommandPriority.js";
import { CommandList } from "./CommandList.js";
export class CommandExecutor {
static #logger: import("../util/Logger").Logger;
static log(...data: any[]){
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: CommandQueue | null = null;
commandList = new CommandList<CommandQueue>();
constructor(autoStart?: boolean){
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: number | null = null;
stop(){
if (this.#scheduleId !== null){
system.clearRun(this.#scheduleId);
this.#scheduleId = null;
}
}
add(priv: CommandPriority, command: CommandQueue){
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;
}
}
}