/testfor for item enchantment
What I have now is:
/testfor @a {SelectedItemSlot:0,Inventory:[{Slot:0b,id:"diamond_pickaxe"}]}
How can I use the /testfor
command to test a specific slot for a specific item that is enchanted?
All item data other than the root id
, Damage
, Count
, and Slot
tags must be placed within a single tag
compound. The ench
list holds enchantments, with id
short tag stating the numerical enchantment ID while the lvl
short tag specifies the level of enchantment.
You may be interested in the SelectedItem
compound, which holds a copy of the player's currently-held item. This way you do not need to check for every hotbar slot for an item.
For example, the following looks for a diamond pickaxe with Sharpness of any level:
/testfor @a {SelectedItem:{id:"minecraft:diamond_pickaxe",tag:{ench:[{id:16s}]}}}
The following looks for Sharpness 1:
/testfor @a {SelectedItem:{id:"minecraft:diamond_pickaxe",tag:{ench:[{id:16s,lvl:1s}]}}}
As a side-note, if you intend to target the player(s) who match the data, you do not want to use /testfor
, because after detecting a matching player there is nothing that tells you who to target. You will want to use a command like /scoreboard
instead, which allows you to assign a label to entities that match the specified NBT data.
Example using 1.9's "tags" feature:
/scoreboard players tag @a[tag=HoldingItem] remove HoldingItem
/scoreboard players tag @a[tag=!HoldingItem] add HoldingItem {SelectedItem:{id:"minecraft:diamond_pickaxe",tag:{ench:[{id:16s,lvl:1s}]}}}
/say @a[tag=HoldingItem]