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

scoreboard players set $anim main 0

#Unload lobby
function f:lobby/unload

#Team & tp
tag @a[team=!lobby] add player
execute if score $game_type main matches 3 run tag @a add player

execute if score $game_type main matches 3 run team join seeker @a[team=lobby]
execute if score $game_type main matches 0..2 run team join spec @a[team=lobby]
team join blue @a[team=f-blue]
team join red @a[team=f-red]
team join green @a[team=f-green]
team join yellow @a[team=f-yellow]
team join hider @a[team=f-hider]

advancement grant @a[tag=player] only f:root
tag @a remove checked
execute if entity @a[team=random] run function f:game/start/team
execute unless entity @a[team=seeker] run team join seeker @r[team=hider,tag=!test]

tag @a remove checked
tp @a[team=spec] 23 154 91
tp @a[team=blue] -20 153 34
tp @a[team=red] 79 153 136
tp @a[team=green] 79 153 34
tp @a[team=yellow] -20 153 136
tp @a[team=hider] 29 141 81 0 0
tp @a[team=seeker] 21.5 145.0 -35.0 -90 0
tag @a remove parkour_winner

#Team suffix reset
team modify teamname7 prefix {"translate":"score.time","color":"gray"}

team modify teamname3 suffix {"text":" 0","bold":true,"color":"white"}
team modify teamname4 suffix {"text":" 0","bold":true,"color":"white"}
team modify teamname5 suffix {"text":" 0","bold":true,"color":"white"}
team modify teamname6 suffix {"text":" 0","bold":true,"color":"white"}
team modify teamname11 suffix {"text":" 0","bold":true,"color":"white"}
team modify teamname13 suffix {"text":" 0","bold":true,"color":"white"}
team modify teamname15 suffix {"text":" 0","bold":true,"color":"white"}
team modify teamname10 suffix {"text":" 0","bold":true,"color":"white"}

#Alarm reset
fill 81 153 36 81 154 36 trapped_chest{Lock:"asd"}
fill -18 153 32 -18 154 32 trapped_chest{Lock:"asd"}
fill -22 153 134 -22 154 134 trapped_chest{Lock:"asd"}
fill 77 153 138 77 154 138 trapped_chest{Lock:"asd"}
execute as @e[tag=alarm] at @s run tp @s ~ 153 ~
execute as @e[tag=alarm] run data merge entity @s {CustomNameVisible:1}
execute as @e[tag=trapinfo] at @s run tp @s ~ 154.85 ~
execute as @e[tag=trapinfo] run data merge entity @s {CustomNameVisible:1}

#Shop reset
fill 25 148 141 25 150 141 ender_chest
fill 58 148 122 58 150 122 ender_chest
fill 59 149 96 59 151 96 ender_chest
fill 63 149 53 63 151 53 ender_chest
fill 18 150 28 18 152 28 ender_chest
fill -3 148 54 -3 150 54 ender_chest
fill -1 148 94 -1 150 94 ender_chest
fill -8 149 111 -8 151 111 ender_chest

execute as @e[tag=shopkeeper,tag=y150] at @s run tp @s ~ 150 ~
execute as @e[tag=shopkeeper,tag=y149] at @s run tp @s ~ 149 ~
execute as @e[tag=shopkeeper,tag=y148] at @s run tp @s ~ 148 ~
execute as @e[tag=shopkeeper] run data merge entity @s {CustomNameVisible:1}

#Sidebar
scoreboard objectives remove score_blue
scoreboard objectives remove score_red
scoreboard objectives remove score_green
scoreboard objectives remove score_yellow
scoreboard objectives remove score_spec

scoreboard objectives add score_blue dummy {"text":"Halloween Chaos","bold":true,"color":"yellow"}
scoreboard objectives add score_red dummy {"text":"Halloween Chaos","bold":true,"color":"yellow"}
scoreboard objectives add score_green dummy {"text":"Halloween Chaos","bold":true,"color":"yellow"}
scoreboard objectives add score_yellow dummy {"text":"Halloween Chaos","bold":true,"color":"yellow"}
scoreboard objectives add score_spec dummy {"text":"Halloween Chaos","bold":true,"color":"yellow"}

scoreboard objectives setdisplay sidebar.team.blue score_blue
scoreboard objectives setdisplay sidebar.team.dark_red score_red
scoreboard objectives setdisplay sidebar.team.dark_green score_green
scoreboard objectives setdisplay sidebar.team.yellow score_yellow
scoreboard objectives setdisplay sidebar.team.dark_gray score_spec

#Scores
scoreboard players set $core main 1
scoreboard players set pumpkin main 0
scoreboard players set timer time 601
scoreboard players set timer tick 1000
scoreboard players set $1000 main 1000

scoreboard players set @a cooldown 2
scoreboard players set @a gold 0
scoreboard players set @a stolen 0

#Reset id
scoreboard players set @a id 0
scoreboard players set $id id 0
execute as @a[tag=player] run function f:game/id

#Tags
tag @a remove died
tag @a add start_no_title

#Other
bossbar set minecraft:display visible true
clear @a
gamemode adventure @a[tag=player]
title @a reset
title @a times 10 80 10
function f:game/gold/spawn
setblock -8 149 112 minecraft:spruce_slab[type=top]

execute as @a at @s run playsound minecraft:entity.wither.death master @s ~ ~ ~ 300 1.7
execute as @a at @s run playsound minecraft:item.totem.use master @s ~ ~ ~ 300
execute as @a at @s run playsound minecraft:entity.player.levelup master @s ~ ~ ~ 300 0.3

title @a title {"translate":"game.start.1","color":"green","bold":true}
execute as @a[tag=player] run title @s subtitle ["",{"translate":"game.start.2","color":"gray"},{"selector":"@s"},{"text":"!","color":"gray"}]

#Game modes
execute if score $game_type main matches 0 run function f:game/start/clasic
execute if score $game_type main matches 1 run function f:game/start/capture_the_pumpkin
execute if score $game_type main matches 2 run function f:game/start/deathmatch
execute if score $game_type main matches 3 run function f:game/start/hide_and_seek
