#reset max hp scoreboards
scoreboard players set @s CadeyrnMaxHP 900
execute as @e[tag=CadeyrnTP,type=skeleton_horse] run scoreboard players set @s CadeyrnMaxHP 900

#for each new player, add 200 hp to current and max hp, and 100 to the half hp detecting threshold scoreboard
execute at @a[tag=CadeyrnFightTag,tag=!GaveCadHP] run scoreboard players add @s CadeyrnBaseHP 500
execute at @a[tag=CadeyrnFightTag,tag=!GaveCadHP] run scoreboard players add @s CadeyrnBossBarHP 500
execute at @a[tag=CadeyrnFightTag,tag=!GaveCadHP] run scoreboard players add @s CadeyrnFinStHP 50
execute at @a[tag=CadeyrnFightTag,tag=!GaveCadHP] run scoreboard players add @s CadeyrnHalfHP 275
execute at @a[tag=CadeyrnFightTag,tag=!GaveCadHP] run scoreboard players add @e[type=armor_stand,name=CadeyrnFight] CadeyrnPlayers 1
tag @a[tag=CadeyrnFightTag,tag=!GaveCadHP] add GaveCadHP
execute store result bossbar minecraft:cadeyrn max run scoreboard players get @s CadeyrnBaseHP

#take cadeyrn's damage taken since last check
execute as @s store result score @s CadeyrnCurrentHP run data get entity @s Health 1
scoreboard players operation @s CadeyrnMaxHP -= @s CadeyrnCurrentHP
scoreboard players operation @s CadeyrnDamTaken = @s CadeyrnMaxHP

#take the horse's damage as well, and add it to cadeyrn's scoreboard
execute as @e[tag=CadeyrnTP,type=skeleton_horse] run execute as @s store result score @s CadeyrnCurrentHP run data get entity @s Health 1
execute as @e[tag=CadeyrnTP,type=skeleton_horse] run scoreboard players operation @s CadeyrnMaxHP -= @s CadeyrnCurrentHP
execute as @e[tag=CadeyrnTP,type=skeleton_horse] run scoreboard players operation @s CadeyrnDamTaken = @s CadeyrnMaxHP
execute as @e[tag=CadeyrnTP,type=skeleton_horse] run scoreboard players operation @e[type=stray,tag=CadeyrnTP,limit=1] CadeyrnDamTaken += @s CadeyrnDamTaken

#subtract boss bar value by his(+horse) total damage taken since last check
#scoreboard players operation @s CadeyrnBossBarHP = @s CadeyrnBaseHP
scoreboard players operation @s CadeyrnBossBarHP -= @s CadeyrnDamTaken
execute store result bossbar minecraft:cadeyrn value run scoreboard players get @s CadeyrnBossBarHP

#heal cadeyrn and the horse's hidden health
execute as @e[tag=CadeyrnTP,type=skeleton_horse] run data merge entity @s {Health:900f,Attributes:[{Name:generic.max_health,Base:900}]}
data merge entity @s {Health:900f,Attributes:[{Name:generic.max_health,Base:900}]}

