#Powerups
execute as @a[scores={snowball=1..}] run function mctsts:game/items/fireball
function mctsts:game/items/teleport
function mctsts:game/items/arrow
execute as @a[nbt={SelectedItem:{id:"minecraft:carrot_on_a_stick",tag:{Damage:6}}},scores={carrot=1..}] run function mctsts:game/items/feather
execute as @a[nbt={SelectedItem:{id:"minecraft:carrot_on_a_stick",tag:{Damage:7}}},scores={carrot=1..}] run function mctsts:game/items/switch
execute as @a[nbt={SelectedItem:{id:"minecraft:carrot_on_a_stick",tag:{CustomModelData:1}}},scores={carrot=1..}] run function mctsts:game/items/gravity
execute as @a[nbt={SelectedItem:{id:"minecraft:carrot_on_a_stick",tag:{CustomModelData:2}}},scores={carrot=1..}] run function mctsts:game/items/chorus
execute as @a[nbt={SelectedItem:{id:"minecraft:carrot_on_a_stick",tag:{CustomModelData:3}}},scores={carrot=1..}] run function mctsts:game/items/close
execute as @a[nbt={SelectedItem:{id:"minecraft:carrot_on_a_stick",tag:{CustomModelData:4}}},scores={carrot=1..}] run function mctsts:game/items/tnt
execute as @a run function mctsts:game/name

# Spec Inv 
execute as @a[gamemode=adventure] at @s if entity @a[gamemode=spectator,distance=..1,team=gold] run function mctsts:game/spec_inv
execute as @a[gamemode=spectator,team=gold] at @s unless entity @a[gamemode=adventure,distance=..1] run clear @s

# Clear Levitation
scoreboard players remove @a[scores={clearL=0..}] clearL 1
effect clear @a[scores={clearL=0}] levitation

# Silverfish
execute as @e[type=minecraft:silverfish] run data merge entity @s {DeathTime:19s,Health:0.0f}

# Traps
execute as @a[nbt={SelectedItem:{id:"minecraft:carrot_on_a_stick",tag:{Damage:4}}},scores={carrot=1..}] at @s run function mctsts:game/items/trap_levitation
execute as @a[nbt={SelectedItem:{id:"minecraft:carrot_on_a_stick",tag:{Damage:5}}},scores={carrot=1..}] at @s run function mctsts:game/items/trap_tnt
scoreboard players add @e[tag=trap,type=armor_stand] trapTimer 1
execute as @e[tag=trap,type=armor_stand,scores={trapTimer=500}] at @s run particle minecraft:cloud ~ ~ ~ 0.3 0.3 0.3 0.1 10
execute as @e[tag=trap,type=armor_stand,scores={trapTimer=500}] at @s run particle minecraft:smoke ~ ~ ~ 0.3 0.3 0.3 0.1 4
execute as @e[tag=trap,type=armor_stand,scores={trapTimer=500}] at @s run particle minecraft:flame ~ ~ ~ 0.3 0.3 0.3 0.1 2
kill @e[tag=trap,type=armor_stand,scores={trapTimer=500}]
execute as @a[gamemode=adventure] at @s if entity @e[tag=trap,type=armor_stand,distance=..0.5] run function mctsts:game/items/trap

# Rocket Boots
execute as @a[nbt={Inventory:[{Slot:100b,id:"minecraft:leather_boots"}]}] run scoreboard players set @s vCharm 60
execute as @a[nbt={Inventory:[{Slot:100b,id:"minecraft:leather_boots"}]}] run scoreboard players set @s clearL 50
execute as @a[nbt={Inventory:[{Slot:100b,id:"minecraft:leather_boots"}]}] run effect give @s levitation 3 5 true
execute as @a[nbt={Inventory:[{Slot:100b,id:"minecraft:leather_boots"}]}] at @s run playsound minecraft:entity.blaze.death master @a ~ ~ ~ 1 0.7
execute as @a[nbt={Inventory:[{Slot:100b,id:"minecraft:golden_boots"}]}] run scoreboard players set @s vCharm 60
execute as @a[nbt={Inventory:[{Slot:100b,id:"minecraft:golden_boots"}]}] run scoreboard players set @s clearL 50
execute as @a[nbt={Inventory:[{Slot:100b,id:"minecraft:golden_boots"}]}] run effect give @s levitation 3 10 true
execute as @a[nbt={Inventory:[{Slot:100b,id:"minecraft:golden_boots"}]}] at @s run playsound minecraft:entity.blaze.death master @a ~ ~ ~ 1 0.7
execute as @a[nbt={Inventory:[{Slot:100b,id:"minecraft:diamond_boots"}]}] run scoreboard players set @s vCharm 60
execute as @a[nbt={Inventory:[{Slot:100b,id:"minecraft:diamond_boots"}]}] run scoreboard players set @s clearL 50
execute as @a[nbt={Inventory:[{Slot:100b,id:"minecraft:diamond_boots"}]}] run effect give @s levitation 3 15 true
execute as @a[nbt={Inventory:[{Slot:100b,id:"minecraft:diamond_boots"}]}] at @s run playsound minecraft:entity.blaze.death master @a ~ ~ ~ 1 0.7
item replace entity @a[nbt={Inventory:[{Slot:100b}]}] armor.feet with air

# Fishing Rod
clear @a[gamemode=adventure,nbt=!{Inventory:[{Slot:1b,id:"minecraft:fishing_rod",Count:1b}]}] fishing_rod
item replace entity @a[gamemode=adventure,nbt=!{Inventory:[{Slot:1b,id:"minecraft:fishing_rod",Count:1b}]}] hotbar.1 with fishing_rod{Unbreakable:1,HideFlags:63}
kill @e[type=item]

clear @a[gamemode=spectator] minecraft:carved_pumpkin

# Misc
team leave @a[team=!,scores={lives=1..}]
team join gold @a[scores={lives=..0},team=!gold]
execute as @a[gamemode=adventure] at @s unless block ~ ~ ~ #transparent run tp @s ~ ~1 ~

# Powerups
execute if score powerup Stats matches ..0 run scoreboard players set powerupX Stats -5
execute if score powerupX Stats matches 2 run effect clear @a[tag=powerupLevitation] levitation
execute if score powerupX Stats matches 2 run tag @a remove powerupLevitation
execute unless entity @e[tag=powerup] run scoreboard players add powerupX Stats 1
execute if score level Stats matches 1 if score powerupX Stats >= powerup Stats positioned -484.0 30.0 -484.0 run function mctsts:game/spawn_powerup
execute if score level Stats matches 2 if score powerupX Stats >= powerup Stats positioned -484.0 31.0 -484.0 run function mctsts:game/spawn_powerup
execute if score level Stats matches 3 if score powerupX Stats >= powerup Stats positioned -484.0 34.0 -484.0 run function mctsts:game/spawn_powerup
execute if score level Stats matches 4 if score powerupX Stats >= powerup Stats positioned -484.0 28.0 -484.0 run function mctsts:game/spawn_powerup
execute if score level Stats matches 5 if score powerupX Stats >= powerup Stats positioned -484.0 31.0 -484.0 run function mctsts:game/spawn_powerup
execute if score level Stats matches 6 if score powerupX Stats >= powerup Stats positioned -484.0 35.0 -484.0 run function mctsts:game/spawn_powerup
execute if score level Stats matches 7 if score powerupX Stats >= powerup Stats positioned -484.0 31.0 -484.0 run function mctsts:game/spawn_powerup
execute if score level Stats matches 8 if score powerupX Stats >= powerup Stats positioned -484.0 31.0 -484.0 run function mctsts:game/spawn_powerup
execute if score level Stats matches 9 if score powerupX Stats >= powerup Stats positioned -484.0 34.0 -484.0 run function mctsts:game/spawn_powerup
execute if score level Stats matches 10 if score powerupX Stats >= powerup Stats positioned -484.0 30.5 -484.0 run function mctsts:game/spawn_powerup
execute if score level Stats matches 11 if score powerupX Stats >= powerup Stats positioned -484.0 35.0 -484.0 run function mctsts:game/spawn_powerup
execute if score level Stats matches 12 if score powerupX Stats >= powerup Stats positioned -484.0 37.0 -484.0 run function mctsts:game/spawn_powerup
execute if score powerupX Stats >= powerup Stats run scoreboard players set powerupX Stats 0
execute as @e[tag=powerup,type=armor_stand] at @s run tp @s ~ ~ ~ ~2 ~
execute as @e[tag=powerupUtil,type=armor_stand] at @s run tp @s ~ ~ ~ ~2 ~
execute as @a[gamemode=adventure] at @s positioned ~ ~0.66 ~ if entity @e[tag=powerup,distance=..0.5] run function mctsts:game/collect
execute as @a[gamemode=adventure] at @s positioned ~ ~0.33 ~ if entity @e[tag=powerup,distance=..0.5] run function mctsts:game/collect
execute as @a[gamemode=adventure] at @s positioned ~ ~-0.33 ~ if entity @e[tag=powerup,distance=..0.5] run function mctsts:game/collect
execute as @a[gamemode=adventure] at @s positioned ~ ~-0.66 ~ if entity @e[tag=powerup,distance=..0.5] run function mctsts:game/collect
execute as @a[gamemode=adventure] at @s positioned ~ ~-1.00 ~ if entity @e[tag=powerup,distance=..0.5] run function mctsts:game/collect
execute as @a[gamemode=adventure] at @s positioned ~ ~-1.33 ~ if entity @e[tag=powerup,distance=..0.5] run function mctsts:game/collect
execute if score pullable Stats matches 1.. as @e[tag=powerupSpawn] at @s unless entity @e[tag=powerup,distance=..1] run scoreboard players add powerupXX Stats 1
execute if score powerupXX Stats matches 150.. run function mctsts:game/remove_powerup

# Lives Display
scoreboard players set players Stats 0
execute as @a[scores={lives=1..},gamemode=!creative] run scoreboard players add players Stats 1
execute unless score players Stats = playersX Stats run scoreboard players reset * livesDis
scoreboard players operation playersX Stats = players Stats
execute as @a[gamemode=!creative,scores={lives=1..}] run scoreboard players operation @s livesDis = @s lives


# Timer
scoreboard players add time Stats 1
execute if score time Stats matches -100 run title @a subtitle [""]
execute if score time Stats matches -100 run title @a title ["",{"text":"5","color":"red"}]
execute if score time Stats matches -80 run title @a title ["",{"text":"4","color":"red"}]
execute if score time Stats matches -60 run title @a title ["",{"text":"3","color":"gold"}]
execute if score time Stats matches -40 run title @a title ["",{"text":"2","color":"gold"}]
execute if score time Stats matches -20 run title @a title ["",{"text":"1","color":"green"}]
execute if score time Stats matches 0 run title @a title ["",{"translate":"game.start","color":"green"}]
execute if score time Stats matches 0 run scoreboard players set playersX Stats -1
execute if score time Stats matches 0 run effect clear @a weakness

execute if score time Stats matches -100 as @a at @s run playsound minecraft:entity.arrow.shoot master @s ~ ~ ~ 1 2
execute if score time Stats matches -80 as @a at @s run playsound minecraft:entity.arrow.shoot master @s ~ ~ ~ 1 2
execute if score time Stats matches -60 as @a at @s run playsound minecraft:entity.arrow.shoot master @s ~ ~ ~ 1 2
execute if score time Stats matches -40 as @a at @s run playsound minecraft:entity.arrow.shoot master @s ~ ~ ~ 1 2
execute if score time Stats matches -20 as @a at @s run playsound minecraft:entity.arrow.shoot master @s ~ ~ ~ 1 2
execute if score time Stats matches 0 as @a at @s run playsound minecraft:entity.evoker.prepare_summon master @s ~ ~ ~ 1 1.6

#Arrow Fix
execute as @e[type=arrow] run data merge entity @s {Air:1s}
execute as @e[type=arrow] run data merge entity @s {Air:0s}
execute as @e[type=arrow] run data merge entity @s {Color:-1}
execute as @e[type=arrow] at @s run particle crit ~ ~ ~ 0.1 0.1 0.1 0 3
execute as @e[type=arrow] at @s run particle cloud ~ ~ ~ 0.1 0.1 0.1 0 1

# Lifetime
execute if score time Stats matches 0.. as @a[gamemode=adventure] run function mctsts:game/lifetime


#Last
scoreboard players reset @a carrot

# Death
#effect clear @a slow_falling
scoreboard players remove @a[scores={levitateX=1..}] levitateX 1
scoreboard players remove @a[scores={vCharm=1..}] vCharm 1
effect clear @a[scores={vCharm=7}] levitation
scoreboard players set @a[nbt={OnGround:1b}] levitate 0
execute if score time Stats matches 0.. positioned -484.0 5.0 -484.0 as @a[distance=..6,gamemode=adventure] if block ~ ~-1 ~ air if block ~ ~-2 ~ air if block ~ ~-3 ~ air if block ~ ~-4 ~ air if block ~ ~1 ~ air if block ~ ~2 ~ air unless score @s vCharm matches 1.. if entity @s[nbt={Inventory:[{id:"minecraft:beetroot_seeds"}]}] run scoreboard players set @s vCharm 20
execute if score time Stats matches 0.. positioned -484.0 5.0 -484.0 as @a[distance=..6,gamemode=adventure] if block ~ ~-1 ~ air if block ~ ~-2 ~ air if block ~ ~-3 ~ air if block ~ ~-4 ~ air if block ~ ~1 ~ air if block ~ ~2 ~ air if score @s vCharm matches 20 if entity @s[nbt={Inventory:[{id:"minecraft:beetroot_seeds"}]}] run effect give @s minecraft:levitation 1 50 true
execute if score time Stats matches 0.. positioned -484.0 5.0 -484.0 as @a[distance=..6,gamemode=adventure] if block ~ ~-1 ~ air if block ~ ~-2 ~ air if block ~ ~-3 ~ air if block ~ ~-4 ~ air if block ~ ~1 ~ air if block ~ ~2 ~ air if score @s vCharm matches 20 if entity @s[nbt={Inventory:[{id:"minecraft:beetroot_seeds"}]}] at @s run playsound minecraft:item.trident.return master @s ~ ~ ~ 1 0
execute if score time Stats matches 0.. positioned -484.0 5.0 -484.0 as @a[distance=..6,gamemode=adventure] if block ~ ~-1 ~ air if block ~ ~-2 ~ air if block ~ ~-3 ~ air if block ~ ~-4 ~ air if block ~ ~1 ~ air if block ~ ~2 ~ air if score @s vCharm matches 20 if entity @s[nbt={Inventory:[{id:"minecraft:beetroot_seeds"}]}] at @s run particle minecraft:enchanted_hit ~ ~ ~ 1 1 1 0.5 100
execute if score time Stats matches 0.. positioned -484.0 5.0 -484.0 as @a[distance=..6,gamemode=adventure] if block ~ ~-1 ~ air if block ~ ~-2 ~ air if block ~ ~-3 ~ air if block ~ ~-4 ~ air if block ~ ~1 ~ air if block ~ ~2 ~ air if score @s vCharm matches 20 if entity @s[nbt={Inventory:[{id:"minecraft:beetroot_seeds"}]}] run clear @s beetroot_seeds 1

execute if score time Stats matches 0.. positioned -484.0 5.0 -484.0 as @a[distance=..6,gamemode=adventure] if block ~ ~-1 ~ air if block ~ ~-2 ~ air if block ~ ~-3 ~ air if block ~ ~-4 ~ air if block ~ ~1 ~ air if block ~ ~2 ~ air unless score @s vCharm matches 1.. run function mctsts:game/death
execute if score time Stats matches 0.. positioned -484.0 5.0 -484.0 as @a[distance=6..,gamemode=adventure] at @s unless score @s levitateX matches 1.. positioned ~ 5 ~ if entity @s[distance=..6] at @s if block ~ ~-1 ~ air if block ~ ~-2 ~ air if block ~ ~-3 ~ air if block ~ ~-4 ~ air if block ~ ~1 ~ air if block ~ ~2 ~ air run function mctsts:game/levitate
execute if score time Stats matches ..-1 positioned -484.0 5.0 -484.0 as @a[distance=..6,gamemode=adventure] if block ~ ~-1 ~ air if block ~ ~-2 ~ air if block ~ ~-3 ~ air if block ~ ~-4 ~ air if block ~ ~1 ~ air if block ~ ~2 ~ air run function mctsts:game/respawn
execute as @a[scores={respawn=0..}] run function mctsts:game/respawn_time
execute unless entity @a[scores={lives=1..}] run function mctsts:game/end
execute store result score players Stats if entity @a[scores={lives=1..}] 
execute if score players Stats matches 1 as @a[scores={lives=1..}] run function mctsts:game/win
execute as @a[gamemode=adventure] at @s positioned ~ -1000 ~ if entity @s[distance=..950] run function mctsts:game/death

# Timer 
scoreboard players add ltTimer Stats 1
execute if score ltTimer Stats matches 100.. run scoreboard players remove lifetimeX Stats 1
execute if score ltTimer Stats matches 100.. run scoreboard players set ltTimer Stats 0
scoreboard players operation lifetimeX Stats > 60 Const


# hole particles 
execute positioned -484.0 20.0 -484.0 run function mctsts:game/particles

# spectator
execute as @a[gamemode=spectator] at @s unless entity @p[gamemode=adventure,distance=..100] run tp @s @p[gamemode=adventure]

# fill structure_void
fill -486 18 -482 -483 18 -487 minecraft:structure_void
fill -487 18 -483 -482 18 -486 minecraft:structure_void