# Wave Stats
    # Highest scores
execute if score world Time matches 23950 run scoreboard players set top_player stats_highest_damage_dealt 0
execute if score world Time matches 23950 run scoreboard players set top_player stats_highest_damage_taken 0
execute if score world Time matches 23950 run scoreboard players set top_player stats_highest_damage_blocked 0
execute if score world Time matches 23950 run scoreboard players set top_player stats_highest_mobs_killed 0
execute if score world Time matches 23950 run scoreboard players set top_player stats_highest_projectiles_shot 0

execute if score world Time matches 23950 as @a at @s run scoreboard players operation @s stats_player_projectiles_fired_bow += @s stats_player_projectiles_fired_crossbow

    # Sort by highest
execute if score world Time matches 23951..23998 as @r[tag=!damage_dealt_checked,tag=!highest_damage_dealt] at @s if score @s stats_player_damage_dealt > top_player stats_highest_damage_dealt run function kiwi2:wave_stats/highest_damage_dealt_tag
execute if score world Time matches 23951..23998 as @r[tag=!damage_taken_checked,tag=!highest_damage_taken] at @s if score @s stats_player_damage_taken > top_player stats_highest_damage_taken run function kiwi2:wave_stats/highest_damage_taken_tag
execute if score world Time matches 23951..23998 as @r[tag=!damage_blocked_checked,tag=!highest_damage_blocked] at @s if score @s stats_player_damage_blocked > top_player stats_highest_damage_blocked run function kiwi2:wave_stats/highest_damage_blocked_tag
execute if score world Time matches 23951..23998 as @r[tag=!mobs_killed_checked,tag=!highest_mobs_killed] at @s if score @s stats_player_mobs_killed > top_player stats_highest_mobs_killed run function kiwi2:wave_stats/highest_mobs_killed_tag
execute if score world Time matches 23951..23998 as @r[tag=!projectiles_shot_checked,tag=!highest_projectiles_shot] at @s if score @s stats_player_projectiles_fired_bow > top_player stats_highest_projectiles_shot run function kiwi2:wave_stats/highest_projectiles_shot_tag

    # Divide to show real values
execute if score world Time matches 23999 as @a at @s run scoreboard players operation @s stats_player_damage_dealt /= @s stats_divide_by_10
execute if score world Time matches 23999 as @a at @s run scoreboard players operation @s stats_player_damage_taken /= @s stats_divide_by_10
execute if score world Time matches 23999 as @a at @s run scoreboard players operation @s stats_player_damage_blocked /= @s stats_divide_by_10

execute if score world Time matches 23999 run tellraw @a {"bold":true,"italic":false,"text":"Wave Stats"}
execute if score world Time matches 23999 run tellraw @a {"color":"white","text":"----------------------------------------------"}

execute if score world Time matches 23980 store result score world stats_tellraw_roll run random value 1..5
execute if score world Time matches 23999 if score world stats_tellraw_roll matches 1 as @a[tag=highest_damage_dealt] run tellraw @a [{"color":"red","italic":false,"text":"Highest Damage Dealer "},{"text":"[","color":"green"},{"selector":"@s"},{"text":"]: ","color":"green"},{"score":{"name":"@s","objective":"stats_player_damage_dealt"},"color": "white"},{"text": " Damage Dealt","color": "white","color": "white"}]
execute if score world Time matches 23999 if score world stats_tellraw_roll matches 2 as @a[tag=highest_damage_dealt] run tellraw @a [{"color":"red","italic":false,"text":"The DPS "},{"text":"[","color":"green"},{"selector":"@s"},{"text":"]: ","color":"green"},{"score":{"name":"@s","objective":"stats_player_damage_dealt"},"color": "white"},{"text": " Damage Dealt","color": "white"}]
execute if score world Time matches 23999 if score world stats_tellraw_roll matches 3 as @a[tag=highest_damage_dealt] run tellraw @a [{"color":"red","italic":false,"text":"Berserker "},{"text":"[","color":"green"},{"selector":"@s"},{"text":"]: ","color":"green"},{"score":{"name":"@s","objective":"stats_player_damage_dealt"},"color": "white"},{"text": " Damage Dealt","color": "white"}]
execute if score world Time matches 23999 if score world stats_tellraw_roll matches 4 as @a[tag=highest_damage_dealt] run tellraw @a [{"color":"red","italic":false,"text":"Pain Distributor "},{"text":"[","color":"green"},{"selector":"@s"},{"text":"]: ","color":"green"},{"score":{"name":"@s","objective":"stats_player_damage_dealt"},"color": "white"},{"text": " Damage Dealt","color": "white"}]
execute if score world Time matches 23999 if score world stats_tellraw_roll matches 5 as @a[tag=highest_damage_dealt] run tellraw @a [{"color":"red","italic":false,"text":"The Big Hurt "},{"text":"[","color":"green"},{"selector":"@s"},{"text":"]: ","color":"green"},{"score":{"name":"@s","objective":"stats_player_damage_dealt"},"color": "white"},{"text": " Damage Dealt","color": "white"}]
execute if score world Time matches 23999 store result score world stats_tellraw_roll run random value 1..5
execute if score world Time matches 23999 if score world stats_tellraw_roll matches 1 as @a[tag=highest_damage_taken] run tellraw @a [{"color":"dark_green","italic":false,"text":"Most Damage Taken "},{"text":"[","color":"green"},{"selector":"@s"},{"text":"]: ","color":"green"},{"score":{"name":"@s","objective":"stats_player_damage_taken"},"color": "white"},{"text": " Damage Tanked","color": "white"}]
execute if score world Time matches 23999 if score world stats_tellraw_roll matches 2 as @a[tag=highest_damage_taken] run tellraw @a [{"color":"dark_green","italic":false,"text":"The Tank "},{"text":"[","color":"green"},{"selector":"@s"},{"text":"]: ","color":"green"},{"score":{"name":"@s","objective":"stats_player_damage_taken"},"color": "white"},{"text": " Damage Tanked","color": "white"}]
execute if score world Time matches 23999 if score world stats_tellraw_roll matches 3 as @a[tag=highest_damage_taken] run tellraw @a [{"color":"dark_green","italic":false,"text":"HP King "},{"text":"[","color":"green"},{"selector":"@s"},{"text":"]: ","color":"green"},{"score":{"name":"@s","objective":"stats_player_damage_taken"},"color": "white"},{"text": " Damage Tanked","color": "white"}]
execute if score world Time matches 23999 if score world stats_tellraw_roll matches 4 as @a[tag=highest_damage_taken] run tellraw @a [{"color":"dark_green","italic":false,"text":"Pain Magnet "},{"text":"[","color":"green"},{"selector":"@s"},{"text":"]: ","color":"green"},{"score":{"name":"@s","objective":"stats_player_damage_taken"},"color": "white"},{"text": " Damage Tanked","color": "white"}]
execute if score world Time matches 23999 if score world stats_tellraw_roll matches 5 as @a[tag=highest_damage_taken] run tellraw @a [{"color":"dark_green","italic":false,"text":"The Fortress "},{"text":"[","color":"green"},{"selector":"@s"},{"text":"]: ","color":"green"},{"score":{"name":"@s","objective":"stats_player_damage_taken"},"color": "white"},{"text": " Damage Tanked","color": "white"}]
execute if score world Time matches 23999 store result score world stats_tellraw_roll run random value 1..5
execute if score world Time matches 23999 if score world stats_tellraw_roll matches 1 as @a[tag=highest_damage_blocked] run tellraw @a [{"color":"gray","italic":false,"text":"Most Damage Blocked "},{"text":"[","color":"green"},{"selector":"@s"},{"text":"]: ","color":"gray"},{"score":{"name":"@s","objective":"stats_player_damage_blocked"},"color": "white"},{"text": " Damage Blocked","color": "white"}]
execute if score world Time matches 23999 if score world stats_tellraw_roll matches 2 as @a[tag=highest_damage_blocked] run tellraw @a [{"color":"gray","italic":false,"text":"The Guardian "},{"text":"[","color":"green"},{"selector":"@s"},{"text":"]: ","color":"gray"},{"score":{"name":"@s","objective":"stats_player_damage_blocked"},"color": "white"},{"text": " Damage Blocked","color": "white"}]
execute if score world Time matches 23999 if score world stats_tellraw_roll matches 3 as @a[tag=highest_damage_blocked] run tellraw @a [{"color":"gray","italic":false,"text":"Shield Master "},{"text":"[","color":"green"},{"selector":"@s"},{"text":"]: ","color":"gray"},{"score":{"name":"@s","objective":"stats_player_damage_blocked"},"color": "white"},{"text": " Damage Blocked","color": "white"}]
execute if score world Time matches 23999 if score world stats_tellraw_roll matches 4 as @a[tag=highest_damage_blocked] run tellraw @a [{"color":"gray","italic":false,"text":"Ironclad Knight "},{"text":"[","color":"green"},{"selector":"@s"},{"text":"]: ","color":"gray"},{"score":{"name":"@s","objective":"stats_player_damage_blocked"},"color": "white"},{"text": " Damage Blocked","color": "white"}]
execute if score world Time matches 23999 if score world stats_tellraw_roll matches 5 as @a[tag=highest_damage_blocked] run tellraw @a [{"color":"gray","italic":false,"text":"The Gatekeeper "},{"text":"[","color":"green"},{"selector":"@s"},{"text":"]: ","color":"gray"},{"score":{"name":"@s","objective":"stats_player_damage_blocked"},"color": "white"},{"text": " Damage Blocked","color": "white"}]
execute if score world Time matches 23999 store result score world stats_tellraw_roll run random value 1..5
execute if score world Time matches 23999 if score world stats_tellraw_roll matches 1 as @a[tag=highest_mobs_killed] run tellraw @a [{"color":"gold","italic":false,"text":"Most Mobs Killed "},{"text":"[","color":"green"},{"selector":"@s"},{"text":"]: ","color":"green"},{"score":{"name":"@s","objective":"stats_player_mobs_killed"},"color": "white"},{"text": " Mobs Killed","color": "white"}]
execute if score world Time matches 23999 if score world stats_tellraw_roll matches 2 as @a[tag=highest_mobs_killed] run tellraw @a [{"color":"gold","italic":false,"text":"The Executor "},{"text":"[","color":"green"},{"selector":"@s"},{"text":"]: ","color":"green"},{"score":{"name":"@s","objective":"stats_player_mobs_killed"},"color": "white"},{"text": " Mobs Killed","color": "white"}]
execute if score world Time matches 23999 if score world stats_tellraw_roll matches 3 as @a[tag=highest_mobs_killed] run tellraw @a [{"color":"gold","italic":false,"text":"Kill Count King "},{"text":"[","color":"green"},{"selector":"@s"},{"text":"]: ","color":"green"},{"score":{"name":"@s","objective":"stats_player_mobs_killed"},"color": "white"},{"text": " Mobs Killed","color": "white"}]
execute if score world Time matches 23999 if score world stats_tellraw_roll matches 4 as @a[tag=highest_mobs_killed] run tellraw @a [{"color":"gold","italic":false,"text":"The Grim Reaper "},{"text":"[","color":"green"},{"selector":"@s"},{"text":"]: ","color":"green"},{"score":{"name":"@s","objective":"stats_player_mobs_killed"},"color": "white"},{"text": " Mobs Killed","color": "white"}]
execute if score world Time matches 23999 if score world stats_tellraw_roll matches 5 as @a[tag=highest_mobs_killed] run tellraw @a [{"color":"gold","italic":false,"text":"Monster Hunter "},{"text":"[","color":"green"},{"selector":"@s"},{"text":"]: ","color":"green"},{"score":{"name":"@s","objective":"stats_player_mobs_killed"},"color": "white"},{"text": " Mobs Killed","color": "white"}]
execute if score world Time matches 23999 store result score world stats_tellraw_roll run random value 1..5
execute if score world Time matches 23999 if score world stats_tellraw_roll matches 1 as @a[tag=highest_projectiles_shot] run tellraw @a [{"color": "blue","italic":false,"text":"Most Projectiles Shot "},{"text":"[","color":"green"},{"selector":"@s"},{"text":"]: ","color":"green"},{"score":{"name":"@s","objective":"stats_player_projectiles_fired_bow"},"color": "white"},{"text": " Projectiles Shot","color": "white"}]
execute if score world Time matches 23999 if score world stats_tellraw_roll matches 2 as @a[tag=highest_projectiles_shot] run tellraw @a [{"color":"blue","italic":false,"text":"Arrow Dispenser "},{"text":"[","color":"green"},{"selector":"@s"},{"text":"]: ","color":"green"},{"score":{"name":"@s","objective":"stats_player_projectiles_fired_bow"},"color": "white"},{"text": " Projectiles Shot","color": "white"}]
execute if score world Time matches 23999 if score world stats_tellraw_roll matches 3 as @a[tag=highest_projectiles_shot] run tellraw @a [{"color":"blue","italic":false,"text":"The Hunter "},{"text":"[","color":"green"},{"selector":"@s"},{"text":"]: ","color":"green"},{"score":{"name":"@s","objective":"stats_player_projectiles_fired_bow"},"color": "white"},{"text": " Projectiles Shot","color": "white"}]
execute if score world Time matches 23999 if score world stats_tellraw_roll matches 4 as @a[tag=highest_projectiles_shot] run tellraw @a [{"color":"blue","italic":false,"text":"The Sniper "},{"text":"[","color":"green"},{"selector":"@s"},{"text":"]: ","color":"green"},{"score":{"name":"@s","objective":"stats_player_projectiles_fired_bow"},"color": "white"},{"text": " Projectiles Shot","color": "white"}]
execute if score world Time matches 23999 if score world stats_tellraw_roll matches 5 as @a[tag=highest_projectiles_shot] run tellraw @a [{"color":"blue","italic":false,"text":"Deadeye "},{"text":"[","color":"green"},{"selector":"@s"},{"text":"]: ","color":"green"},{"score":{"name":"@s","objective":"stats_player_projectiles_fired_bow"},"color": "white"},{"text": " Projectiles Shot","color": "white"}]

schedule function kiwi2:wave_stats/remove_stat_tags_objectives 20t