Minecraft – How to detect a block in Minecraft from all sides using /execute and detect

minecraft-commandsminecraft-java-edition

I've recently added some new commands into a .mcfunction, since 1.12 is now out. What I wanted was that if the player ran directly into a block, it destroys it, now that works with this command:

execute @a ~ ~ ~ detect ~1 ~ ~ minecraft:wool 7 setblock ~1 ~ ~ minecraft:air 0 destroy

But my only problem, is that it only works from 1 side, not all 4 sides of the block. Can anyone help me out? I really only want it to work from the front, left, right and back sides.

Example: (Sorry it's an MS Paint Example. But it's the only way I can kind of explain, the arrows represent which way the player will run into the block.)

Thanks,
Daniel

Best Answer

To detect a block from all sides, you have to apply all test cases; basically, four commands blocks; one for each side and have them chainned.

execute @a ~ ~ ~ detect ~1 ~ ~ minecraft:wool 7 setblock ~1 ~ ~ minecraft:air 0 destroy
execute @a ~ ~ ~ detect ~-1 ~ ~ minecraft:wool 7 setblock ~-1 ~ ~ minecraft:air 0 destroy
execute @a ~ ~ ~ detect ~ ~ ~1 minecraft:wool 7 setblock ~ ~ ~1 minecraft:air 0 destroy
execute @a ~ ~ ~ detect ~ ~ ~-1 minecraft:wool 7 setblock ~ ~ ~-1 minecraft:air 0 destroy