Testing for an item in inventory with custom name

How do i test for a item with a name in a players inventory? Like an identity card for each player that has to verify correctly to open the doors.


The following will test for a piece of paper renamed to "Card" in the player's inventory:

/testfor @a {Inventory:[{id:"minecraft:paper",tag:{display:{Name:"Card"}}}]}

If you want to keep track of who has the card (to select them later), you can give players with the card a tag based on the NBT test:

/scoreboard players tag @a remove HasCard
/scoreboard players tag @a add HasCard {Inventory:[{id:"minecraft:paper",tag:{display:{Name:"Card"}}}]}

Running the above commands on a clock in that order will allow you to select people with the card with @a[tag=HasCard].


You can read about the NBT format for players and items on this wiki page:
http://minecraft.gamepedia.com/Player.dat_format