Minecraft 1.12 – execute entity when clicked on while empty handed

minecraft-commandsminecraft-java-edition

I've seen this done before, and even though I am highly proficient in commands, it seems this is the only thing I can't quite figure out.

When I click on an empty armor stand while my hands are empty, I want it to execute a command on the clicked-on entity. For example, I click on an entity (armor stand, villager, etc.) and lightning is summoned at the entity, or it tags the nearest sheep with a test tag.

Best Answer

To check hitting on a specific spot, you can put a "deactivated" mob at that location. To least affect the environment and have the same size as a player/standard mob, I recommend a skeleton:

/summon skeleton ~ ~ ~ {NoAI:1,NoGravity:1,Silent:1,ActiveEffects:{Id:14,Amplifier:1,Duration:2147483647,ShowParticles:0}}

To also make it not collide with the player, you can assign it to the same team as the player and then do:

/scoreboard teams option <team name> collisionRule pushOtherTeams

But if you are already using teams otherwise in your map, that might conflict with it.