#make sure player has the item

tag @s remove hasCarrot
tag @s remove invalidCarrot
tag @s[nbt={SelectedItem:{id:"minecraft:carrot_on_a_stick"}}] add invalidCarrot
tag @s[nbt={Inventory:[{Slot:-106b,id:"minecraft:carrot_on_a_stick"}]}] add hasCarrot

#execute if entity @s[tag=invalidCarrot] run tellraw @s ["",{"color":"red","text":"[ ! ] Sorry, you can't hold items in your offhand!","bold":"true"}]
#execute if entity @s[tag=invalidCarrot] run playsound minecraft:block.lava.extinguish master @s ~ ~ ~ .7 1 .7
execute if entity @s[tag=invalidCarrot] run clear @s minecraft:carrot_on_a_stick

#give carrot on stick if missing
execute if entity @s[tag=!hasCarrot] run clear @s minecraft:carrot_on_a_stick
execute if entity @s[tag=!hasCarrot,tag=!noInventory] run item replace entity @s weapon.offhand with minecraft:carrot_on_a_stick{Tags:["input_carrot"],CustomModelData:1234567,display:{Lore:["This is used to detect right clicks!"],Name:"{\"text\":\"Input Detector\",\"bold\":\"true\",\"italic\":\"false\",\"color\":\"light_purple\"}"}} 1