4

I have the following command:

execute as @a
[nbt={SelectedItem:{id:"minecraft:snowball",components:{"minecraft:custom_data":{inv:1b}}}}] 
at @s run effect give @s minecraft:invisibility 1 0 true

It checks if the player has an item with specific custom data in their main hand. But I need to check if the item is in their secondary hand (or in any other slot), and unfortunately, my knowledge of command blocks in Minecraft version 1.21.10 does not allow me to understand why this command doesn't work.

I tried using Inventory slots and Items instead of SelectedItem, but my attempts were unsuccessful.

New contributor
CatDak is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

1 Answer 1

5

First, you can target the offhand slot by looking at equipment.offhand. It would look like this in your selector:

nbt={equipment:{offhand:{id:"minecraft:snowball",components:{"minecraft:custom_data":{inv:1b}}}}}}

Still, I recommend using a different argument of the execute command that's faster and more readable, which has the added bonus of being capable of checking multiple slots in the same command: execute if items. Your command would look like this:

execute as @a if items entity @a weapon.* snowball[custom_data={inv:1b}] run effect give @s invisibility 1 0 true

(I also removed the at @s since you don't need positioning)

There are similar arguments to weapon.* targeting any slot in the hotbar (hotbar.*) or in the entire inventory (inventory.*), and so on. You can also target specific slots like weapon.mainhand,weapon.offhand, etc.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.