How do I test for an item with a name in a player's inventory? I want to make a working command that checks that the player has an identity card with a specific name to open doors.
-
2This question is similar to: Clear item with custom name?. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem.– ppperyCommented Oct 24 at 15:29
-
@pppery Does the /clear command select items? It doesn't seem like a straightforward duplicate..– JoachimCommented Oct 24 at 16:45
-
1@Joachim It's the same concept (find a custom name in data tags) and the answers here are outdated. That's good enough for me.– ppperyCommented Oct 24 at 17:52
-
For findability reasons I'll vote against, but note once more that I know little about Minecraft commands (only really what I've learned here :).– JoachimCommented Oct 25 at 8:12
1 Answer
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:
https://minecraft.wiki/w/Player.dat_format