物品与物品栏
物品栏可用玩家或实体句柄、带位置的容器方块、[x, y, z] 或界面句柄指定,也可用在线玩家名查找玩家物品栏。读取装备栏或末影箱时,把物品栏参数换成 "equipment", entity 或 "enderchest", player。
物品堆叠以 [itemId, count, nbt] 返回,空槽位为 null。返回的原版物品 ID 可能省略 minecraft:。
槽位操作
mc.inventory 下的函数 | 结果 |
|---|---|
inventory_size(inventory) | 槽位数量。该对象没有物品栏时返回 null。 |
inventory_has_items(inventory) | 判断物品栏中是否有物品。该对象没有物品栏时返回 null。 |
inventory_get(inventory[, slot]) | 返回完整的槽位数组,或根据从 0 开始的索引读取单个槽位。 |
inventory_find(inventory[, item][, startSlot]) | 返回首个匹配的槽位索引,找不到时返回 null。物品参数为 null 时查找首个空槽。 |
inventory_set(inventory, slot, count[, item][, nbt]) | 写入槽位并返回原有物品。数量为 0 时清空槽位。 |
inventory_remove(inventory, item[, amount]) | 尝试按物品类型移除指定数量,默认移除 1 个。库存不足时返回 false。 |
drop_item(inventory, slot[, amount]) | 将槽位里的物品丢到世界中并返回丢出的数量。省略数量或传入 0 会丢出整组。 |
function countStone() {
const player = mc.entities.player();
if (player === null) return 0;
return mc.inventory.inventory_get(player).reduce((total, item) => {
return total + (item !== null && item[0] === "stone" ? item[1] : 0);
}, 0);
}
物品、配方和界面
mc.inventory 下的函数 | 用途 |
|---|---|
stack_limit(item) | 读取物品的最大堆叠数。 |
item_list([tag])、item_tags([item][, tag]) | 列出物品、列出标签成员、查看物品所属标签或判断成员关系。标签 ID 不带 #。 |
recipe_data(item[, recipeType]) | 按产物查找配方,默认为合成配方类型。 |
crafting_remaining_item(item) | 取得参加合成后剩余的物品堆叠。 |
create_screen(player, type, title[, callback]) | 为在线玩家打开界面并返回句柄。 |
close_screen(screen) | 关闭已打开的界面。 |
screen_property(screen, name, ...args) | 只传属性名时读取属性,追加参数时设置属性。 |