Minecraft – Select Players with inventory open using Mincraft Commands

minecraft-commandsminecraft-java-edition

What I'm trying to do: Make sure players always have a particular guidebook/spellbook (a special command-generated one with clickable links and a special tag).

If they lose their book, it should appear back in their inventory. If they drop their book, it should do that and the book on the ground should disappear.

I have this working on a basic level, except… Any time a player tries to move their book to a different slot, it gives them a new one, which just feels wrong. There is apparently no inventory slot you can detect for the mouse. Even if there were…
This can lead to fast infinite generation if, for instance, they open a chest and repeatedly put that book in that chest.

So the thought process here is maybe make it so that if the player has the inventory GUI open, they are excluded from the selection. So they don't get the book until the inventory is closed. It still leaves some gaps, and they could fill chests with these books. But they would have to try to make it glitch out, and I think it would feel right at that point. And in order to do infinite generation, they'd have to repeatedly open and close the chest.

Here are my commands as it stands. (2 command blocks per spellbook)

/give @a[nbt=!{Inventory:[{id:"minecraft:written_book", tag:{spellbook:basic}}]}] written_book{pages:[...yada...],title:"Spellbook",author:"Dartania @ http://minecraft.tools/",display:{Lore:["You need this for lots of stuff!  Don't lose it!"]},spellbook:basic}
/kill @e[nbt={Item:{id:"minecraft:written_book", tag:{spellbook:basic}}}]

What I'm looking to do is add to this part…
@a[nbt=!{Inventory:[{...}]
and make it more like @a[nbt=!{Inventory:[{...}]},nbt=!{Client:{InventoryOpen:true}}]

I know Client:{InventoryOpen} is not the correct thing, but I don't know what is the correct thing.

For reference, I'm building a Singleplayer/Multiplayer puzzle map on Vanilla Minecraft 1.14, with a bedrock/command_block bunker below spawn. I'm using http://minecraft.tools/ to generate stuff (like the spellbook)

Best Answer

easy fix:

clear @a[nbt=!{Inventory: [{Slot: 0b, id: "minecraft:written_book"}]}] minecraft:written_book

replaceitem entity @a hotbar.0 minecraft:written_book.......