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

#Return to base
scoreboard players add @a trap_cooldown 0
scoreboard players remove @a[scores={trap_cooldown=1..}] trap_cooldown 1
execute as @a[scores={click=1..,trap_cooldown=0}] if entity @s[nbt={SelectedItem:{id:"minecraft:carrot_on_a_stick",tag:{CustomModelData:9}}}] at @s run function f:game/store/return
execute as @a[scores={click=1..,trap_cooldown=1..}] if entity @s[nbt={SelectedItem:{id:"minecraft:carrot_on_a_stick",tag:{CustomModelData:9}}}] at @s run function f:game/store/return_fail

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

#Misc
function f:game/score/score
execute if score $game_type main matches 0..1 if entity @p[tag=in_base] run function f:game/alarm/alarm
execute as @a[team=spec,x=24,y=247,z=84,distance=..10] run function f:spec-tp

#Quit
execute as @e[tag=player_indicator] at @s run function f:game/player_indicator

#Powerup
execute as @e[tag=powerup] at @s run function f:game/powerup/powerup
execute as @a[tag=get_powerup] at @s run function f:game/powerup/player

#Pumpkins
execute if score $game_type main matches 0..1 run function f:game/pumpkin/main

#Emergency game end
scoreboard players set $core team-count 0
execute if entity @a[team=blue] run scoreboard players add $core team-count 1
execute if entity @a[team=green] run scoreboard players add $core team-count 1
execute if entity @a[team=yellow] run scoreboard players add $core team-count 1
execute if entity @a[team=red] run scoreboard players add $core team-count 1
execute if entity @a[team=hider] run scoreboard players add $core team-count 1
execute if entity @a[team=seeker] run scoreboard players add $core team-count 1
execute if entity @a[tag=test,team=!spec] run scoreboard players set $core team-count 2

execute if score timer time matches 1.. if score $core team-count matches ..1 run function f:game/end/end

#Win effects
execute at @e[tag=hearts] run particle minecraft:heart ~ ~ ~ 0 0 0 0 1 force @a
execute if entity @p[scores={wineffect_select=2},tag=win] if score timer time matches -10..0 run function f:game/end/win/effect2
execute if entity @e[tag=dab] as @a[scores={wineffect_select=3},limit=1] run function f:game/end/win/effect3_anim
execute as @a[scores={wineffect_select=4},tag=win] at @s run function f:game/end/win/effect4
execute as @e[tag=win6,nbt={HurtTime:10s}] at @s run function f:game/end/win/effect6
execute as @a[scores={wineffect_select=8},tag=win] run function f:game/end/win/effect8

#Timer
execute if score timer time matches ..1201 run function f:game/timer/main

#Store & items
execute if score $game_type main matches 0..2 run function f:game/store/main

#Other
effect clear @a weakness
execute as @a[tag=player] at @s facing entity @p[distance=0.1..5] feet positioned ^ ^ ^1 rotated as @s positioned ^ ^ ^-1 if entity @s[distance=..0.45] run effect give @s minecraft:weakness 1000000 250 true
function f:game/armor

setblock 88 150 122 minecraft:birch_trapdoor[facing=east,open=true]
setblock 85 149 119 minecraft:birch_trapdoor[facing=east,open=true]
setblock 85 149 115 minecraft:birch_trapdoor[facing=east,open=true]
setblock 86 149 109 minecraft:birch_trapdoor[facing=east,open=true]
setblock 80 148 112 minecraft:birch_trapdoor[facing=east,open=true]
setblock 82 148 110 minecraft:birch_trapdoor[facing=east,open=true]
setblock 81 148 108 minecraft:birch_trapdoor[facing=east,open=true]
setblock 87 150 102 minecraft:birch_trapdoor[facing=east,open=true]
setblock -13 149 54 minecraft:birch_trapdoor[facing=east,open=true]
setblock -10 149 57 minecraft:birch_trapdoor[facing=east,open=true]
setblock -9 149 60 minecraft:birch_trapdoor[facing=east,open=true]
setblock -12 149 62 minecraft:birch_trapdoor[facing=east,open=true]
setblock -15 149 65 minecraft:birch_trapdoor[facing=east,open=true]
setblock 78 148 114 minecraft:birch_trapdoor[facing=west,open=true]
setblock -26 148 65 minecraft:birch_trapdoor[facing=west,open=true]
setblock -27 148 63 minecraft:birch_trapdoor[facing=west,open=true]
setblock -29 148 60 minecraft:birch_trapdoor[facing=west,open=true]
setblock -31 149 56 minecraft:birch_trapdoor[facing=west,open=true]
setblock -26 148 51 minecraft:birch_trapdoor[facing=west,open=true]
setblock -22 148 46 minecraft:birch_trapdoor[facing=west,open=true]
setblock -21 148 49 minecraft:birch_trapdoor[facing=west,open=true]


#Death
execute as @a[nbt={Air:0s},scores={kill-timer=0},team=!seeker] run function f:game/kill/kill

#Kill animation
scoreboard players add @e[tag=kill-timer] kill-timer 1
execute as @e[tag=kill-timer] at @s run tp @s ~ ~0.05 ~ ~2 90
execute as @a[scores={kill-timer=1..}] at @s run spectate @e[tag=kill-timer,distance=..2.5,limit=1,sort=nearest]
kill @e[tag=kill-timer,scores={kill-timer=50}]
scoreboard players add @a[scores={kill-timer=1..}] kill-timer 1
execute as @a[scores={kill-timer=50..}] at @s run function f:game/kill/select

#Activity sensor
tag @a remove hold_sensor
execute if score timer time matches 1.. if score $game_type main matches 0..2 run item replace entity @a[tag=player,gamemode=!spectator,scores={kill-timer=0}] hotbar.7 with minecraft:feather{display:{Name:'{"translate":"activity_sensor.name","color":"aqua","bold":true,"italic":false}'}}
execute if score timer time matches 1.. if score $game_type main matches 0..2 as @a[tag=player,gamemode=!spectator,scores={kill-timer=0},nbt={SelectedItemSlot:7}] at @s run function f:game/activity_sensor/toggle

#Compass
execute if score timer time matches 1.. run function f:game/compass/async

#Bossbar
bossbar set minecraft:display players @a
execute store result bossbar minecraft:display value run scoreboard players get $core id

execute if score $core id matches ..40 run scoreboard players remove $core id 1
execute if score $core id matches ..0 run scoreboard players set $core id 41
execute if score $core id matches 41.. run bossbar set minecraft:display name ["",{"text":"Halloween Chaos","color":"yellow","bold":true}]

#Snowball
execute at @e[type=minecraft:snowball] run particle minecraft:cloud ~ ~-0.3 ~ 0.1 0.1 0.1 0.05 2 force @a[distance=2..50]
execute as @e[type=snowball,tag=!idconfirm] at @s run function f:game/snowball/id
execute as @e[type=snowball] at @s run function f:game/snowball/kill

#Snowball cooldown
scoreboard players add @a cooldown 0
scoreboard players set @a[tag=!player] cooldown 0
execute as @a[scores={cooldown=1..},team=!hider] at @s run function f:game/snowball/cooldown
clear @a[nbt={SelectedItem:{id:"minecraft:carrot_on_a_stick"}}] snowball
execute as @a[tag=!hold_compass,tag=!hold_sensor,scores={cooldown=0,kill-timer=0},tag=player,team=!hider] unless entity @s[nbt={SelectedItem:{id:"minecraft:carrot_on_a_stick"}}] run item replace entity @s weapon.offhand with minecraft:snowball{display:{Name:'""'}}

scoreboard players set @a click 0

#Gold
execute as @a[scores={goldmined=1..}] at @s run function f:game/gold/mined
execute if score timer time matches 1.. run item replace entity @a[gamemode=!spectator,tag=player,scores={kill-timer=0},team=!hider] hotbar.4 with minecraft:iron_pickaxe{CanDestroy:["minecraft:red_nether_bricks"],display:{Name:'{"translate":"game.pickaxe","color":"gray","bold":true,"italic":false}'},HideFlags:14,Unbreakable:1b} 1

scoreboard players add @e[tag=gold_cooldown] time 1
execute at @e[tag=gold_cooldown,scores={time=2400}] run setblock ~ ~ ~ red_nether_bricks
kill @e[tag=gold_cooldown,scores={time=2400..}]
