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

#Collect
playsound minecraft:block.note_block.pling master @a ~ ~ ~ 1 2
playsound minecraft:entity.player.levelup master @a ~ ~ ~ 1 2
playsound minecraft:block.beacon.power_select master @a ~ ~ ~ 1

kill @e[tag=powerup]
function f:game/powerup/summon
particle minecraft:dragon_breath ~ ~1.3 ~ 0 0 0 0.1 100

#Summon random
summon marker ~ ~1 ~ {Tags:["random","random1"]}
summon marker ~ ~1 ~ {Tags:["random","random2"]}
summon marker ~ ~1 ~ {Tags:["random","random3"]}
execute if score $game_type main matches 0..1 run summon marker ~ ~1 ~ {Tags:["random","random4"]}
summon marker ~ ~1 ~ {Tags:["random","random5"]}
summon marker ~ ~1 ~ {Tags:["random","random6"]}
tag @e[tag=random,limit=1,sort=random] add selected
title @s times 5 20 5

#Bossbar
bossbar set minecraft:display max 20
bossbar set minecraft:display name ["",{"selector":"@s","bold":true},{"translate":"powerup.get","color":"white","bold":true}]
scoreboard players set $core id 20

#Activity sensor
tag @s add get_powerup
execute if score @s milk_time matches 0 as @a[scores={activity_sensor=0,kill-timer=..0},tag=!get_powerup,tag=player] run function f:game/activity_sensor/info
execute if score @s milk_time matches 0 as @a[scores={activity_sensor=3,kill-timer=..0},tag=!get_powerup,tag=player] run function f:game/activity_sensor/info
execute if score @s milk_time matches 0 as @a[scores={activity_sensor=0,kill-timer=..0},tag=!get_powerup,tag=player] at @s run playsound minecraft:entity.warden.attack_impact master @s
execute if score @s milk_time matches 0 run title @a[scores={activity_sensor=0,kill-timer=..0},tag=!get_powerup,tag=player] actionbar ["",{"selector":"@s","bold":true},{"translate":"powerup.get","color":"gray"}]
execute if score @s milk_time matches 0 run title @a[scores={activity_sensor=3,kill-timer=..0},tag=!get_powerup,tag=player] actionbar ["",{"selector":"@s","bold":true},{"translate":"powerup.get","color":"gray"}]

#Powerup bossbar tag
tag @s remove powerup_bossbar1
tag @s remove powerup_bossbar2
tag @s remove powerup_bossbar3
tag @s remove powerup_bossbar4
execute unless entity @p[tag=powerup_bossbar1] run tag @s add powerup_bossbar1
execute unless entity @p[tag=powerup_bossbar2] run tag @s[tag=!powerup_bossbar1] add powerup_bossbar2
execute unless entity @p[tag=powerup_bossbar3] run tag @s[tag=!powerup_bossbar1,tag=!powerup_bossbar2] add powerup_bossbar3
execute unless entity @p[tag=powerup_bossbar4] run tag @s[tag=!powerup_bossbar1,tag=!powerup_bossbar2,tag=!powerup_bossbar3] add powerup_bossbar4

#Random powerup
tag @s remove invisible
tag @s remove machinegun
tag @s remove invulnerable
tag @s remove base
tag @s remove speed
tag @s remove goldmultiplier

execute if entity @e[tag=random1,tag=selected] run tag @s add machinegun
execute if entity @e[tag=random2,tag=selected] run tag @s add invulnerable
execute if entity @e[tag=random3,tag=selected] run tag @s add invisible
execute if entity @e[tag=random4,tag=selected] run tag @s add base
execute if entity @e[tag=random5,tag=selected] run tag @s add speed
execute if entity @e[tag=random6,tag=selected] run tag @s add goldmultiplier
kill @e[tag=random]

#Title
title @s[tag=machinegun] title ["",{"text":"> ","color":"gold","bold":true},{"translate":"powerup.machinegun","bold":true},{"text":" <","color":"gold","bold":true}]
title @s[tag=invulnerable] title ["",{"text":"> ","color":"gold","bold":true},{"translate":"powerup.invulnerable","bold":true},{"text":" <","color":"gold","bold":true}]
title @s[tag=invisible] title ["",{"text":"> ","color":"gold","bold":true},{"translate":"powerup.invisibility","bold":true},{"text":" <","color":"gold","bold":true}]
title @s[tag=base] title ["",{"text":"> ","color":"gold","bold":true},{"translate":"powerup.base","bold":true},{"text":" <","color":"gold","bold":true}]
title @s[tag=speed] title ["",{"text":"> ","color":"gold","bold":true},{"translate":"powerup.speed","bold":true},{"text":" <","color":"gold","bold":true}]
title @s[tag=goldmultiplier] title ["",{"text":"> ","color":"gold","bold":true},{"translate":"powerup.multiplier","bold":true},{"text":" <","color":"gold","bold":true}]

#4 hardcoded bossbars
execute if entity @s[tag=powerup_bossbar1] run function f:game/powerup/bossbar/1
execute if entity @s[tag=powerup_bossbar2] run function f:game/powerup/bossbar/2
execute if entity @s[tag=powerup_bossbar3] run function f:game/powerup/bossbar/3
execute if entity @s[tag=powerup_bossbar4] run function f:game/powerup/bossbar/4

#Time
scoreboard players set @s poweruptime 500
scoreboard players set @s[tag=base] poweruptime 1000