top of page

Right Click Detection

This tutorial I will show how to make basic right click detection. If you don't know the basics of command blocks go to this tutorial and come back. There is a button that will take you to back to this page don't worry.

Ok before we start I'll link a command generator below because I know you might want to have a lot more custom items and I don't have time to go over enchant nbt and custom tags and stuff so just go there if you want more than right clicking a dirt to get a diamond.

Before we start off I will be doing these commands out of order so just read through until the end before you start placing blocks. The italics following can be ignored if you are going to the advanced version after (which i recommend, it is at the bottom of the page as it leads off from the end of this one)
If you don't proceed to the advanced version there will be lots of bugs. Scrolling, moving items in GUIs and dropping items, or anything that would take the item out of your inventory without right clicking will end up giving you the item even though you still have it. It also will not work in creative, mods that replace your item or with stacked items or anything else that may cause your item to not disappear from your slot even though it physically disappeared. This will not give you the item.

Ok so lets start off with tagging everyone that has our item. tag @e[tag=Dirt] remove Dirt is the first command. This removes all dirt tags so that when you only have the tag if in this tick you are holding dirt which is what we want. Next is tag @e[type=player,nbt={SelectedItem:{id:"minecraft:dirt"}}] add Dirt. This involves nbt which is complicated so if you don't understand that's fine. Personally even though I know nbt it has weird syntax so I use a generator regardless and I advise you to too.

Now we also need to see if the player had the item last tick to see if they got rid of it. We do this by adding at the end tag @e remove WasDirt and then tag @e[tag=Dirt] add WasDirt. This makes sure that if you had it last tick that you will have the tag (as it is at the end it will only affect the next tick).

Now how do you get rid of it? This is by summoning an invisible armor stand. This is even more complicated so again if you don't understand don't worry. First we will kill all the armor stands so there are only ever one per player. The command is kill @e[tag=RightClickDemo]-this is the first command before everything else. Now we need to add the summon command, we will do this in between the two tags (after Dirt, before WasDirt). execute at @e[tag=WasDirt] run summon armor_stand ~ ~ ~ {ShowArms:1b,NoGravity:1b,Invulnerable:1b,Invisible:1b,Tags:["RightClickDemo"]} is the command to do so. That looks like a lot and it is. However just stop panicking and you'll be fine. It executes at anybody who had a dirt tag last tick and it summons an armor stand at their position. The armor stand has arms (so it can pick up any item not just armor), has No Gravity so even if your falling it will be at your position not falling below you, not that important for the basic version but especially for the advanced version. I always add it in just to be safe anyway. THen is invulnerable so you don't break it, same thing as last one. Invisible so you don't see it and the tag is so we can use a selector to get it. That must have been a mouthful but your doing good. That is the most complex it'll get through out this so don't worry.

The final command, which will be just after the previous one and just before the WasDirt tag commands, is execute as @e[tag=!Dirt,tag=WasDirt] run give @s diamond. This says if you no longer have dirt get a diamond. If you want to have any other features besides getting a diamond change the command after run to whatever you want, if you need more than one command block then copy and paste it but make sure it is still after the last command and before the WasDirt tag commands.

If you want the more advanced version which works on certain activities like scrolling in gui and such then that will be below.

bottom of page