能不能做到将箱子里某一物品(比如钻石)的数量储存到计分板上?
- /execute store result score @s 【计分板名称】 run data get block 【方块坐标】 Items[{id:"【那个物品的命名空间名称,例如minecraft:diamond】"}].Count
获取指定方块坐标(那个箱子)中路径为Items[{id:"【命名空间名称】"}].Count的值(箱子内满足条件“物品id为【命名空间名称】”的槽位的“数量”值),并将结果(那个数量)储存至【计分板名称】中
缺点就是如果箱子里有两个同种物品就废掉了......
将箱子中所有放了钻石的槽位筛选出来并放入一个列表,遍历列表,将数量相加。
代码:
- # 初始化
- scoreboard objectives add foo dummy
- data merge storage foo:example {Items:[]}
- # 统计
- data modify storage foo:example Items append from block ~ ~ ~ Items[{id:"minecraft:diamond"}]
- execute store result score #i foo if data storage foo:example Items[]
- scoreboard players set #sum foo 0
- execute if score #i foo matches 1.. run function foo:example
- tellraw @s [{"text":"箱子内钻石的总数为 "},{"score":{"name":"#sum","objective":"foo"}}]
- # 函数 foo:example
- execute store result score #temp foo run data get storage foo:example Items[0].Count
- scoreboard players operation #sum foo += #temp foo
- scoreboard players remove #i foo 1
- data remove storage foo:example Items[0]
- execute if score #i foo matches 1.. run function foo:example