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

#Click detect
execute at @e[tag=alarm_blue,tag=!active_trap] run tag @p[team=blue,distance=..5,scores={chest=1..}] add trytobuy
execute at @e[tag=alarm_red,tag=!active_trap] run tag @p[team=red,distance=..5,scores={chest=1..}] add trytobuy
execute at @e[tag=alarm_green,tag=!active_trap] run tag @p[team=green,distance=..5,scores={chest=1..}] add trytobuy
execute at @e[tag=alarm_yellow,tag=!active_trap] run tag @p[team=yellow,distance=..5,scores={chest=1..}] add trytobuy
scoreboard players set @a chest 0

#Buy
execute as @p[tag=trytobuy,scores={gold=..4}] at @s run playsound minecraft:entity.villager.no master @s

execute as @p[tag=trytobuy,scores={gold=5..}] at @s run playsound minecraft:entity.experience_orb.pickup master @s
execute at @p[tag=trytobuy,scores={gold=5..}] run tag @e[tag=alarm,limit=1,sort=nearest] add active_trap
execute at @p[tag=trytobuy,scores={gold=5..}] run data merge entity @e[tag=alarm,limit=1,sort=nearest,tag=active_trap] {VillagerData:{level:1,profession:"minecraft:armorer",type:"minecraft:jungle"},CustomName:"{\"translate\":\"trap.active\",\"bold\":\"true\",\"color\":\"green\"}"}
clear @p[tag=trytobuy,scores={gold=5..}] gold_ingot 5
scoreboard players remove @p[tag=trytobuy,scores={gold=5..}] gold 5
title @p[tag=trytobuy] actionbar {"text":" "}

tag @a[tag=trytobuy] remove trytobuy

#Trigger (more in f:game/score/<color>_base)
scoreboard players add @e[tag=alarm,tag=triggered] time 1
execute as @e[tag=alarm,scores={time=1}] run data merge entity @s {CustomName:"{\"translate\":\"game.alarm\",\"bold\":\"true\",\"color\":\"red\"}"}

execute if entity @e[tag=alarm_blue,scores={time=1}] as @a[team=blue] at @s run function f:game/alarm/alarm_title
execute if entity @e[tag=alarm_red,scores={time=1}] as @a[team=red] at @s run function f:game/alarm/alarm_title
execute if entity @e[tag=alarm_green,scores={time=1}] as @a[team=green] at @s run function f:game/alarm/alarm_title
execute if entity @e[tag=alarm_yellow,scores={time=1}] as @a[team=yellow] at @s run function f:game/alarm/alarm_title
execute at @e[tag=alarm_blue,scores={time=1}] as @a[tag=blue_base,team=!blue,scores={milk_time=0},gamemode=adventure] at @s run function f:game/alarm/alarm_trigger
execute at @e[tag=alarm_red,scores={time=1}] as @a[tag=red_base,team=!red,scores={milk_time=0},gamemode=adventure] at @s run function f:game/alarm/alarm_trigger
execute at @e[tag=alarm_green,scores={time=1}] as @a[tag=green_base,team=!green,scores={milk_time=0},gamemode=adventure] at @s run function f:game/alarm/alarm_trigger
execute at @e[tag=alarm_yellow,scores={time=1}] as @a[tag=yellow_base,team=!yellow,scores={milk_time=0},gamemode=adventure] at @s run function f:game/alarm/alarm_trigger

execute as @e[tag=alarm,scores={time=40}] run data merge entity @s {VillagerData:{level:1,profession:"minecraft:cleric",type:"minecraft:dessert"},CustomName:'[{"text":"5","bold":true,"color":"gold"},{"translate":"game.gold_icon","color":"white","bold":false}]'}
tag @e[tag=alarm,scores={time=40..}] remove active_trap
tag @e[tag=alarm,scores={time=40..}] remove triggered
scoreboard players set @e[tag=alarm,scores={time=40..}] time 0