Minecraft – Different outputs based on item in offhand

minecraft-commandsminecraft-java-edition

I'm making a Minecraft adventure map in 1.13.2 where the player gets a item that can collect information by right clicking on signs or items with it in their offhand. I only know of one way the sign can give a message in chat: /tellraw. However, I don't want it to show the same message if the player doesn't have this item in their offhand.

I have searched for terms like "minecraft different outputs offhand item" and "minecraft rightclick sign offhand item different" but it seems no one else has this same problem. No one else, as I've seen, has explained how this could even be accomplished.

Best Answer

You'll only need one command to place the sign that will do this. The command is:

setblock ~ ~1 ~ sign{Text1:"{\"text\":\"\",\"clickEvent\":{\"action\":\"run_command\",\"value\":\"execute as @p run tellraw @s[nbt={Inventory:[{Slot:-106b,id:\\\"minecraft:stone\\\"}]}] {\\\"text\\\":\\\"You have stone\\\",\\\"color\\\":\\\"green\\\"}\"}}",Text2:"{\"text\":\"YOUR TEXT\",\"clickEvent\":{\"action\":\"run_command\",\"value\":\"execute as @p run tellraw @s[nbt=!{Inventory:[{Slot:-106b,id:\\\"minecraft:stone\\\"}]}] {\\\"text\\\":\\\"You do not have stone\\\",\\\"color\\\":\\\"red\\\"}\"}}"}

Swap hands with stone back and forth and right click the sign. Edit sign text as necessary.

This has been tested well, so if something is wrong please let me know and I will try my best to help.