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

#Load entities
execute unless entity @e[tag=option] run function f:lobby/load

#Creators
execute as @e[tag=head] at @s run tp @s ~ ~ ~ 90 0
execute if entity @p[x=337,y=105,z=-319,distance=..20] run function f:lobby/creators
execute as @a[x=337,y=105,z=-319,distance=..5,tag=!text] at @s run playsound minecraft:entity.experience_orb.pickup master @s
tellraw @a[x=337,y=105,z=-319,distance=..5,tag=!text] ["",{"text":"\n> ","color":"gray"},{"text":"Check out our social media!\n\n","color":"green","bold":true},{"text":" Website: ","bold":true,"color":"none"},{"text":"skilendarz.com\n","color":"gold","clickEvent":{"action":"open_url","value":"https://skilendarz.com"},"hoverEvent":{"action":"show_text","value":{"text":"","extra":[{"text":"https://skilendarz.com","color":"gray"}]}},"bold":false},{"text":" Twitter: ","bold":true,"color":"none"},{"text":"twitter.com/skilendarz\n","color":"gold","clickEvent":{"action":"open_url","value":"https://twitter.com/skilendarz"},"hoverEvent":{"action":"show_text","value":{"text":"","extra":[{"text":"https://twitter.com/skilendarz","color":"gray"}]}},"bold":false},{"text":" Discord: ","bold":true,"color":"none"},{"text":"discord.gg/ZEdzVTR\n","color":"gold","clickEvent":{"action":"open_url","value":"https://discord.gg/ZEdzVTR"},"hoverEvent":{"action":"show_text","value":{"text":"","extra":[{"text":"https://discord.gg/ZEdzVTR","color":"gray"}]}},"bold":false}]
tag @a[x=337,y=105,z=-319,distance=..5,tag=!text] add text
execute unless entity @p[x=337,y=105,z=-319,distance=..20] if entity @e[tag=body,scores={time=1..}] run function f:lobby/creators
execute if entity @e[tag=dab] as @p[tag=creator] run function f:game/end/win/effect3_anim

#Misc
tp @a[x=317,y=100,z=-317,distance=..1.5] 317 104 -317
tp @a[x=328,y=102,z=-328,distance=..1.5] 328 107 -328
tp @a[x=323,y=100,z=-343,distance=..2] 212 166 -399 -90 -90
title @a[x=319,y=107,z=-349,distance=..2] actionbar {"text":""}
effect give @a[team=lobby] speed 1 1 true
execute as @a[team=lobby] at @s run particle minecraft:firework ~ ~ ~ 20 20 20 0 10 force @s
execute as @e[tag=lobbyp] at @s unless entity @p[distance=..3] run tp @s ~ ~ ~ facing entity @p[distance=..40]
execute as @e[tag=lb_win_select] at @s run function f:game/store/villager
effect give @e[tag=BlockFP] invisibility 1000000 100 true

#Win effects, Info & Stats
clear @a[team=lobby,x=311,y=110,z=-285,distance=17..] carrot_on_a_stick{Limit:1}
item replace entity @a[team=lobby,x=311,y=110,z=-285,distance=..17] weapon.offhand with minecraft:carrot_on_a_stick{display:{Name:'""'},Limit:1}
execute positioned 313 104 -339 as @a[distance=..6] at @s run function f:lobby/win_effects/main
execute positioned 320 110 -288 if entity @p[distance=..20] run function f:lobby/info/info
function f:data/stats/main

#Parkour
execute at @e[tag=parkour_detect] run particle minecraft:witch ~ ~0.1 ~ 0 0 0 0 0
execute at @e[tag=parkour_max,scores={time=0}] if entity @p[distance=..0.8,tag=!start_parkour] run tag @p add start_parkour
execute if entity @p[tag=start_parkour] as @e[tag=parkour_max] at @s run function f:lobby/parkour/parkour
execute if entity @e[tag=parkour_max,scores={time=1..}] unless entity @p[tag=start_parkour] run function f:lobby/parkour/reset

effect give @a[tag=parkour_winner] minecraft:jump_boost 1 4 true
effect give @a[tag=parkour_winner] minecraft:speed 1 3 true
execute at @a[tag=parkour_winner] run particle minecraft:composter ~ ~2 ~
setblock 327 104 -331 dirt
setblock 332 106 -339 coarse_dirt

#Options
execute as @e[tag=cauldron,scores={time=..20}] at @s run function f:lobby/options/click
execute as @e[tag=cauldron,scores={time=1..}] at @s run function f:lobby/options/cauldron
title @a[x=314,y=107,z=-347,dx=10,dy=2,dz=7] actionbar {"translate":"option.info","color":"yellow"}
execute if entity @p[team=lobby] as @e[tag=option_selected] at @s run function f:lobby/options/display

effect give @e[tag=option_game_villager] invisibility 1000000 100 true
execute unless score $game_type time matches 1.. at @a[scores={vill=1..},x=315,y=107,z=-345,distance=..6] run function f:lobby/options/game_type/click
execute as @e[tag=option_rotation,scores={time=1..}] run function f:lobby/options/game_type/rotate
execute if score $game_type time matches 1.. run function f:lobby/options/game_type/update
scoreboard players set @a vill 0

#Witch
execute as @e[tag=floor_block] run data merge entity @s {Time:-32000}
execute as @e[tag=start_game] at @s run tp @s ~ ~ ~ facing entity @p
execute as @e[tag=start_particles] at @s run tp @s ~ ~ ~ ~10 ~
execute as @e[tag=start_particles] at @s run particle witch ^ ^ ^0.4 0 0 0 0 1
execute as @e[tag=start_particles2] at @s run tp @s ~ ~ ~ ~-10 ~
execute as @e[tag=start_particles2] at @s run particle enchant ^ ^ ^0.4 0 0 0 0 1

#Lock start
execute unless block 319 108 -348 air as @a[x=319,y=107,z=-345,distance=..5,scores={chest=1..}] at @s run playsound minecraft:entity.enderman.teleport player @s ~ ~ ~ 1000000
execute unless block 319 108 -348 air at @a[x=319,y=107,z=-345,distance=..5,scores={chest=1..}] run particle minecraft:dragon_breath 316 111.5 -289 0 0 0 0.5 200 force @a
execute unless block 319 108 -348 air run tp @a[x=319,y=107,z=-345,distance=..5,scores={chest=1..}] 316 110 -289 -90 0

#Start
execute if score start time matches 0 run tp @a[x=315,y=101,z=-354,dx=6,dy=1,dz=6] 318 107 -351
execute if score start time matches 0 if entity @p[x=318,y=107,z=-354,distance=..6,scores={chest=1..}] run scoreboard players set start time 1

scoreboard players set @a[x=318,y=107,z=-354,distance=..80,scores={chest=1..}] chest 0
execute if score start time matches 1.. run function f:lobby/start/start

#Resourcepack
execute if score start time matches 0 run title @a[x=317,y=104,z=-317,distance=5..20,team=lobby] title {"translate":"ERROR","color":"red"}
execute if score start time matches 0 run title @a[x=317,y=104,z=-317,distance=5..20,team=lobby] subtitle {"translate":"This map requires a resourcepack.","color":"gray"}

#Lock start
execute unless score info main matches 0 if entity @p[x=315,y=109,z=-295,dx=8,dy=10,dz=13] unless block 319 108 -348 air run fill 318 108 -347 320 108 -347 air
execute unless score info main matches 0 if entity @p[x=315,y=109,z=-295,dx=8,dy=10,dz=13] unless block 319 108 -348 air as @e[tag=game_start_locked] run data merge entity @s {CustomNameVisible:0}
execute unless score info main matches 0 if entity @p[x=315,y=109,z=-295,dx=8,dy=10,dz=13] unless block 319 108 -348 air run fill 320 107 -348 318 109 -348 air destroy
