• 欢迎加入MineBBS QQ讨论群:点击查看所有的官方讨论群
  • 我们将于近期对服务器进行迁移,服务可能中断至多2日。请各位安排好自己的访问计划,造成不便敬请谅解!
  • MineBBS入站考试已经上线!想要成为【正式会员】解锁更多功能吗?快来参与吧!【点我去看】

已解决 @minecraft/server,/scripts/.js中怎么检测玩家是否佩戴头盔槽物品?

XiaoXueli

【Lv:4】

开发者
正式会员
注册
2024/06/24
消息
81
金粒
4,731.34金粒
@minecraft/server,/scripts/.js中怎么检测玩家是否佩戴头盔槽物品?

"module_name": "@minecraft/common",
"version": "1.2.0"

"module_name": "@minecraft/server",
"version": "1.17.0"
 
最后编辑:
解决方案
world.events.tick.subscribe并不存在(以前有,但所提及版本已废弃),正确做法是注册worldLoad事件再利用System:
JavaScript:
function handler() {
    // 从 World 取得所有玩家
    const players = world.getAllPlayers();
    player.forEach((player) => {
        // Do something...
    });
}

// 注意: 所提及版本为1.17.0,将worldLoad换成worldInitialize即可(仍无world.events.tick)
// ScriptV2迁移: https://learn.microsoft.com/en-us/minecraft/creator/documents/scripting/v2-overview?view=minecraft-bedrock-experimental
world.afterEvents.worldLoad.subscribe(() => {
    // 用 System 注册回调
    system.runInterval(handler);
});
另外EquipmentSlot是TS接口,除非你用TS写脚本再编译,否则不应当使用(TS的interface在编译成JS时会删除,在特定函数用动态类型检查代替),所以在SAPI的QuickJs语境下是import不到EquipmentSlot的。
要在 Minecraft 1.21.93 至 1.21.132 版本的脚本 (@minecraft/server) 中检测玩家是否在头盔槽 (head) 佩戴了物品,你可以按照以下步骤操作:
获取玩家对象 (Player): 你需要先有一个有效的玩家对象。这通常是通过事件回调(如 world.events.tick.subscribe 中遍历玩家,或事件触发时传入的玩家)获得的。
获取玩家的装备组件 (EquipmentInventoryComponent): 使用 player.getComponent('equippable') 来获取玩家的装备栏组件。
获取头盔槽的物品 (ItemStack): 使用 getEquipment("Head") 方法从装备组件中获取头盔槽 ("Head") 的物品堆叠 (ItemStack)。
检测物品是否存在: 检查获取到的 ItemStack 是否为空 (undefined)。如果返回 undefined,则表示该槽位没有物品;否则,表示有物品。
import { world, EquipmentSlot, ItemStack } from "@minecraft/server";

// 假设你已经有了一个 player 对象
// const player = ...;

function checkPlayerHasHelmet(player) {
try {
// 1. 获取玩家的装备栏组件
const equipmentComponent = player.getComponent('equippable');

if (!equipmentComponent) {
console.warn(`Player ${player.name} does not have an equippable component! This should not happen.`);
return false; // 或者根据需要处理
}

// 2. 获取头盔槽的物品
// 注意:使用 EquipmentSlot.Head 或字符串 "Head"
const helmetItem = equipmentComponent.getEquipment(EquipmentSlot.Head);
// 或者也可以用字符串: const helmetItem = equipmentComponent.getEquipment("Head");

// 3. 检测物品是否存在
if (helmetItem !== undefined) {
// 头盔槽有物品
console.log(`${player.name} is wearing a ${helmetItem.typeId} on their head.`);

// (可选) 你还可以进一步检查物品的类型
// 例如,检查是否是铁头盔
// if (helmetItem.typeId === "minecraft:iron_helmet") {
// console.log(`${player.name} is wearing an iron helmet.`);
// }
return true;
} else {
// 头盔槽没有物品
console.log(`${player.name} is not wearing anything on their head.`);
return false;
}
} catch (error) {
console.error(`Error checking helmet for player ${player.name}: ${error}`);
return false; // 或根据需要处理错误
}
}

// --- 示例:在 Tick 事件中检查 ---
// world.events.tick.subscribe(() => {
// // 遍历所有在线玩家
// for (const player of world.getAllPlayers()) {
// checkPlayerHasHelmet(player);
// // 你的其他逻辑...
// }
// });

// --- 示例:假设你有一个特定的 player 对象 ---
// world.afterEvents.playerSpawn.subscribe((event) => {
// const player = event.player;
// // 玩家重生时检查一次
// checkPlayerHasHelmet(player);
// });
关键点:
组件名称: 玩家的装备栏组件是 'equippable'。
方法名称: 获取装备的方法是 .getEquipment(slot)。
槽位标识: 头盔槽使用 EquipmentSlot.Head (推荐) 或字符串 "Head"。
空值判断: 槽位没有物品时,getEquipment 返回 undefined。这是判断的关键。
这样,你就可以在你的 Minecraft 服务器脚本中检测玩家是否佩戴了头盔了。
 
world.events.tick.subscribe并不存在(以前有,但所提及版本已废弃),正确做法是注册worldLoad事件再利用System:
JavaScript:
function handler() {
    // 从 World 取得所有玩家
    const players = world.getAllPlayers();
    player.forEach((player) => {
        // Do something...
    });
}

// 注意: 所提及版本为1.17.0,将worldLoad换成worldInitialize即可(仍无world.events.tick)
// ScriptV2迁移: https://learn.microsoft.com/en-us/minecraft/creator/documents/scripting/v2-overview?view=minecraft-bedrock-experimental
world.afterEvents.worldLoad.subscribe(() => {
    // 用 System 注册回调
    system.runInterval(handler);
});
另外EquipmentSlot是TS接口,除非你用TS写脚本再编译,否则不应当使用(TS的interface在编译成JS时会删除,在特定函数用动态类型检查代替),所以在SAPI的QuickJs语境下是import不到EquipmentSlot的。
 
解决方案

在线会员

  • WR无若
  • luotj121
  • mcmanzi
  • sszj
  • crm
  • wmj16
  • milktea0723
  • 夜刀神756
  • C ZZ
  • 菖蒲泽漆
  • zky什么地点?
  • xbyyds
  • Mandysa
  • Klinsky6
  • auhuys
后退
顶部 底部