execute at @s run summon armor_stand ~ ~ ~ {Tags:[unequiphat],Invisible:1b,Small:1b,DisabledSlots:4144959,Silent:1b}
execute if entity @s[nbt={SelectedItem:{components:{"minecraft:custom_data":{replacehat:tag}}}}] run tag @e[tag=unequiphat] add main
execute if entity @s[nbt={equipment:{offhand:{components:{"minecraft:custom_data":{replacehat:tag}}}}}] run tag @e[tag=unequiphat] add off
execute if entity @e[tag=unequiphat,tag=main] run item replace entity @n[tag=unequiphat,tag=main] armor.head from entity @s weapon.mainhand
execute if entity @e[tag=unequiphat,tag=off] run item replace entity @n[tag=unequiphat,tag=off] armor.head from entity @s weapon.offhand

#bushhat
execute if entity @e[tag=unequiphat,nbt={equipment:{head:{id:"minecraft:azalea",components:{"minecraft:custom_data":{bushhat:tag}}}}}] run tag @e[tag=unequiphat] add bushhat
execute as @e[tag=unequiphat,tag=bushhat] run data modify entity @s equipment.head.id set value iron_helmet
execute as @e[tag=unequiphat,tag=bushhat] run data modify entity @s equipment.head.components."minecraft:custom_data" set value {bushhat:tag,hat:tag,specialhat:tag}
#illagerhat
execute if entity @e[tag=unequiphat,nbt={equipment:{head:{id:"minecraft:white_banner",components:{"minecraft:custom_data":{illagerhat:tag}}}}}] run tag @e[tag=unequiphat] add illagerhat
execute as @e[tag=unequiphat,tag=illagerhat] run data modify entity @s equipment.head.id set value iron_helmet
execute as @e[tag=unequiphat,tag=illagerhat] run data modify entity @s equipment.head.components."minecraft:custom_data" set value {illagerhat:tag,hat:tag,specialhat:tag}
#spacehat
execute if entity @e[tag=unequiphat,nbt={equipment:{head:{id:"minecraft:glass",components:{"minecraft:custom_data":{spacehat:tag}}}}}] run tag @e[tag=unequiphat] add spacehat
execute as @e[tag=unequiphat,tag=spacehat] run data modify entity @s equipment.head.id set value iron_helmet
execute as @e[tag=unequiphat,tag=spacehat] run data modify entity @s equipment.head.components."minecraft:custom_data" set value {spacehat:tag,hat:tag}
#tradehat
execute if entity @e[tag=unequiphat,nbt={equipment:{head:{id:"minecraft:emerald",components:{"minecraft:custom_data":{tradehat:tag}}}}}] run tag @e[tag=unequiphat] add tradehat
execute as @e[tag=unequiphat,tag=tradehat] run data modify entity @s equipment.head.id set value iron_helmet
execute as @e[tag=unequiphat,tag=tradehat] run data modify entity @s equipment.head.components."minecraft:custom_data" set value {tradehat:tag,hat:tag}

execute if entity @e[tag=unequiphat,tag=main] run item replace entity @s weapon.mainhand from entity @n[tag=unequiphat] armor.head
execute if entity @e[tag=unequiphat,tag=off] run item replace entity @s weapon.offhand from entity @n[tag=unequiphat] armor.head

kill @e[tag=unequiphat]