UNPKG

yoni-mcscripts-lib

Version:

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

123 lines (122 loc) 4.12 kB
import { VanillaWorld, Minecraft } from "../basis.js"; import { Location } from "./Location.js"; import { EntityUtils } from "../EntityUtils.js"; import { Command } from "../command.js"; import { DimensionValues } from "../dimensionutils.js"; /** * 代表维度,一种世界内的空间。 */ class Dimension { static #dimensionMappings = new Map(); static isDimensionValue(value) { return value != null && (value instanceof Minecraft.Dimension || value instanceof Dimension || value in DimensionValues); } static toDimension(dimid) { //使用缓存加快异型参数处理速度 let result = Dimension.#dimensionMappings.get(dimid); if (result) { return result; } let vanilla = null; if (typeof dimid === "string" || typeof dimid === "number") { vanilla = DimensionValues[dimid]; if (vanilla == null) { //此处可能抛出错误,如果参数错误的话 vanilla = VanillaWorld.getDimension(dimid); } } else if (dimid instanceof Dimension) { return dimid; } else if (dimid instanceof Minecraft.Dimension) { vanilla = dimid; } if (vanilla == null) { throw new Error("specific identifier doesn't refer to a dimension"); } result = Dimension.#dimensionMappings.get(vanilla); if (result === undefined) { result = new Dimension(vanilla); Dimension.#dimensionMappings.set(dimid, result); Dimension.#dimensionMappings.set(vanilla, result); } return result; } static isDimension(object) { return object instanceof Minecraft.Dimension || object instanceof Dimension; } // @ts-ignore vanillaDimension; /** * Identifier of the dimension. * @throws This property can throw when used. */ // @ts-ignore id; /** * @hideconstructor * @param {Minecraft.Dimension} */ constructor(vanillaDimension) { Object.defineProperty(this, "vanillaDimension", { configurable: false, enumerable: false, writable: false, value: vanillaDimension }); Object.defineProperty(this, "id", { configurable: true, enumerable: false, get() { return this.vanillaDimension.id; }, set(v) { // @ts-ignore this.vanillaDimension.id = v; } }); } /** * 获取指定位置上的方块对象。 */ getBlock(location) { let loc; if (location instanceof Location) { loc = location; } else { loc = new Location(location); } let vanillaBlock = this.vanillaDimension.getBlock(loc.getVanillaBlockLocation()); if (vanillaBlock) return vanillaBlock; else throw new Error("This location has not been loaded\n" + loc.toString()); } getEntities(options) { return this.vanillaDimension.getEntities(options).map(EntityUtils.from); } getEntitiesAtBlockLocation(location) { return this.vanillaDimension.getEntitiesAtBlockLocation(location) .map(EntityUtils.from); } getEntitiesFromRay(location, direction, options) { return this.vanillaDimension.getEntitiesFromRay(location, direction, options) .map(EntityUtils.from); } getPlayers(option) { return this.vanillaDimension.getPlayers(option).map(EntityUtils.from); } fetchCommand(commandString) { return Command.fetchExecute(this.vanillaDimension, commandString); } spawnEntity(identifier, location) { return EntityUtils.from(this.vanillaDimension.spawnEntity(identifier, location)); } spawnItem(item, location) { return EntityUtils.from(this.vanillaDimension.spawnItem(item, location)); } } export { Dimension, Dimension as YoniDimension };