When accessing pre-existing NBT data, you have to specify it exactly as it's stored. This includes the name, value, and the datatype. The Slot
tag is stored as a Byte, while you've declared it as an Integer. You can append the numerical value with a "b" to declare it as a Byte:
/testfor @a {Inventory:[{Slot:0b,id:"minecraft:wooden_button"}]}
To mention, while the command will output whether or any there is any player at all with a wooden button in that slot, it does not allow you to target that player afterwards.
You can instead assign a label based on the player's NBT data and then target them based on that label:
1.8:
Objective required.
/scoreboard objectives add HasItem dummy
Detection commands.
/scoreboard players set @a HasItem 0
/scoreboard players set @a HasItem 1 {Inventory:[{Slot:0b,id:"minecraft:wooden_button"}]}
/say @a[score_HasItem_min=1]
1.9:
No objective needed, only detection commands.
/scoreboard players tag @a[tag=HasItem] remove HasItem
/scoreboard players tag @a[tag=!HasItem] add HasItem {Inventory:[{Slot:0b,id:"minecraft:wooden_button"}]}
/say @a[tag=HasItem]
The number that we're interested in is the 2 in the /give
command. This is the data value for the map. This corresponds to the Damage:
field of the NBT tag. Damage is a short, so you must use 2s
to indicate a damage value of 2. So the proper NBT tag for the item is {id:minecraft:filled_map,Damage:2s}
. Therefore, the final command becomes as follows:
/testfor @p {Inventory:[{id:"minecraft:filled_map",Damage:2s}]}
That said, /give @p filled_map 1 2
doesn't necessarily work in the way you would expect: the damage value 2 will only apply if it has already been created. Otherwise, it'll give the player a map with value 0, then 1. Use F3+H to verify the actual data value of the map that you have. This won't be a problem after the map has been generated.
Best Answer
You don't need
SelectedItemSlot
when you can just useSelectedItem
! Also probably because you were testing for an item named "key", not "Key2A".