PT-BR EN ES
Channel Icon

Crazy Ozz

Official Channel Website

Command Block

How to Detect Items in Main Hand in Minecraft (Command Block)

Minecraft Java Edition 1.21+ and 26+ – Detect Items by ID, Name, and custom_data

In this tutorial, you will learn how to detect items in a player's main hand in Minecraft using command blocks. This method works in Minecraft Java Edition 1.21+ and 26+ and uses the execute if items command to check if a player is holding a specific item. You can detect items by ID, custom name (custom_name), or custom_data tags, making it perfect for adventure maps, servers, minigames, and automatic systems.

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"}]']

If it doesn't work, use:

give @a iron_sword[custom_name='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"}]']

Same thing, if it still doesn't work, change it to:

execute if items entity @p weapon.mainhand minecraft:iron_sword[custom_name='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.

Want cheap and lag-free Minecraft hosting?

I recommend Sparked Host: affordable plans, great performance, easy panel and fast support.

🚀 Start your Minecraft Server

Still have questions? Watch the video below