execute store result bossbar boss_health value run data get entity @s Health
execute store result score @s phantom_sovereign_health run data get entity @s Health
scoreboard players add @s mob_first_skill_counter 1
execute if score @s mob_first_skill_counter matches 48 run playsound minecraft:entity.ender_dragon.flap master @a[distance=..150] ~ ~ ~ 10 0.1 1
execute if score @s mob_first_skill_counter matches 48 run playsound minecraft:entity.ender_dragon.flap master @a[distance=..150] ~ ~ ~ 10 0.1 1
execute if score @s mob_first_skill_counter matches 48 run playsound minecraft:entity.ender_dragon.flap master @a[distance=..150] ~ ~ ~ 10 0.1 1
execute if score @s mob_first_skill_counter matches 48.. run scoreboard players reset @s mob_first_skill_counter

execute if entity @s[nbt={HurtTime:10s}] run function kiwi2:custom_mobs_fx/phantom_sovereign_bleed_fx

scoreboard players add @s[tag=!phantom_sovereign_no_spawn,tag=!phantom_sovereign_phase_5] mob_second_skill_counter 1
execute if score @s mob_second_skill_counter matches 200 run summon phantom ~ ~ ~ {DeathLootTable:"",Tags:["wave_mob"],attributes:[{id:'minecraft:follow_range',base:12}],PersistenceRequired:1b}
execute if score @s mob_second_skill_counter matches 400 run summon phantom ~ ~ ~ {DeathLootTable:"",Tags:["wave_mob"],attributes:[{id:'minecraft:follow_range',base:12}],PersistenceRequired:1b}
execute if score @s mob_second_skill_counter matches 600 run summon phantom ~ ~ ~ {DeathLootTable:"",Tags:["wave_mob"],attributes:[{id:'minecraft:follow_range',base:12}],PersistenceRequired:1b}
execute if score @s mob_second_skill_counter matches 800 run summon phantom ~ ~ ~ {DeathLootTable:"",Tags:["wave_mob"],attributes:[{id:'minecraft:follow_range',base:12}],PersistenceRequired:1b}
execute if score @s mob_second_skill_counter matches 2000 if predicate kiwi2:chance_random_70 run summon phantom ~ ~ ~ {DeathLootTable:"",Health:120f,Size:10,Tags:["wave_mob"],Passengers:[{id:"minecraft:spawner_minecart",Tags:["wave_mob"],Invulnerable:1b,SpawnCount:1,MinSpawnDelay:80,MaxSpawnDelay:80,RequiredPlayerRange:32,SpawnData:{entity:{id:"minecraft:phantom",DeathTime:20,DeathLootTable:"",Health:10f,Tags:["wave_mob"],attributes:[{id:"minecraft:gravity",base:10},{id:"minecraft:max_health",base:10},{id:"minecraft:scale",base:0.75}]}}}],attributes:[{id:"minecraft:max_health",base:120},{id:'minecraft:follow_range',base:256},{id:'minecraft:tempt_range',base:256}],PersistenceRequired:1b}
execute if score @s mob_second_skill_counter matches 2000 store result score @s mob_second_skill_counter run random value 1..120

execute unless entity @e[type=skeleton,tag=phantom_cultist] run scoreboard players add @s[tag=!phantom_sovereign_no_spawn,tag=!phantom_sovereign_phase_5] mob_third_skill_counter 1
execute if score @s mob_third_skill_counter matches 500 run tellraw @a {"text": "Phantom Cultists spawned! Kill them to damage the Sovereign!","color": "yellow"}
execute if score @s mob_third_skill_counter matches 500 run playsound minecraft:entity.arrow.hit_player hostile @a ~ ~ ~ 10 1.8
execute if score @s mob_third_skill_counter matches 500 run summon skeleton ~ ~ ~ {Health:75f,Tags:["wave_mob","phantom_cultist","phantom_cultist_init"],HandItems:[{id:"minecraft:enchanted_book",count:1},{id:"minecraft:stick",count:1}],ArmorItems:[{id:"minecraft:leather_boots",count:1,components:{"minecraft:dyed_color":9974428,"minecraft:trim":{material:"minecraft:gold",pattern:"minecraft:tide"}}},{id:"minecraft:leather_leggings",count:1,components:{"minecraft:dyed_color":9780131,"minecraft:trim":{material:"minecraft:gold",pattern:"minecraft:ward"}}},{id:"minecraft:leather_chestplate",count:1,components:{"minecraft:dyed_color":10375574,"minecraft:trim":{material:"minecraft:gold",pattern:"minecraft:flow"}}},{id:"minecraft:player_head",count:1,components:{"minecraft:profile":{properties:[{name:"textures",value:"eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvN2FhNDI0YTliNzYxZmJlOTZjMmM5YjY2YmY2NTZjYjAwMmRlMjA5MmMzZDI0ZjE3MjMxYTk2OGQ0YWEwMzI5NyJ9fX0="}]}}}],active_effects:[{id:"minecraft:invisibility",amplifier:1,duration:19999980,show_particles:0b},{id:"minecraft:glowing",amplifier:1,duration:19999980,show_particles:0b}],attributes:[{id:"minecraft:follow_range",base:0},{id:"minecraft:max_health",base:75}],Passengers:[{id:"minecraft:slime",DeathLootTable:"",Silent:1b,Invulnerable:1b,DeathTime:400,NoAI:0b,Health:9999999f,Size:0,Tags:["phantom_cultist_death_check","knight_no_taunt","border_no_tp"],active_effects:[{id:"minecraft:invisibility",amplifier:1,duration:199999980,show_particles:0b},{id:"minecraft:resistance",amplifier:255,duration:199999980,show_particles:0b}],attributes:[{id:"minecraft:max_health",base:9999999},{id:"minecraft:scale",base:0.01}]}]}
execute if score @s mob_third_skill_counter matches 500 run summon skeleton ~ ~ ~ {Health:75f,Tags:["wave_mob","phantom_cultist","phantom_cultist_init"],HandItems:[{id:"minecraft:enchanted_book",count:1},{id:"minecraft:stick",count:1}],ArmorItems:[{id:"minecraft:leather_boots",count:1,components:{"minecraft:dyed_color":9974428,"minecraft:trim":{material:"minecraft:gold",pattern:"minecraft:tide"}}},{id:"minecraft:leather_leggings",count:1,components:{"minecraft:dyed_color":9780131,"minecraft:trim":{material:"minecraft:gold",pattern:"minecraft:ward"}}},{id:"minecraft:leather_chestplate",count:1,components:{"minecraft:dyed_color":10375574,"minecraft:trim":{material:"minecraft:gold",pattern:"minecraft:flow"}}},{id:"minecraft:player_head",count:1,components:{"minecraft:profile":{properties:[{name:"textures",value:"eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvN2FhNDI0YTliNzYxZmJlOTZjMmM5YjY2YmY2NTZjYjAwMmRlMjA5MmMzZDI0ZjE3MjMxYTk2OGQ0YWEwMzI5NyJ9fX0="}]}}}],active_effects:[{id:"minecraft:invisibility",amplifier:1,duration:19999980,show_particles:0b},{id:"minecraft:glowing",amplifier:1,duration:19999980,show_particles:0b}],attributes:[{id:"minecraft:follow_range",base:0},{id:"minecraft:max_health",base:75}],Passengers:[{id:"minecraft:slime",DeathLootTable:"",Silent:1b,Invulnerable:1b,DeathTime:400,NoAI:0b,Health:9999999f,Size:0,Tags:["phantom_cultist_death_check","knight_no_taunt","border_no_tp"],active_effects:[{id:"minecraft:invisibility",amplifier:1,duration:199999980,show_particles:0b},{id:"minecraft:resistance",amplifier:255,duration:199999980,show_particles:0b}],attributes:[{id:"minecraft:max_health",base:9999999},{id:"minecraft:scale",base:0.01}]}]}
execute if score @s mob_third_skill_counter matches 500 run summon skeleton ~ ~ ~ {Health:75f,Tags:["wave_mob","phantom_cultist","phantom_cultist_init"],HandItems:[{id:"minecraft:enchanted_book",count:1},{id:"minecraft:stick",count:1}],ArmorItems:[{id:"minecraft:leather_boots",count:1,components:{"minecraft:dyed_color":9974428,"minecraft:trim":{material:"minecraft:gold",pattern:"minecraft:tide"}}},{id:"minecraft:leather_leggings",count:1,components:{"minecraft:dyed_color":9780131,"minecraft:trim":{material:"minecraft:gold",pattern:"minecraft:ward"}}},{id:"minecraft:leather_chestplate",count:1,components:{"minecraft:dyed_color":10375574,"minecraft:trim":{material:"minecraft:gold",pattern:"minecraft:flow"}}},{id:"minecraft:player_head",count:1,components:{"minecraft:profile":{properties:[{name:"textures",value:"eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvN2FhNDI0YTliNzYxZmJlOTZjMmM5YjY2YmY2NTZjYjAwMmRlMjA5MmMzZDI0ZjE3MjMxYTk2OGQ0YWEwMzI5NyJ9fX0="}]}}}],active_effects:[{id:"minecraft:invisibility",amplifier:1,duration:19999980,show_particles:0b},{id:"minecraft:glowing",amplifier:1,duration:19999980,show_particles:0b}],attributes:[{id:"minecraft:follow_range",base:0},{id:"minecraft:max_health",base:75}],Passengers:[{id:"minecraft:slime",DeathLootTable:"",Silent:1b,Invulnerable:1b,DeathTime:400,NoAI:0b,Health:9999999f,Size:0,Tags:["phantom_cultist_death_check","knight_no_taunt","border_no_tp"],active_effects:[{id:"minecraft:invisibility",amplifier:1,duration:199999980,show_particles:0b},{id:"minecraft:resistance",amplifier:255,duration:199999980,show_particles:0b}],attributes:[{id:"minecraft:max_health",base:9999999},{id:"minecraft:scale",base:0.01}]}]}
execute if score @s mob_third_skill_counter matches 500 run summon skeleton ~ ~ ~ {Health:75f,Tags:["wave_mob","phantom_cultist","phantom_cultist_init"],HandItems:[{id:"minecraft:enchanted_book",count:1},{id:"minecraft:stick",count:1}],ArmorItems:[{id:"minecraft:leather_boots",count:1,components:{"minecraft:dyed_color":9974428,"minecraft:trim":{material:"minecraft:gold",pattern:"minecraft:tide"}}},{id:"minecraft:leather_leggings",count:1,components:{"minecraft:dyed_color":9780131,"minecraft:trim":{material:"minecraft:gold",pattern:"minecraft:ward"}}},{id:"minecraft:leather_chestplate",count:1,components:{"minecraft:dyed_color":10375574,"minecraft:trim":{material:"minecraft:gold",pattern:"minecraft:flow"}}},{id:"minecraft:player_head",count:1,components:{"minecraft:profile":{properties:[{name:"textures",value:"eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvN2FhNDI0YTliNzYxZmJlOTZjMmM5YjY2YmY2NTZjYjAwMmRlMjA5MmMzZDI0ZjE3MjMxYTk2OGQ0YWEwMzI5NyJ9fX0="}]}}}],active_effects:[{id:"minecraft:invisibility",amplifier:1,duration:19999980,show_particles:0b},{id:"minecraft:glowing",amplifier:1,duration:19999980,show_particles:0b}],attributes:[{id:"minecraft:follow_range",base:0},{id:"minecraft:max_health",base:75}],Passengers:[{id:"minecraft:slime",DeathLootTable:"",Silent:1b,Invulnerable:1b,DeathTime:400,NoAI:0b,Health:9999999f,Size:0,Tags:["phantom_cultist_death_check","knight_no_taunt","border_no_tp"],active_effects:[{id:"minecraft:invisibility",amplifier:1,duration:199999980,show_particles:0b},{id:"minecraft:resistance",amplifier:255,duration:199999980,show_particles:0b}],attributes:[{id:"minecraft:max_health",base:9999999},{id:"minecraft:scale",base:0.01}]}]}
execute if score @s mob_third_skill_counter matches 500 run summon skeleton ~ ~ ~ {Health:75f,Tags:["wave_mob","phantom_cultist","phantom_cultist_init"],HandItems:[{id:"minecraft:enchanted_book",count:1},{id:"minecraft:stick",count:1}],ArmorItems:[{id:"minecraft:leather_boots",count:1,components:{"minecraft:dyed_color":9974428,"minecraft:trim":{material:"minecraft:gold",pattern:"minecraft:tide"}}},{id:"minecraft:leather_leggings",count:1,components:{"minecraft:dyed_color":9780131,"minecraft:trim":{material:"minecraft:gold",pattern:"minecraft:ward"}}},{id:"minecraft:leather_chestplate",count:1,components:{"minecraft:dyed_color":10375574,"minecraft:trim":{material:"minecraft:gold",pattern:"minecraft:flow"}}},{id:"minecraft:player_head",count:1,components:{"minecraft:profile":{properties:[{name:"textures",value:"eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvN2FhNDI0YTliNzYxZmJlOTZjMmM5YjY2YmY2NTZjYjAwMmRlMjA5MmMzZDI0ZjE3MjMxYTk2OGQ0YWEwMzI5NyJ9fX0="}]}}}],active_effects:[{id:"minecraft:invisibility",amplifier:1,duration:19999980,show_particles:0b},{id:"minecraft:glowing",amplifier:1,duration:19999980,show_particles:0b}],attributes:[{id:"minecraft:follow_range",base:0},{id:"minecraft:max_health",base:75}],Passengers:[{id:"minecraft:slime",DeathLootTable:"",Silent:1b,Invulnerable:1b,DeathTime:400,NoAI:0b,Health:9999999f,Size:0,Tags:["phantom_cultist_death_check","knight_no_taunt","border_no_tp"],active_effects:[{id:"minecraft:invisibility",amplifier:1,duration:199999980,show_particles:0b},{id:"minecraft:resistance",amplifier:255,duration:199999980,show_particles:0b}],attributes:[{id:"minecraft:max_health",base:9999999},{id:"minecraft:scale",base:0.01}]}]}
execute if score @s mob_third_skill_counter matches 500 run summon skeleton ~ ~ ~ {Health:75f,Tags:["wave_mob","phantom_cultist","phantom_cultist_init"],HandItems:[{id:"minecraft:enchanted_book",count:1},{id:"minecraft:stick",count:1}],ArmorItems:[{id:"minecraft:leather_boots",count:1,components:{"minecraft:dyed_color":9974428,"minecraft:trim":{material:"minecraft:gold",pattern:"minecraft:tide"}}},{id:"minecraft:leather_leggings",count:1,components:{"minecraft:dyed_color":9780131,"minecraft:trim":{material:"minecraft:gold",pattern:"minecraft:ward"}}},{id:"minecraft:leather_chestplate",count:1,components:{"minecraft:dyed_color":10375574,"minecraft:trim":{material:"minecraft:gold",pattern:"minecraft:flow"}}},{id:"minecraft:player_head",count:1,components:{"minecraft:profile":{properties:[{name:"textures",value:"eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvN2FhNDI0YTliNzYxZmJlOTZjMmM5YjY2YmY2NTZjYjAwMmRlMjA5MmMzZDI0ZjE3MjMxYTk2OGQ0YWEwMzI5NyJ9fX0="}]}}}],active_effects:[{id:"minecraft:invisibility",amplifier:1,duration:19999980,show_particles:0b},{id:"minecraft:glowing",amplifier:1,duration:19999980,show_particles:0b}],attributes:[{id:"minecraft:follow_range",base:0},{id:"minecraft:max_health",base:75}],Passengers:[{id:"minecraft:slime",DeathLootTable:"",Silent:1b,Invulnerable:1b,DeathTime:400,NoAI:0b,Health:9999999f,Size:0,Tags:["phantom_cultist_death_check","knight_no_taunt","border_no_tp"],active_effects:[{id:"minecraft:invisibility",amplifier:1,duration:199999980,show_particles:0b},{id:"minecraft:resistance",amplifier:255,duration:199999980,show_particles:0b}],attributes:[{id:"minecraft:max_health",base:9999999},{id:"minecraft:scale",base:0.01}]}]}
execute if score @s mob_third_skill_counter matches 500 run spreadplayers ~ ~ 8 25 false @e[type=skeleton,tag=phantom_cultist_init]
execute if score @s mob_third_skill_counter matches 500 run tag @e[tag=phantom_cultist_init] remove phantom_cultist_init
execute if score @s mob_third_skill_counter matches 500.. store result score @s mob_third_skill_counter run random value 1..150

# Lightning Flurry / Meteor Shower / Snapfreeze
scoreboard players add @s[tag=!phantom_sovereign_defeat] mob_fourth_skill_counter 1
# Phase 1 & Phase 4
execute if score @s[tag=phantom_sovereign_phase_1,tag=!phantom_sovereign_phase_5] mob_fourth_skill_counter matches 1200..1292 run function kiwi2:custom_mobs_fx/phantom_sovereign_lightning_flurry
execute if score @s[tag=phantom_sovereign_phase_4,tag=!phantom_sovereign_phase_5] mob_fourth_skill_counter matches 1200..1292 run function kiwi2:custom_mobs_fx/phantom_sovereign_lightning_flurry

# Phase 3 & Phase 4 (phase 3 tag doesn't get removed)
execute if score @s[tag=phantom_sovereign_phase_3,tag=!phantom_sovereign_phase_5] mob_fourth_skill_counter matches 1200..1500 run function kiwi2:custom_mobs_fx/phantom_sovereign_meteor_shower

# Phase 3 & 4
execute unless score world MultiplayerScaling matches 1 if score @s[tag=phantom_sovereign_phase_3,tag=!phantom_sovereign_phase_5] mob_fourth_skill_counter matches 600 as @r at @s run function kiwi2:custom_mobs_fx/phantom_sovereign_snapfreeze/phantom_sovereign_snapfreeze
execute unless score world MultiplayerScaling matches 1 if entity @a[tag=sovereign_frozen] as @a[tag=sovereign_frozen] at @s if entity @a[tag=!sovereign_frozen,distance=..4] run function kiwi2:custom_mobs_fx/phantom_sovereign_snapfreeze/freeze_breakout
execute unless score world MultiplayerScaling matches 1 if entity @a[tag=sovereign_frozen] as @a[tag=sovereign_frozen] at @s run particle snowflake ~ ~1 ~ 0.2 0.5 0.2 0 1

# Phase 5 Attacks
execute if score @s[tag=phantom_sovereign_phase_5,tag=!phantom_sovereign_defeat] mob_fourth_skill_counter matches 1200..1300 run function kiwi2:custom_mobs_fx/phantom_sovereign_phase_5_attack

# Phase Handling
execute if score @s[tag=!phantom_sovereign_phase_2] phantom_sovereign_health matches ..716 run function kiwi2:custom_mobs_fx/phantom_sovereign_phase_2_init
execute if score @s[tag=phantom_sovereign_phase_3,tag=!phantom_sovereign_phase_4] phantom_sovereign_health matches ..341 run function kiwi2:custom_mobs_fx/phantom_sovereign_phase_4_init
execute if score @s[tag=phantom_sovereign_phase_4,tag=!phantom_sovereign_phase_5] phantom_sovereign_health matches ..100 run function kiwi2:custom_mobs_fx/phantom_sovereign_phase_5_init
execute if entity @s[tag=phantom_sovereign_phase_2,tag=!phantom_sovereign_phase_2.1_complete] unless entity @e[tag=phantom_sovereign_phase_2.1] run function kiwi2:custom_mobs_fx/phantom_sovereign_phase_2_2
execute if entity @s[tag=phantom_sovereign_phase_2,tag=phantom_sovereign_phase_2.1_complete,tag=!phantom_sovereign_phase_2.2_complete] unless entity @e[tag=phantom_sovereign_phase_2.2] run function kiwi2:custom_mobs_fx/phantom_sovereign_phase_2_3
execute if entity @s[tag=phantom_sovereign_phase_2,tag=phantom_sovereign_phase_2.1_complete,tag=phantom_sovereign_phase_2.2_complete,tag=!phantom_sovereign_phase_3] unless entity @e[tag=phantom_sovereign_phase_2.3] run function kiwi2:custom_mobs_fx/phantom_sovereign_phase_2_complete

# Phase 5 Desperation
execute if entity @s[tag=phantom_sovereign_phase_5] run scoreboard players add @s phantom_sovereign_phase_5_timer 1
execute if entity @s[tag=phantom_sovereign_phase_5] if score @s phantom_sovereign_phase_5_timer matches 1..4200 run function kiwi2:custom_mobs_fx/phantom_sovereign_phase_5_time