2
Hello! I am making a map and I wanted there to be an item that when you hold it, it will trigger more commands. My and my friend cant seem to figure out how to do this properly. The command that we are using is as follows
/give @p minecraft:player_head{SkullOwner:Cobblestone,display:{Name:'[{"text":"Piece of the Ruin","italic":false,"color":"light_purple"}]',Lore:['[{"text":"Holding this in your hand","italic":false,"color":"gray"}]','[{"text":"while in the ruins will allow","italic":false,"color":"gray"}]','[{"text":"you to see the boss room","italic":false,"color":"gray"}]','[{"text":"through walls when near it.","italic":false,"color":"gray"}]']}}
2
Like Francielly said below, you can test for the player's inventory NBT to determine if they are holding an item.
- SelectedItem:{} is the currently held item
- Inventory:[{Slot:-106b}] is the offhand slot.
All the other item data you want to test for goes in the curly brackets. Remember that you do not need to test for every piece of data on the item, it is actually more efficient to only check a single critical datatag instead of matching every last one.
When working with the Inventory:[{}] or SelectedItem:{} datatags, remember that item NBT you would add in commands like /give is stored in a the subtag tag:{}.
A complete set of item data is like this:
(SelectedItem:){id:"minecraft:diamond_pickaxe",Count:1b,Slot:2b*,tag:{Damage:150}}
*Slot is not used in SelectedItem or entities that don't have a full inventory (like dropped items or armor stands)
- SelectedItem:{} is the currently held item
- Inventory:[{Slot:-106b}] is the offhand slot.
All the other item data you want to test for goes in the curly brackets. Remember that you do not need to test for every piece of data on the item, it is actually more efficient to only check a single critical datatag instead of matching every last one.
When working with the Inventory:[{}] or SelectedItem:{} datatags, remember that item NBT you would add in commands like /give is stored in a the subtag tag:{}.
A complete set of item data is like this:
(SelectedItem:){id:"minecraft:diamond_pickaxe",Count:1b,Slot:2b*,tag:{Damage:150}}
*Slot is not used in SelectedItem or entities that don't have a full inventory (like dropped items or armor stands)
This command will detect if the player is holding the head with either mainhand or offhand and will execute a command
Note that I'm only checking by the SkullOwner and Display→Name tags
(I didnt tested the command ingame, so it should (i'm not sure if it will, but the logic is right) work in the game)
execute unless entity @s[nbt=!{SelectedItem:{id:"minecraft:player_head",tag:{SkullOwner:Cobblestone,display:{Name:'[{"text":"Piece of the Ruin","italic":false,"color":"light_purple"}]'}}}},nbt=!{Inventory:[{id:"minecraft:player_head",Slot:-106b,tag:{SkullOwner:Cobblestone,display:{Name:'[{"text":"Piece of the Ruin","italic":false,"color":"light_purple"}]'}}}]}] <command>Note that I'm only checking by the SkullOwner and Display→Name tags
(I didnt tested the command ingame, so it should (i'm not sure if it will, but the logic is right) work in the game)
