How do I setblock whatever item is currently in hand in minecraft?

I'm playing minecraft, doing some random stuff in my server.
I was creating an automatic builder, to avoid having to build blocks, so that I could just walk, and it would build.
What I did was create a pulse-clock connected to a commandblock having:
execute @p ~ ~ ~ setblock ~ ~ ~ dirt
I was wondering how I would make that "dirt" into whatever item I was holding in hand, or at a specific slot in my inventory as setblock ~ ~ ~ {slot:0b}
doesn't seem to work.
--
On another note, how can I make the commandblock to work inside a range, and/or coordinates, as in execute @p[x=~10,y=~,z=~15,r=5] ~ ~ ~ setblock ~ ~-1 ~ dirt
Thanks in advance.
Best Answer
No, this is impossible unless you want to add command blocks for every block type.
Pictures about "How do I setblock whatever item is currently in hand in minecraft?"



How do you detect items in offhand in Minecraft?
Use /setblock! The /setblock command in Minecraft allows the user to change a block at a specific coordinates in their current Minecraft world to the block specified. The usage and syntax of this command depends on which platform (Java or Bedrock) you are using it.How can you tell what an item a player is holding in Minecraft?
Type the command in the chat window and press the Enter key to run the command. This /fill command would replace all blocks in the fill region, including air. The replaced blocks will be dropped and can then be collected as if they were mined with a pickaxe or shovel.Using Commands in Minecraft: How to Use /setblock \u0026 /fill to Alter Terrain! | 1.11.2
More answers regarding how do I setblock whatever item is currently in hand in minecraft?
Answer 2
You would have to add a command blocks comparator setup for each block, which is already a pain to do for just one item. To have the command block work inside of a range, use the fill command instead, ex
/fill ~1 ~-1 ~1 ~-1 ~-1 ~-1 dirt
will give a 3x3 area of dirt beneath you.
Sources: Stack Exchange - This article follows the attribution requirements of Stack Exchange and is licensed under CC BY-SA 3.0.
Images: Rachel Claire, Karolina Grabowska, Karolina Grabowska, Erik Mclean