Minecraft – Is it possible to use an item to activate a command in Minecraft

minecraft-commandsminecraft-java-edition

Is it possible to detect when a player right-clicks with a stick and activate a command?

What I'm trying to do is have several items with specific NBT data and set it so that right-clicking with those items uses certain commands (for instance, right-clicking with a specific sword will /summon a fireball, or right-clicking with a specific stick will activate a speed /effect). Is there a way to do this in vanilla Minecraft?

Best Answer

dly's answer is fine, but DonielF is right. If you have a mob in you you can't hit anything.

In Minecraft you can't right click and trigger a command with all items. But some items (like carrot on a stick) can be used.

Example:

Type this in chat:

/scoreboard objectives add speed stat.useItem.minecraft.carrot_on_a_stick speed
/scoreboard objectives setdisplay sidebar speed


Run this on a 1 tick clock:
    /effect @p[score_speed_min=1,score_speed=1] minecraft:speed 30 1

This will detect whenever a player right-clicks with a carrot on a stick and it will give him the effect speed 1 for 30 seconds.

It will also work with an empty map (make sure to remove the new map and give the player another empty map!) or when a placed cake is eaten.