ruhuasiyu
本帖最后由 ruhuasiyu 于 2019-6-20 13:42 编辑

命令loot replace 可用于动态替换实体/方块物品,是个十分便利的命令。然而可能很多人不知道,该命令是可以一次性替换多个槽位物品的。
  1. loot replace entity @s 槽位 <source>
  2. loot replace block x y z 槽位 <source>
复制代码
执行该命令之后,
(1)首先会根据掉落品表确定将要掉落的物品序列,这些物品可能会相同。如果是潜影箱 "type":"dynamic"得到的掉落物,第X个就是第X个槽位的物品,即使该位置为空。但是通常的战利品表会忽视那些没有掉落物的项。

(2)根据liach查看源代码(见2L)得到结论(或见英文版wiki),确定槽位对应的编号,然后从其开始的编号从小到大依次排列构成将要放入的槽位的序列。
槽位
编号
容器container.0~container.530~53
快捷栏hotbar.0~hotbar.80~8
背包inventory.0~inventory.269+0~26,即9~35
主手weapon或weapon.mainhand98
副手weapon.offhand99
靴子栏armor.feet100
护腿栏armor.legs101
胸甲栏armor.chest102
头盔栏armor.head103
末影箱enderchest.0~enderchest.26200+0~26,即200~226
村民背包villager.0~villager.7300+0~7,即300~307
马、驴、骡的鞍horse.saddle400
马的马铠、羊驼的地毯horse.armor401
驴、骡、羊驼的箱子horse.chest499
驴、骡、羊驼的背包horse.0~horse.14500+0~14,即500~514
注意到,无论是玩家还是容器,选择槽位container.0和hotbar.0是一样的,实际效果是相同的。

(3)然后确定槽位数量。玩家可以在命令中指定槽位数量
  1. loot replace entity @s 槽位 槽位数量 <source>
  2. loot replace block x y z 槽位 槽位数量 <source>
复制代码
如果未指定槽位数量,替换的槽位数量为战利品表的物品序列长度,对于潜影箱 "type":"dynamic"而言,固定是27个槽位。

(4)最后将物品序列塞入槽位序列。


例如使用
  1. loot replace entity @s weapon.mainhand mine x y z diamond_pickaxe{isShulkerMarker:1b}
复制代码
挖掘修改过战利品表的潜影盒时,
主手塞入潜影盒内第1个掉落物,若空则清空该位置
副手塞入潜影盒内第2个掉落物,若空则清空该位置
靴子塞入潜影盒内第3个掉落物品,若空或非法则清空该位置
护腿塞入潜影盒内第4个掉落物品,若空或非法则清空该位置
胸甲塞入潜影盒内第5个掉落物品,若空或非法则清空该位置
头盔塞入潜影盒内第6个掉落物品,若空或非法则清空该位置

例如使用
  1. loot replace entity @s hotbar.0 loot 战利品表

  2. loot replace entity @s container.0 loot 战利品表
复制代码
快捷栏塞入第1-9个物品
背包塞入第10-36个物品
主手、副手、靴子、护腿、胸甲、头盔塞入第99-104个物品
末影箱塞入第201-227个物品

下面这个例子中,我们可以将玩家手部的一个物品转移到头部,类似于插件/hat指令。
  1. setblock ~ 255 ~ shulker_box{Items:[{Slot:0b,id:"minecraft:firework_star",Count:1b}]}
  2. data modify block ~ 255 ~ Items[0].id set from entity @s SelectedItem.id
  3. data modify block ~ 255 ~ Items[0].tag set from entity @s SelectedItem.tag
  4. loot replace entity @s armor.head 1 mine ~ 255 ~ diamond_pickaxe{isShulkerMarker:1b}

  5. execute store result score #temp sivalue run data get entity @s SelectedItem.Count
  6. execute store result block ~ 255 ~ Items[0].Count byte 1 run scoreboard players remove #temp sivalue 1
  7. loot replace entity @s weapon.mainhand 1 mine ~ 255 ~ diamond_pickaxe{isShulkerMarker:1b}

  8. setblock ~ 255 ~ air
复制代码
\data\minecraft\loot_tables\blocks\shulker_box.json
  1. {
  2.     "type": "minecraft:block",
  3.     "pools": [
  4.         {
  5.             "rolls": 1,
  6.             "entries": [
  7.                 {
  8.                     "type": "minecraft:alternatives",
  9.                     "children": [
  10.                         {
  11.                             "type": "minecraft:dynamic",
  12.                             "name": "minecraft:contents",
  13.                             "conditions": [
  14.                                 {
  15.                                     "condition": "minecraft:match_tool",
  16.                                     "predicate": {
  17.                                         "nbt":"{isShulkerMarker:1b}"
  18.                                     }
  19.                                 }
  20.                             ]
  21.                         },
  22.                         {
  23.                             "type": "minecraft:item",
  24.                             "functions": [
  25.                                 {
  26.                                     "function": "minecraft:copy_name",
  27.                                     "source": "block_entity"
  28.                                 },
  29.                                 {
  30.                                     "function": "minecraft:copy_nbt",
  31.                                     "source": "block_entity",
  32.                                     "ops": [
  33.                                         {
  34.                                             "source": "Lock",
  35.                                             "target": "BlockEntityTag.Lock",
  36.                                             "op": "replace"
  37.                                         },
  38.                                         {
  39.                                             "source": "LootTable",
  40.                                             "target": "BlockEntityTag.LootTable",
  41.                                             "op": "replace"
  42.                                         },
  43.                                         {
  44.                                             "source": "LootTableSeed",
  45.                                             "target": "BlockEntityTag.LootTableSeed",
  46.                                             "op": "replace"
  47.                                         }
  48.                                     ]
  49.                                 },
  50.                                 {
  51.                                     "function": "minecraft:set_contents",
  52.                                     "entries": [
  53.                                         {
  54.                                             "type": "minecraft:dynamic",
  55.                                             "name": "minecraft:contents"
  56.                                         }
  57.                                     ]
  58.                                 }
  59.                             ],
  60.                             "name": "minecraft:shulker_box"
  61.                         }
  62.                     ]
  63.                 }
  64.             ]
  65.         }
  66.     ]
  67. }
复制代码

来自群组: The Command's Power

chyx
太长慢慢去看
我先要说
bugs.mojang.com
等着你

缇米莉娅
遇事不决,量子力......走错片场,打扰了
大晚上为什么要发这种看得眼睛疼的东西

小矿工ing
期待,回头试试。
真的看着眼睛疼。。。

NoName德里奇
遇事不决,量子力学.jpg
所以大佬的原版hat是怎么实现的?

ruhuasiyu
NoName德里奇 发表于 2019-6-19 10:53
遇事不决,量子力学.jpg
所以大佬的原版hat是怎么实现的?

下面列出了函数,以及对潜影盒战利品表的修改

liach
搞晕了半天才发现这并不是bug,只是working as intended……

几个小细节:


看样子是时候详细写写这个了 https://minecraft.gamepedia.com/Commands#minecraft:item_slot


  1.     private static final Map<String, Integer> slotNamesToSlotCommandId = SystemUtil.consume(Maps.newHashMap(), hashMap_1 -> {
  2.         for (int int_1 = 0; int_1 < 54; ++int_1) {
  3.             hashMap_1.put("container." + int_1, int_1);
  4.         }
  5.         for (int int_2 = 0; int_2 < 9; ++int_2) {
  6.             hashMap_1.put("hotbar." + int_2, int_2);
  7.         }
  8.         for (int int_3 = 0; int_3 < 27; ++int_3) {
  9.             hashMap_1.put("inventory." + int_3, 9 + int_3);
  10.         }
  11.         for (int int_4 = 0; int_4 < 27; ++int_4) {
  12.             hashMap_1.put("enderchest." + int_4, 200 + int_4);
  13.         }
  14.         for (int int_5 = 0; int_5 < 8; ++int_5) {
  15.             hashMap_1.put("villager." + int_5, 300 + int_5);
  16.         }
  17.         for (int int_6 = 0; int_6 < 15; ++int_6) {
  18.             hashMap_1.put("horse." + int_6, 500 + int_6);
  19.         }
  20.         hashMap_1.put("weapon", 98);
  21.         hashMap_1.put("weapon.mainhand", 98);
  22.         hashMap_1.put("weapon.offhand", 99);
  23.         hashMap_1.put("armor.head", 100 + EquipmentSlot.HEAD.getEntitySlotId());
  24.         hashMap_1.put("armor.chest", 100 + EquipmentSlot.CHEST.getEntitySlotId());
  25.         hashMap_1.put("armor.legs", 100 + EquipmentSlot.LEGS.getEntitySlotId());
  26.         hashMap_1.put("armor.feet", 100 + EquipmentSlot.FEET.getEntitySlotId());
  27.         hashMap_1.put("horse.saddle", 400);
  28.         hashMap_1.put("horse.armor", 401);
  29.         hashMap_1.put("horse.chest", 499);
  30.     });
复制代码


这段代码就是 1.14.3 Pre 2 中反编译的 item slot 参数转整数格子编号的逻辑。
还有这个loot replace的时候是连号replace的,如果中间有个号断掉了那么那个对应的号的loot就没了。
不过号中间差那么多应该不会出现两头有中间没的现象。



hawdsf.
厉害 路过路过看不懂

wazzzzz
有点长。。。。。等收藏起来过会再看

🥶❄️☠️
loot replace强行直译是战利品重放,是这个意思吗?

第一页 上一页 下一页 最后一页