#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
#
#    This function has been created by Skilendarz Group.
#             https://skilendarz.com
#
#      GIVE MAP MAKERS THE CREDIT THEY DESERVE!
#             COPYING IS PROHIBITED!
#
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#

tag @s[nbt={Air:0s}] add drowned
execute at @s run kill @e[type=minecraft:snowball,distance=..2]
execute at @s run summon firework_rocket ~ ~1 ~ {LifeTime:1,FireworksItem:{id:"firework_rocket",Count:1,tag:{Fireworks:{Explosions:[{Type:0,Colors:[I;12844856,8485119],FadeColors:[I;16744760,4587500]}]}}}}

#Deathmatch
execute if score $game_type main matches 2 if entity @p[tag=killer,team=blue] run scoreboard players add $team_blue pumpkin_count 1
execute if score $game_type main matches 2 if entity @p[tag=killer,team=red] run scoreboard players add $team_red pumpkin_count 1
execute if score $game_type main matches 2 if entity @p[tag=killer,team=green] run scoreboard players add $team_green pumpkin_count 1
execute if score $game_type main matches 2 if entity @p[tag=killer,team=yellow] run scoreboard players add $team_yellow pumpkin_count 1

#Advancements
scoreboard players add @p[tag=killer] kills 1
scoreboard players add @p[tag=killer] game_kills 1

advancement grant @p[tag=killer] only f:slayer
advancement grant @p[tag=killer,scores={kills=100..}] only f:warrior
execute if score @s pumpkin_count matches 10.. run advancement grant @p[tag=killer] only f:halloween_tragedy
execute if score @s pumpkin_count matches 20.. run advancement grant @p[tag=killer] only f:unforgivable_crime
execute at @s run advancement grant @p[tag=killer,distance=50..] only f:sniper

#Pumpkin drops
execute if score @s pumpkin_count matches 1.. run function f:game/kill/drop

#Killer info
title @s[tag=!drowned] subtitle ["",{"selector":"@p[tag=killer]","bold":true},{"translate":"game.kills.you","color":"gray","bold":true}]
title @s[tag=drowned] subtitle ["",{"translate":"game.drowned.you","color":"gray","bold":true}]
execute if entity @s[tag=!drowned] run tellraw @a ["",{"text":"\n > ","color":"gold","bold":true},{"selector":"@p[tag=killer]","bold":true},{"translate":"game.kills","color":"gray"},{"selector":"@s","bold":true},{"text":"\n"}]
execute if entity @s[tag=drowned] run tellraw @a ["",{"text":"\n > ","color":"gold","bold":true},{"selector":"@s","bold":true},{"translate":"game.drowned","color":"gray"},{"text":"\n"}]

bossbar set minecraft:display max 40
execute if entity @s[tag=!drowned] run bossbar set minecraft:display name ["",{"selector":"@p[tag=killer]","bold":true},{"text":" ► ","color":"black","bold":true},{"selector":"@s","bold":true}]
execute if entity @s[tag=drowned] run bossbar set minecraft:display name ["",{"text":" X ","color":"red","bold":true},{"selector":"@s","bold":true},{"text":" X ","color":"red","bold":true}]
scoreboard players set $core id 40

#Seeker
execute as @p[team=seeker,tag=killer,tag=!original_seeker] at @s run playsound minecraft:entity.player.levelup master @s ~ ~ ~ 1 0.5
tellraw @p[team=seeker,tag=killer,tag=!original_seeker] ["",{"text":"\n"},{"translate":"game.qualify","color":"green","bold":true},{"text":"\n"}]
tag @p[team=seeker,tag=killer] add original_seeker

#Gravestone (override color)
scoreboard players add @s team_gravestone 0
scoreboard players set @s[team=blue] team_gravestone 1
scoreboard players set @s[team=red] team_gravestone 2
scoreboard players set @s[team=green] team_gravestone 3
scoreboard players set @s[team=yellow] team_gravestone 4
scoreboard players set @s[team=spec] team_gravestone 5
scoreboard players set @s[team=hider] team_gravestone 6
scoreboard players set @s[team=seeker] team_gravestone 6
team join gravestone @s

loot replace entity @s weapon.mainhand loot f:nick
function f:game/gravestone
execute at @e[tag=gravestone,limit=5,sort=random] run data modify block ~ ~ ~ Text2 set from entity @s SelectedItem.tag.display.Name
kill @e[tag=gravestone]

team join blue @s[scores={team_gravestone=1}]
team join red @s[scores={team_gravestone=2}]
team join green @s[scores={team_gravestone=3}]
team join yellow @s[scores={team_gravestone=4}]
team join spec @s[scores={team_gravestone=5}]
team join seeker @s[scores={team_gravestone=6}]
team leave @s[scores={team_gravestone=0}]

#Reset
tag @s add died
scoreboard players set @s game_kills 0
scoreboard players set @s poweruptime 0
scoreboard players set @s gold 0
scoreboard players set @s kill-timer 1
scoreboard players set @s milk_time 0

clear @s
gamemode spectator @s
execute at @s run summon armor_stand ~ ~1.7 ~ {NoGravity:1b,Invulnerable:1b,Marker:1b,Invisible:1b,Tags:["kill-timer"],Rotation:[0.0f,90.0f]}
playsound minecraft:item.chorus_fruit.teleport master @s ~ ~ ~ 300
playsound minecraft:entity.ghast.warn master @s ~ ~ ~ 300 0.7
title @s title ["",{"text":"✖","color":"red"}]

effect clear @s minecraft:unluck
effect clear @s minecraft:blindness
effect clear @s minecraft:slowness
effect clear @s minecraft:speed
effect clear @s minecraft:jump_boost
effect give @s minecraft:blindness 1 250 true

tag @a remove killer
tag @s remove drowned
