Minecraft – Right click detection on a block (No item on hand) Minecraft

minecraft-commandsminecraft-java-edition

How can I modify this code to detect a right click when right-clicking a specific block only, without any item in a player hand? This is not like others, for I am not using an Item or I am not right-clicking any object. Just a specific one like a torch, to act like a button. I can use scorebords with this though.

/scoreboard players set @e[type=ArmorStand,name=Activated] action 1 {Equipment:[{},{},{},{},{}]}
/scoreboard players set @e[type=ArmorStand,name=Activated] action 0 {Equipment:[{id:"minecraft:carpet"},{},{­},{},{}]}
/execute @e[name=Activated,score_action_min=1] ~ ~ ~ /setblock XYZ-Coordinates redstone_block
/execute @e[name=Activated,score_action=0] ~ ~ ~ /setblock Same XYZ-Coordinates air

Best Answer

You can do this using a villager with no trades and a stat.talkedToVillager objective. This is because a villager without trades doesn't show the trade interface at all.

Summon an invisible ArmorStand with an invisible, silent Villager as it's passenger, like this:

/summon ArmorStand <x> <y> <z> {
  Invisible:1b,
  Marker:1b,
  NoGravity:1b,
  Passengers:[{
    id:Villager,
    Silent:1b,
    NoAI:1b,
    Invulnerable:1b,
    Career:1,
    CareerLevel:6,
    Offers:{Recipes:[]},
    ActiveEffects:[{
      Id:14b,
      Amplifier:0b,
      ShowParticles:0b,
      Duration:2147483647
    }]
  }]
}

Copy-paste version of the command:

/summon ArmorStand <x> <y> <z> {Invisible:1b,Marker:1b,NoGravity:1b,Passengers:[{id:Villager,Silent:1b,NoAI:1b,Invulnerable:1b,Career:1,CareerLevel:6,Offers:{Recipes:[]},ActiveEffects:[{Id:14b,Amplifier:0b,ShowParticles:0b,Duration:2147483647}]}]}

The armor stand serves purely as a way to hold the villager in place. Play around with <x> <y> <z> until you find a good position for the villager. I suggest temporarily removing the villager's invisibility effect for "alignment" by replacing 2147483647 with 0.

Now, create your objective

/scoreboard objectives add torchLever stat.talkedToVillager

Use a repeat/chain command block line to run the following commands:

/execute @a[x=X,y=Y,z=Z,r=R,score_torchLever_min=1] ~ ~ ~ <your command>
/scoreboard players set @a torchLever 0

Replace X, Y, Z and R with appropriate values. This is to prevent triggering your torch lever when interacting with any villagers.