yoni-mcscripts-lib
Version:
为 Minecraft Script API 中的部分接口创建了 wrapper,并提供简单的事件管理器和任务管理器,另附有一些便于代码编写的一些小工具。
92 lines (91 loc) • 3.01 kB
JavaScript
import { VanillaWorld, Minecraft } from "../basis.js";
import { getAllVanillaDimensions } from "../dimensionutils.js";
import { EntityUtils } from "../EntityUtils.js";
import { Scoreboard } from "../scoreboard/Scoreboard.js";
import { Dimension } from "./dimension.js";
import { copyPropertiesWithoutOverride } from "../lib/ObjectUtils.js";
/**
* 代表由一系列维度与其环境组成的世界。
*/
class World {
static isWorld(object) {
return object instanceof Minecraft.World || object instanceof World;
}
vanillaWorld = VanillaWorld;
constructor(vanillaWorld) {
Object.defineProperty(this, "vanillaWorld", {
configurable: false,
enumerable: false,
writable: false,
value: vanillaWorld
});
}
get scoreboard() {
return Scoreboard;
}
/**
* @deprecated 自从返回结果改为数组之后,这方法就没有意义了。
* 查找游戏中符合特定条件的玩家。
* @param {Minecraft.EntityQueryOptions} options
* @yields {YoniPlayer}
*/
*selectPlayers(options) {
for (let pl of VanillaWorld.getPlayers(options)) {
yield EntityUtils.from(pl);
}
}
/**
* 获取游戏中符合特定条件的玩家。
* @param {Minecraft.EntityQueryOptions} [option]
* @yields {YoniPlayer}
*/
getPlayers(option) {
return EntityUtils.getWorldVanillaPlayers(option).map(EntityUtils.from);
}
/**
* 获取与 `dimid` 对应的维度对象。
* @param {string|number} dimid
* @returns {Dimension}
*/
getDimension(dimid) {
//@ts-ignore
return Dimension.toDimension(dimid);
}
/**
* 获取一个游戏中的所有玩家的对象。
* @returns {YoniPlayer[]} 一个包含了游戏中所有玩家的对象的数组。
*/
getAllPlayers() {
return Array.from(this.getPlayers());
}
/**
* 获取一个包含了当前世界中已经加载的所有实体的对象的数组。
*/
getLoadedEntities() {
return EntityUtils.getLoadedVanillaEntities()
.map(ent => EntityUtils.from(ent));
}
/**
* @deprecated 自从返回结果改为数组之后,这方法就没有意义了。
* 查找游戏中符合特定条件的实体。
* @param {Minecraft.EntityQueryOptions} options
* @yields {YoniEntity}
*/
*selectEntities(option) {
for (let d of getAllVanillaDimensions()) {
for (let entity of d.getEntities(option)) {
yield EntityUtils.from(entity);
}
}
}
/**
* 获取所有生物实体。
*/
getLivingEntities() {
return this.getLoadedEntities()
.filter((ent) => ent.isLivingEntity());
}
}
copyPropertiesWithoutOverride(World.prototype, Minecraft.World.prototype, "vanillaWorld");
export { World, World as YoniWorld };
export const world = new World(VanillaWorld);