scoreboard players set @s AttackTimer 0
scoreboard players set @s[scores={CadeyrnTimer=50..}] 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]

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

effect clear @s wither
effect clear @s slowness

execute at @s[tag=!NoPlayerNear,tag=!DoFinalStand] positioned ~ 255 ~ run loot spawn ~ ~ ~ loot minecraft:cadeyrnp2
execute at @s[tag=NoPlayerNear,tag=!DoFinalStand] positioned ~ 255 ~ run loot spawn ~ ~ ~ loot minecraft:cadeyrnp2notnear

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 ~

execute at @s positioned ~ 255 ~ run execute at @e[type=item,distance=..5,nbt={Item:{id:"minecraft:paper",Count:1b,tag:{display:{Lore:["\"EntityCharge\""]}}}}] run execute at @e[type=armor_stand,name=DayQ] run data merge block 999991 147 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:["\"FallingEffect\""]}}}}] run execute at @e[type=armor_stand,name=DayQ] run data merge block 999991 149 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:["\"FireCircle\""]}}}}] run execute at @e[type=armor_stand,name=DayQ] run data merge block 999991 150 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:["\"FireLine\""]}}}}] run execute at @e[type=armor_stand,name=DayQ] run data merge block 999991 151 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:["\"FireLineMassive\""]}}}}] run execute at @e[type=armor_stand,name=DayQ] run data merge block 999991 152 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:["\"GroundCrack\""]}}}}] run execute at @e[type=armor_stand,name=DayQ] run data merge block 999991 155 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:["\"P2ChargeSlash\""]}}}}] run execute at @e[type=armor_stand,name=DayQ] run data merge block 999991 156 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:["\"SoulSplit\""]}}}}] run execute at @e[type=armor_stand,name=DayQ] run data merge block 999991 159 999988 {auto:1b}
execute at @s[tag=!SummonedInd] positioned ~ 255 ~ run execute at @e[type=item,distance=..5,nbt={Item:{id:"minecraft:paper",Count:1b,tag:{display:{Lore:["\"SummonIndolent\""]}}}}] run execute at @e[type=armor_stand,name=DayQ] run data merge block 999991 160 999988 {auto:1b}
execute at @s[tag=SummonedInd] positioned ~ 255 ~ run execute at @e[type=item,distance=..5,nbt={Item:{id:"minecraft:paper",Count:1b,tag:{display:{Lore:["\"SummonIndolent\""]}}}}] run execute at @e[type=armor_stand,name=DayQ] run data merge block 999991 156 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:["\"VineAttack\""]}}}}] run execute at @e[type=armor_stand,name=DayQ] run data merge block 999991 163 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 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]