Minecraft – Test to see if chest minecart has specific item

minecraft-commandsminecraft-java-edition

I'm building a minecart system for multiple players and stations.
When a player drives a minecart, a writable book sets the destination. If the car runs over a detector rail, a command block checks which text is contained in the book and sets the switch to the left or right depending on the content of the book. This only works if a player is sitting in the minecart.

Now we also want to use chest minecarts for transportation. So we have to test whether a book with text is in the minecart. how does it work? I have researched and come to no result.

The command to test with the player is execute if entity @a [nbt = {SelectedItem: {id:" minecraft: writable_book ", tag: {pages: [" town "]}}}]

Best Answer

You can detect the book like this:

/execute if entity @e[type=chest_minecart,nbt={Items:[{id:"minecraft:writable_book",tag:{pages:["town"]}}]}]