scoreboard objectives add HatThrow minecraft.used:minecraft.snowball
scoreboard objectives add HatTime dummy
execute as @a[scores={HatThrow=1..},tag=!has_thrown_hat] at @s run summon armor_stand ~ ~ ~ {CustomName:"\"Hat\"",Invisible:1,ArmorItems:[{},{},{},{id:"dead_brain_coral_block",Count:1b}],NoGravity:1}
execute as @a[scores={HatThrow=1..},tag=!has_thrown_hat] at @s rotated as @s positioned ^ ^ ^5 align x align y align z run tp @e[name=Hat,type=armor_stand,limit=1,sort=nearest] ~0.5 ~-1.5 ~0.5
execute as @a[scores={HatThrow=1..},tag=!has_thrown_hat] at @s rotated as @s positioned ^ ^ ^5 align x align y align z run setblock ~ ~ ~ yellow_stained_glass keep
execute as @a[scores={HatThrow=1..},tag=!has_thrown_hat] at @s rotated as @s run tag @s add has_thrown_hat
execute as @a[scores={HatThrow=1..}] run clear @s snowball
execute as @a[scores={HatThrow=1..}] run clear @s dead_brain_coral_block
execute as @a[scores={HatThrow=1..}] at @s rotated as @s run scoreboard players set @s HatThrow 0
execute as @e[type=armor_stand,name=Hat] at @s rotated as @s run tp @s ~ ~ ~ ~10 ~
execute as @e[type=armor_stand,name=Hat] at @s run scoreboard players add @s HatTime 1
execute as @e[type=armor_stand,name=Hat,scores={HatTime=59..}] at @s run fill ~-2 ~-2 ~-2 ~2 ~4 ~2 air replace yellow_stained_glass
execute as @e[type=armor_stand,name=Hat,scores={HatTime=60..}] at @s run tp @s @a[tag=has_thrown_hat,limit=1,sort=nearest]
execute as @e[type=armor_stand,name=Hat,scores={HatTime=60..}] at @s run replaceitem entity @a[tag=has_thrown_hat,limit=1,sort=nearest] armor.head dead_brain_coral_block{display:{Name:"{\"text\":\"Hat \",\"color\":\"white\",\"italic\":false}"},Unbreakable:1,HideFlags:1}
execute as @e[type=armor_stand,name=Hat,scores={HatTime=60..}] at @s run replaceitem entity @a[tag=has_thrown_hat,limit=1,sort=nearest] hotbar.8 snowball{display:{Name:"{\"text\":\"Throw Hat \",\"color\":\"white\",\"italic\":false}"},Unbreakable:1,HideFlags:1}
execute as @e[type=armor_stand,name=Hat,scores={HatTime=60..}] at @s run tag @a[tag=has_thrown_hat,limit=1,sort=nearest] remove has_thrown_hat
kill @e[type=armor_stand,name=Hat,scores={HatTime=70..}]
kill @e[type=snowball]

execute unless entity @e[type=armor_stand,name=Hat] run replaceitem entity @a[tag=!PlayingIn2D,gamemode=adventure] armor.head dead_brain_coral_block{display:{Name:"{\"text\":\"Hat \",\"color\":\"white\",\"italic\":false}"},Unbreakable:1,HideFlags:1}
execute unless entity @e[type=armor_stand,name=Hat] run replaceitem entity @a[tag=!PlayingIn2D,gamemode=adventure] hotbar.8 snowball{display:{Name:"{\"text\":\"Throw Hat \",\"color\":\"white\",\"italic\":false}"},Unbreakable:1,HideFlags:1}
execute unless entity @e[type=armor_stand,name=Hat] run tag @a[tag=!PlayingIn2D,gamemode=adventure] remove has_thrown_hat
clear @a[tag=PlayingIn2D] snowball