Setblock at a Player's Location with Command Blocks
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}]}
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.
Pictures about "Setblock at a Player's Location with Command Blocks"



How do you find the position of a player in Minecraft?
Enter /position in Minecraft to see your current world position in the top left corner of Minecraft. You can also use the player world position block in your code. This way, no matter where you are in the game world, your code can use your position to do things near you.How do you find player coordinates in Minecraft with commands?
Also, you can't get their exact coordinates. If you are targeting a specific player in a command, simply type their name on the <Player> tag or do @p for nearest player, @r for random player or @a for all players.How do you use command blocks with Setblock?
How to Enter the CommandHow to make blocks under the player command minecraft
Sources: Stack Exchange - This article follows the attribution requirements of Stack Exchange and is licensed under CC BY-SA 3.0.
Images: Maria Orlova, Maria Orlova, Olga Lioncat, Maria Orlova
