UNPKG

yoni-mcscripts-lib

Version:

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

36 lines (35 loc) 1.03 kB
export class CommandList { #commands = [[], [], [], [], []]; #indexes = [0, 0, 0, 0, 0]; #count = [0, 0, 0, 0, 0]; hasNext() { for (const prio of [0, 1, 2, 3, 4]) { if (this.#count[prio] > 0) return true; } return false; } next() { let nextCommand = null; for (const prioIndex of [0, 1, 2, 3, 4]) { if (this.#count[prioIndex] > 0) { const index = this.#indexes[prioIndex]; nextCommand = this.#commands[prioIndex][index]; this.#indexes[prioIndex] += 1; this.#count[prioIndex] -= 1; break; } } if (!nextCommand) throw new Error("no next command"); return nextCommand; } add(prio, command) { const prioIndex = prio - 1; this.#commands[prioIndex].push(command); this.#count[prioIndex] += 1; } count() { return this.#count.reduce((a, b) => a + b); } }