Minecraft – Changing the NBT data of a variable amount of items in a chest inventory

minecraft-commandsminecraft-java-edition

My goal is to be able to put a variable amount of gold nuggets in a chest and then run some commands to replace those with gold nuggets that have edited display NBT data.

Is this even possible? I'm encountering a few issues.

First, when checking for the amount of gold nuggets in an inventory with /testforblock, I have to specify the amount of gold nuggets I am testing for. However, I want the amount of nuggets you input to be variable, not restricted to a certain stack size. I also want it to be able to work with every slot in the chest, so somebody can fill the whole thing up.

Second, how would I store the amount of nuggets being inputted into the chest and pass that value to /replaceitem so that I replace the correct number of gold nuggets with edited display NBT nuggets?

Best Answer

For the first problem, you don't have to specify a slot or count when testing for NBT data. Try something like this:

/testforblock X Y Z chest -1 {Items:[{id:minecraft:gold_nugget}]}

That way, the command will succeed if any gold nuggets are in any slot.


No good way to do the second problem, AFAIK. Best way would be do:

  1. Break the chest with a /setblock
  2. Identify the golden nugget items that just dropped with a scoreboard objective, E.G: /scoreboard players set @e[type=Item] isNugget 1 {Item:{id:minecraft:gold_nugget}}

  3. Change the NBT data of the dropped nuggets: /entitydata @e[type=Item,score_IsNugget_min=1] {Item:{tag:{display:{Name:"NUGGET"}}}}

  4. Use a (minecart) hopper to scoop the items back up into a chest.