###Prepare Scores###
scoreboard players add @s nitSuccess 0
scoreboard players add @s nitResourceTime 0

##Spawn Tyrant if portals are destroyed###
execute unless entity @e[tag=nitWitherPortal,distance=..10] if score @s nitSuccess matches 0 run scoreboard players add @s nitResourceTime 1
execute if score @s nitResourceTime matches 1..199 run particle soul ~ ~1.75 ~ 0.4 0.75 0.4 0.01 10 normal
execute if score @s nitResourceTime matches 1..199 run playsound minecraft:ambient.soul_sand_valley.mood hostile @a ~ ~ ~ 1 1
execute if score @s nitResourceTime matches 200 if score @s nitSuccess matches 0 run stopsound @a hostile ambient.soul_sand_valley.mood
execute if score @s nitResourceTime matches 200 if score @s nitSuccess matches 0 run playsound entity.wither.spawn hostile @a ~ ~ ~ 5 1
execute if score @s nitResourceTime matches 200 if score @s nitSuccess matches 0 run function nitk:spawns/tyrant_overworld

###Control Tyrant and Skulls###
execute at @s run tp @e[type=wither,distance=..10,tag=nitTyrant] ~ ~ ~
execute if entity @e[tag=nitTyrant,distance=..10] run scoreboard players set @s nitSuccess 1

###Clear Condition###
execute if score @s nitSuccess matches 1.. unless entity @e[type=wither,distance=..10] run tag @s add nitCleared
execute if entity @s[tag=nitCleared] run advancement grant @p only nitk:nitk/tyrant_defeat
execute if entity @s[tag=nitCleared] run playsound entity.generic.explode hostile @a ~ ~ ~ 2 1
execute if entity @s[tag=nitCleared] run playsound entity.wither.death hostile @a ~ ~ ~ 7 1
execute if entity @s[tag=nitCleared] run particle explosion ~ ~ ~ 1 1 1 0 10 normal
execute if entity @s[tag=nitCleared] run particle soul ~ ~1.5 ~ 1 1 1 0.1 30 normal
execute if entity @s[tag=nitCleared] run kill @s