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

#Villagers
execute as @e[type=minecraft:villager] at @s run function f:game/store/villager

#Click
scoreboard players add @a buy 0
scoreboard players set @a[scores={buy=..100}] buy -1
execute as @a unless data entity @s EnderItems[{Slot:22b}] run scoreboard players set @s buy 100
execute as @a unless data entity @s EnderItems[{Slot:10b}] run scoreboard players set @s buy 1
execute as @a unless data entity @s EnderItems[{Slot:12b}] run scoreboard players set @s buy 2
execute as @a unless data entity @s EnderItems[{Slot:14b}] run scoreboard players set @s buy 3
execute as @a unless data entity @s EnderItems[{Slot:16b}] run scoreboard players set @s buy 4

#Store
execute as @a[scores={buy=1..4}] at @s if entity @e[tag=shopkeeper,distance=..5] run function f:game/store/buy
execute as @a[scores={buy=100..}] at @s run function f:game/store/close
execute as @a at @s if entity @e[tag=shopkeeper,distance=..5] run function f:game/store/gui

#Bonegun
execute as @a[scores={click=1..}] if entity @s[nbt={SelectedItem:{id:"minecraft:carrot_on_a_stick",tag:{CustomModelData:10}}}] at @s run function f:game/store/bonegun

#Milk
scoreboard players set @a[scores={milk=1..}] milk_time 1
scoreboard players set @a[scores={milk=1..}] trap_cooldown 0
effect give @a[scores={milk=1..}] minecraft:unluck 30 0
execute as @a[scores={milk=1..}] at @s run playsound minecraft:block.note_block.bell master @s

scoreboard players set @a milk 0
scoreboard players add @a milk_time 0
scoreboard players add @a[scores={milk_time=1..}] milk_time 1
execute as @a[scores={milk_time=600}] at @s run playsound minecraft:block.note_block.bell master @s ~ ~ ~ 1 0.5
effect clear @a[scores={milk_time=600..}] minecraft:unluck
scoreboard players set @a[scores={milk_time=600..}] milk_time 0
clear @a minecraft:bucket

#Minigun
execute as @e[type=minecraft:pufferfish] at @s unless block ~ ~-0.1 ~ air unless block ~ ~-0.1 ~ barrier if block ~ ~ ~ air if block ~ ~1.5 ~ air unless block ~ ~ ~ dead_tube_coral_fan align xyz positioned ~0.5 ~ ~0.5 unless entity @e[tag=minigun,distance=..0.1] if entity @s[distance=..0.1] run function f:game/store/minigun/spawn
execute as @e[type=minecraft:pufferfish] at @s unless block ~ ~-0.1 ~ air unless block ~ ~-0.1 ~ barrier if block ~ ~1.5 ~ air unless block ~ ~ ~ dead_tube_coral_fan align xyz positioned ~0.5 ~0.5 ~0.5 unless entity @e[tag=minigun,distance=..0.1] if entity @s[distance=..0.1] run function f:game/store/minigun/spawn
execute if entity @e[tag=minigun] run function f:game/store/minigun/minigun
execute as @e[type=minecraft:pufferfish] at @s run function f:game/store/minigun/error

execute unless entity @e[tag=minigun] run kill @e[tag=candy_bullet]