/replaceitem
原文:Commands/replaceitem;源修订:3569480。
本命令为基岩版和教育版独有,需要开启作弊;权限等级为 1。在 Java 版 1.17 中,本命令已由 /item 取代。
将方块(如箱子、熔炉)或实体(玩家或生物)物品栏指定槽位中的物品替换为指定物品。
语法
replaceitem block <position: x y z> slot.container <slotId: int> <itemName: Item> [amount: int] [data: int] [components: json]
replaceitem entity <target: target> <slotType: EntityEquipmentSlot> <slotId: int> <itemName: Item> [amount: int] [data: int] [components: json]
replaceitem block <position: x y z> slot.container <slotId: int> <oldItemHandling: ReplaceMode> <itemName: Item> [amount: int] [data: int] [components: json]
replaceitem entity <target: target> <slotType: EntityEquipmentSlot> <slotId: int> <oldItemHandling: ReplaceMode> <itemName: Item> [amount: int] [data: int] [components: json]
参数
position: x y z:要修改的方块位置,类型为CommandPosition。须为由空格分隔的三个坐标,可使用波浪号或插入符号表示相对坐标。target: target:要修改的一个或多个实体,类型为CommandSelector<Actor>。可以使用玩家名称或目标选择器。slotType: EntityEquipmentSlot、slot.container和slotId: int:要修改的物品栏槽位。有效值取决于目标为方块还是实体,详见槽位。可以使用slot.enderchest指定玩家的末影箱。slotId: int须为 -2147483648 至 2147483647(含端点)的 32 位整数。itemName: Item:放入方块或实体物品栏槽位的物品,为物品枚举值。amount: int:放入指定槽位的物品数量。须为 32 位整数,应介于 1 至 64(含端点)。data: int:物品的数据值。对指定物品 ID 无效的值按 0 处理;省略时为 0。须为 -2147483648 至 2147483647(含端点)的 32 位整数。components: json:指定物品组件,类型为Json::Value,须为有效的 JSON 对象。类似数据标签,但仅支持minecraft:can_place_on、minecraft:can_destroy、minecraft:item_lock和minecraft:keep_on_death功能,参见 /give 的示例。oldItemHandling: ReplaceMode:必须为destroy或keep。destroy直接替换指定槽位中的物品,不考虑原物品;keep在槽位已被物品占用时不替换。
结果
| 条件 | 结果 |
|---|---|
| 命令不完整或任一参数指定不正确 | 无法解析 |
amount: int 小于 1 或大于 64 |
失败 |
block 模式下,指定方块不是容器或不存在指定槽位 |
失败 |
entity 模式下,目标无法解析为一个或多个实体(指定名称的玩家须在线) |
失败 |
entity 模式下,所有选中实体均不存在指定槽位(例如僵尸没有 horse.armor),或物品不能放入其指定槽位(例如石头不能放入玩家的 armor.chest) |
失败 |
keep 模式下,指定槽位已有物品 |
失败 |
| 其他情况 | 成功 |
输出
失败时成功次数为 0;block 模式成功时为 1;entity 模式成功时为物品被成功替换的实体数量。
示例
将上方两格处单个箱子右下角槽位的物品替换为 4 棵云杉树苗:
replaceitem block ~ ~2 ~ slot.container 26 sapling 4 1
将最近玩家快捷栏最右侧槽位的物品替换为 4 棵云杉树苗:
replaceitem entity @p slot.hotbar 8 sapling 4 1
清空所有玩家末影箱的第一个槽位:
replaceitem entity @a slot.enderchest 0 air
另见
- /item:Java 版中的对应命令。
- /give:给予玩家物品,无需指定槽位,也不会覆盖其他物品。
- /setfixedinvslot(已过时):可以替换生物物品栏中的槽位。
外部链接
Microsoft Learn:/replaceitem 命令(2023 年 10 月 11 日)。
来源为 Minecraft Wiki;本中文翻译/改编按 CC BY-NC-SA 3.0 共享。