跳到主要内容

玩家与实体

本页的函数接受 player、实体查询或事件返回的实体句柄。读取可能缺失的句柄之前,先检查它是否为 null。

查找实体​

mc.entities 下的函数结果
player()查找应用对应的玩家或执行命令的玩家。两者都没有时,查找最近的存活玩家。
player(name)按名称查找在线玩家,找不到时返回 null。
player("all")返回所有维度中的在线玩家数组。
player("*")、player("survival")、player("creative")、player("spectating")返回当前维度中符合条件的玩家数组。"survival" 包括冒险模式。
entity_id(idOrUuid)在当前维度按数字 ID 或 UUID 查找已加载的实体。
entity_list(descriptor)查找符合类型或分类的已加载实体,例如 "minecraft:cow" 或 "living"。
entity_area(descriptor, center, range)查找以坐标或实体为中心、向三轴各扩展 [dx, dy, dz] 的盒子中的实体。
entity_selector(selector)在当前命令上下文中解析 Minecraft 选择器,返回匹配的实体数组。
entity_types([descriptor])列出符合分类的已注册类型。省略参数时列出全部类型。
function onlinePositions() {
return mc.entities.player("all").map(player => ({
name: mc.entities.query(player, "name"),
pos: mc.entities.query(player, "pos"),
dimension: mc.entities.query(player, "dimension")
}));
}

读写属性与生成实体​

mc.entities 下的函数用途
query(entity, property, ...args)读取属性。常用键有 name、uuid、id、type、pos、motion、dimension、health、yaw 和 pitch。
modify(entity, property, ...args)修改支持写入的属性,并返回实体句柄。常用可写键有 pos、motion、yaw、pitch、health 和 custom_name。
spawn(type, pos[, nbt])在当前维度生成可召唤的实体。NBT 可用 SNBT 字符串或 NBT 句柄传入。返回实体句柄,生成失败时可返回 null。
entity_event(entity, name, callback, ...args)监听单个实体。传入 null 可移除回调,见实体事件参数。
entity_load_handler(descriptor, callback, ...args)监听某类实体进入世界。回调先接收 (entity, created),再接收绑定的额外参数。传入 null 可移除回调。

位置和速度使用数组。例如,mc.entities.modify(entity, "motion", [0, 0.3, 0]) 设置竖直速度。health 用于生物实体。