How do I put an item into multiple containers?
I'm currently experimenting with building a TNT cannon in Minecraft 1.16.3. The idea is that a command block fills the dispensers just before shooting with 1 TNT. So far I managed found out how to give a single block to a single dispenser with the following command:
/replaceitem block ~ ~ ~1 container.0 minecraft:tnt 1
However I didn't want to create and set a new command block for every dispenser, and given that /fill
exists, I was wondering if I could modify the above command to select multiple dispensers. Perhaps something along the line of
/replaceitem block ~ ~ [~1,~7] container.0 minecraft:tnt 1
This command replaces all dispensers in a specified area with dispensers fully filled with TNT.
fill ~ ~ ~ ~ ~ ~ dispenser{Items:[{Slot:0b,id:"minecraft:tnt",Count:64b},{Slot:1b,id:"minecraft:tnt",Count:64b},{Slot:2b,id:"minecraft:tnt",Count:64b},{Slot:3b,id:"minecraft:tnt",Count:64b},{Slot:4b,id:"minecraft:tnt",Count:64b},{Slot:5b,id:"minecraft:tnt",Count:64b},{Slot:6b,id:"minecraft:tnt",Count:64b},{Slot:7b,id:"minecraft:tnt",Count:64b},{Slot:8b,id:"minecraft:tnt",Count:64b}]} replace dispenser
If you would like the dispensers to be only filled with 1 TNT, use this command:
fill ~ ~ ~ ~ ~ ~ dispenser{Items:[{Slot:0b,id:"minecraft:tnt",Count:1b}]} replace dispenser
Unfortunately, the fill command will not preserve the orientation of the preexisting dispensers. To avoid this issue, you will need 6 commands (1 for each dispenser orientation). You can optimize this yourself by removing the commands that reference dispenser orientations that your TNT cannon does not use:
fill ~ ~ ~ ~ ~ ~ dispenser[facing=north]{Items:[{Slot:0b,id:"minecraft:tnt",Count:1b}]} replace dispenser[facing=north]
fill ~ ~ ~ ~ ~ ~ dispenser[facing=east]{Items:[{Slot:0b,id:"minecraft:tnt",Count:1b}]} replace dispenser[facing=east]
fill ~ ~ ~ ~ ~ ~ dispenser[facing=south]{Items:[{Slot:0b,id:"minecraft:tnt",Count:1b}]} replace dispenser[facing=south]
fill ~ ~ ~ ~ ~ ~ dispenser[facing=west]{Items:[{Slot:0b,id:"minecraft:tnt",Count:1b}]} replace dispenser[facing=west]
fill ~ ~ ~ ~ ~ ~ dispenser[facing=up]{Items:[{Slot:0b,id:"minecraft:tnt",Count:1b}]} replace dispenser[facing=up]
fill ~ ~ ~ ~ ~ ~ dispenser[facing=down]{Items:[{Slot:0b,id:"minecraft:tnt",Count:1b}]} replace dispenser[facing=down]