Minecraft spawning items with inventories in them

minecraft-commandsminecraft-java-edition

I was wondering, is there a way to spawn a dispenser that is filled with snowballs using a command? Like, i want to be able to /fill over a row of dispensers with full dispensers… so how would I specify the inventory of the spawned block in the command?

Best Answer

Please see

Minecraft Empty Chests Command Block

as I think this answers you question.

Let's say for example your equipment chest is supposed to contain a notch apple, a gold sword, a bow, and a stack of arrows. The command for that would be /blockdata X Y Z {Items:[{id:322,Damage:1,Count:1,Slot:0},{id:283,Count:1,Slot:1},{id:261,Count:1,Slot:2},{id:262,Count:64,Slot:3}]}, replacing X Y and Z as appropriate. There's a lot of stuff going on in that command, but essentially, you are changing the data of the chest to contain the specified items. It's the same concept as using /setblock to spawn a new chest with these items, except instead of replacing the chest, you're overwriting its data.