Minecraft – Setblock at a Player’s Location with Command Blocks

minecraft-java-edition

I want to make it so you place a dispenser at the nearest player's location (if the nearest player is not available, than I could just use me). I can't just make the coordinates ~ ~ ~ because I'm using this command from a command block, and that would just place the block at the location of the command block. Here is what I've got so far:

/setblock (this is where I need help) minecraft:dispenser 2 keep {Items:[{id:35, Slot:0, Count:-1,Data:0}]}

Just so you guys know, most of the command is specific to what I need to do, the basic form of this is:

/setblock (this is where I need help) minecraft:dispenser 2

Best Answer

The full command you want should be this:

/execute @p ~ ~ ~ /setblock ~ ~ ~ minecraft:dispenser 2 keep {Items:[{id:35, Slot:0, Count:-1,Data:0}]}

The "execute" command takes a selector, a set of coordinates, and another command. It runs that other command as if the selected entity was running it. For example:

/execute @p ~ ~ ~ /say hello

Would make the closest player say "hello", even if a command block or another player is actually running the command above.

Related Topic