PT-BR EN
Channel Icon

Crazy Ozz

Official Channel Website

Command Block

How to Identify/Detect Items by Name or Tag in the Main Hand in Minecraft

Version 1.21.4 Java Edition

Let’s start by detecting the item through its tag, but first, let’s get an iron sword to use as an example.

give @a iron_sword[custom_data={Teste:1}]

Choose a tag using custom_data, then place a Repeat command block set to Always Active with the following command:

execute if items entity @p weapon.mainhand minecraft:iron_sword[custom_data={Teste:1}]

Basically, this will detect whether the nearest player to the command block is holding an iron sword with the tag Teste:1b in their main hand.

The idea is the same for detecting by name. Let’s once again give the player a sword, but this time with a custom name:

give @a iron_sword[custom_name='[{"text":"Teste"}]']

Then place this command in a Repeat command block set to Always Active:

execute if items entity @p weapon.mainhand minecraft:iron_sword[custom_name='[{"text":"Teste"}]']

If you’re working in multiplayer, use execute as @a run at the beginning of the command to apply it to all players,

and replace @p with @s to target the player executing the command.

Still have questions? Watch the video below