Minecraft. Testfor command syntax

minecraft-commandsminecraft-java-editionminecraft-java-edition-server

For example I want find a player with a book that have text: abcdefg
Where should I put NBT data?

/testfor <player> {Inventory:[{id:book_and_quill, damage:<data>s}]}

This is everything I know about detecting something in inventory.

Best Answer

Page data is saved in the pages list. All item data that isn't the root id, Damage, Count, or Slot tags, must be placed within a single tag compound.

As well, when testing for pre-existing data, you must test for it exactly as it's saved. Item IDs are saved with the namespace (defaulting to "minecraft"), so you have to specify that. The correct ID for a book an quill is "minecraft:writable_book". All tags are case-sensitive, so it would be "Damage" rather than "damage", though this value is not used with that item so does not need to be specified.

/testfor @a {Inventory:[{id:"minecraft:writable_book",tag:{pages:["abcdefg"]}}]}

Keep in mind that you cannot specify which page in the book to look at; it will instead look through every element on the target list for a match.