/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?
Best Answer
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]
Pictures about "/testfor for item enchantment"



How do you check enchantments in Minecraft?
On the left you can drop in a number of lapis lazuli and the item you want to enchant, then on the right you'll see some scripture that signals your choice of randomly rolled enchantments \u2013 hover over one of the glyphs and will give you a clue as to one of the possible enchantments.Is there a TestFor command in Minecraft Java?
TestFor Command in Minecraft Java Edition (PC/Mac) It is a data tag to test for, as it relates to player. It is a compound NBT tag such as {playerGameType:0}, {foodLevel:20} or {XpLevel:20}. (See NBT tags for player.)What is the ID of sharpness?
Enchanting/IDEnchantmentNameID (PC)Sharpnesssharpness16Smitesmite17Bane of Arthropodsbane_of_arthropods18Knockbackknockback1926 more rowsBest Enchantments For All Armor and Items in Minecraft
Sources: Stack Exchange - This article follows the attribution requirements of Stack Exchange and is licensed under CC BY-SA 3.0.
Images: Karolina Grabowska, Anete Lusina, Andrea Piacquadio, Anete Lusina
