Using functions and 1.14, I tried testing for a book and quill with nothing typed inside,
tag @a[tag=cleanbook] remove cleanbook ^ resetting the tag since it's an infinite looping function.
tag @a[tag=!cleanbook,nbt={Inventory:[{id:"minecraft:writable_book",tag:{pages:[""]}}]}] add cleanbook So that when a player has a clean book, something happens, for example...
execute as @a[tag=cleanbook] run say @a =Clean book= This makes it so that, when a player has an empty book, "=Clean book=" will be sent in chat, when having a book with anything typed inside it, "=Clean book=" will stop. It works perfectly, until...
When holding the book with cursor, no matter what's inside, it won't be updated and the player will never get a "cleanbook" tag (no chat messages at the left). What can I do to take into account a book being held that way, so that the "cleanbook" tag updates depending on it's content?
1 Answer
The item held in the cursor is not saved to NBT, but it's still a part of the player's inventory, so /clear can find it. If you want the same chat spam while cursor-holding the item, throw out the slow NBT method altogether and do this:
/execute as @a store result score @s book run clear @s writable_book{pages:[""]} 0 Of course, you will need to add a scoreboard objective for this (book in this example). When a player's book score is 1, that player has a book in their inventory or cursor. This will not work in creative mode, but will in survival/adventure.
If you want something different to happen when you click on the book, you can take advantage of this NBT vs clear disparity. Just add this command in addition to the one above:
/scoreboard players set @a[scores={book=1},nbt={Inventory:[{tag:{pages:[""]}}]}] book 2 Now a score of 0 means no book, a score of 1 means the book is in the cursor, and a score of 2 means the book is in an inventory slot.