scoreboard players set @s AttackTimer 0
scoreboard players set @s[scores={CadeyrnTimer=60..}] CadeyrnTimer 0
#check for half hp threshold 
#scoreboard players set @s KyuDummy 10000
#scoreboard players operation @s KyuDummy = @s CadeyrnBossBarHP
#scoreboard players operation @s KyuDummy -= @s CadeyrnHalfHP
#execute as @s[scores={KyuDummy=..0}] run DO PHASE TRANSITION

#check for final stand threshold 
scoreboard players set @s KyuDummy 10000
scoreboard players operation @s KyuDummy = @s CadeyrnBossBarHP
scoreboard players operation @s KyuDummy -= @s CadeyrnFinStHP
execute as @s[scores={KyuDummy=..0},tag=!DoFinalStand,tag=!DoneFinalStand] run tag @s add DoFinalStand
execute as @s[tag=DoFinalStand,tag=!DoneFinalStand] run execute at @e[type=armor_stand,name=DayQ] run data merge block 999991 165 999988 {auto:1b}


bossbar set minecraft:cadeyrn players @a[tag=aaaaaaaaaaaa]
bossbar set minecraft:cadeyrn players @a[tag=CadeyrnFightTag]

execute at @e[type=armor_stand,name=CadeyrnFight] positioned ~32 126 ~32 run tp @e[dx=-64,dy=30,dz=-64,type=#suhc:pigtype,tag=!CadeyrnMinion] ~ -500 ~

effect clear @s wither
effect clear @s slowness

#loot table for random attack

tag @s remove NoPlayerNear
#loot table for random attack
execute at @s positioned ~-20 ~-4 ~-20 run execute unless entity @a[tag=CadeyrnFightTag,gamemode=!spectator,dx=40,dy=8,dz=40] run tag @s add NoPlayerNear
execute at @s positioned ~-4 ~5 ~-4 run execute if entity @a[tag=CadeyrnFightTag,gamemode=!spectator,dx=4,dy=15,dz=4] run tag @s add NoPlayerNear
execute at @s[tag=!NoPlayerNear,tag=!DoFinalStand] positioned ~ 255 ~ run loot spawn ~ ~ ~ loot minecraft:cadeyrnp2small
execute at @s[tag=NoPlayerNear,tag=!DoFinalStand] positioned ~ 255 ~ run loot spawn ~ ~ ~ loot minecraft:cadeyrnp2smallnotnear

execute at @s positioned ~ 255 ~ run execute at @e[type=item,distance=..5,nbt={Item:{id:"minecraft:paper",Count:1b,tag:{display:{Lore:["\"ArrowVolley\""]}}}}] run execute at @e[type=armor_stand,name=DayQ] run data merge block 999991 146 999988 {auto:1b}
execute at @s positioned ~ 255 ~ run execute at @e[type=item,distance=..5,nbt={Item:{id:"minecraft:paper",Count:1b,tag:{display:{Lore:["\"FireWhip\""]}}}}] run execute at @e[type=armor_stand,name=DayQ] run data merge block 999991 153 999988 {auto:1b}
execute at @s positioned ~ 255 ~ run execute at @e[type=item,distance=..5,nbt={Item:{id:"minecraft:paper",Count:1b,tag:{display:{Lore:["\"GetOverHere\""]}}}}] run execute at @e[type=armor_stand,name=DayQ] run data merge block 999991 154 999988 {auto:1b}
execute at @s positioned ~ 255 ~ run execute at @e[type=item,distance=..5,nbt={Item:{id:"minecraft:paper",Count:1b,tag:{display:{Lore:["\"PocketSand\""]}}}}] run execute at @e[type=armor_stand,name=DayQ] run data merge block 999991 157 999988 {auto:1b}
execute at @s positioned ~ 255 ~ run execute at @e[type=item,distance=..5,nbt={Item:{id:"minecraft:paper",Count:1b,tag:{display:{Lore:["\"ShortLeap\""]}}}}] run execute at @e[type=armor_stand,name=DayQ] run data merge block 999991 158 999988 {auto:1b}
execute at @s positioned ~ 255 ~ run execute at @e[type=item,distance=..5,nbt={Item:{id:"minecraft:paper",Count:1b,tag:{display:{Lore:["\"Teleport\""]}}}}] run execute at @e[type=armor_stand,name=DayQ] run data merge block 999991 161 999988 {auto:1b}
execute at @s positioned ~ 255 ~ run execute at @e[type=item,distance=..5,nbt={Item:{id:"minecraft:paper",Count:1b,tag:{display:{Lore:["\"TinyExplode\""]}}}}] run execute at @e[type=armor_stand,name=DayQ] run data merge block 999991 162 999988 {auto:1b}


#setblock 999991 164 999988 repeating_command_block[facing=up]{auto:0b,Command:"execute as @e[type=minecraft:stray,limit=1,tag=CadeyrnTP] run function zzzzdonotuse:cadeyrn/attacks/autoattackphase2/autoattackmain",TrackOutput:0} destroy
#setblock 999991 165 999988 repeating_command_block[facing=up]{auto:0b,Command:"execute as @e[type=minecraft:stray,limit=1,tag=CadeyrnTP] run function zzzzdonotuse:cadeyrn/attacks/finalstand/finalstandmain",TrackOutput:0} destroy
#setblock 999991 166 999988 repeating_command_block[facing=up]{auto:0b,Command:"execute as @e[type=minecraft:stray,limit=1,tag=CadeyrnTP] run function zzzzdonotuse:cadeyrn/attacks/finalstand/persistentparticles",TrackOutput:0} destroy
#setblock 999991 167 999988 repeating_command_block[facing=up]{auto:0b,Command:"execute as @e[type=minecraft:stray,limit=1,tag=CadeyrnTP] run function zzzzdonotuse:cadeyrn/attacks/deathanim/deathmain",TrackOutput:0} destroy



#execute at @s[tag=!ThrewBigBone] positioned ~ 255 ~ run execute at @e[type=item,distance=..5,nbt={Item:{id:"minecraft:paper",Count:1b,tag:{display:{Lore:["\"BoneThrow\""]}}}}] run execute at @e[type=armor_stand,name=DayQ] run data merge block 999991 140 999988 {auto:1b}
#execute at @s[tag=ThrewBigBone] positioned ~ 255 ~ run execute at @e[type=item,distance=..5,nbt={Item:{id:"minecraft:paper",Count:1b,tag:{display:{Lore:["\"BoneThrow\""]}}}}] run execute at @e[type=armor_stand,name=DayQ] run data merge block 999991 144 999988 {auto:1b}


#execute at @s positioned ~ 255 ~ run execute at @e[type=item,distance=..5,nbt={Item:{id:"minecraft:paper",Count:1b,tag:{display:{Lore:["\"Attack1\""]}}}}] run execute at @e[type=armor_stand,name=DayQ] run data merge block 999991 137 999988 {auto:1b}

execute at @s positioned ~ 255 ~ run kill @e[type=item,distance=..10]