How can you test if a player is holding an item with a specific name in Minecraft? [duplicate]

How can you test if a player is holding an item with a specific name in Minecraft? [duplicate] - Student Cheating During an Exam

I don't know how to test if a player is holding an item with a specific name, but I know it's something like /testfor @e[type=Player,SelectedItem=example_id,Slot=0], but I probably got it totally wrong... Can you guys help?



Best Answer

A list of valid target selector parameters can be found here.

/testfor syntax:

/testfor <selector> {dataTags}

You'll be checking the player's NBT data instead. The SelectedItem compound holds a copy of the player's currently-held item, minus the Slot tag:

/testfor @e[type=Player] {SelectedItem:{id:"minecraft:stone",tag:{display:{Name:"Custom Name"}}}}

Or if you need to target the matching player afterwards, you'll assign a score.

Objective prerequisite:

/scoreboard objectives add HoldingItem dummy

Detection:

/scoreboard players set @e[type=Player,score_HoldingItem_min=1] HoldingItem 0
/scoreboard players set @e[type=Player] HoldingItem 1 {SelectedItem:{id:"minecraft:stone",tag:{display:{Name:"Custom Name"}}}}
/say @a[score_HoldingItem_min=1]



Pictures about "How can you test if a player is holding an item with a specific name in Minecraft? [duplicate]"

How can you test if a player is holding an item with a specific name in Minecraft? [duplicate] - Student Cheating During an Exam
How can you test if a player is holding an item with a specific name in Minecraft? [duplicate] - Student Cheating During an Exam
How can you test if a player is holding an item with a specific name in Minecraft? [duplicate] - Student Cheating During an Exam





[1.17] How to Detect If a Player Is Holding a Named Item




Sources: Stack Exchange - This article follows the attribution requirements of Stack Exchange and is licensed under CC BY-SA 3.0.

Images: RODNAE Productions, RODNAE Productions, RODNAE Productions, RODNAE Productions