# Different Shrines
tag @e[tag=enchantment_shrine,limit=1,sort=nearest] add shrine_player_interacted_with

execute if entity @e[tag=enchantment_shrine,tag=shrine_player_interacted_with,limit=1,sort=nearest,tag=protection_shrine] unless items entity @s weapon.mainhand #minecraft:enchantable/armor unless items entity @s weapon.mainhand *[minecraft:enchantments={levels:{"minecraft:fire_protection":1}}] unless items entity @s weapon.mainhand *[minecraft:enchantments={levels:{"minecraft:blast_protection":1}}] unless items entity @s weapon.mainhand *[minecraft:enchantments={levels:{"minecraft:projectile_protection":1}}] run tag @s add shrine_fail_incompatible
execute if entity @e[tag=enchantment_shrine,tag=shrine_player_interacted_with,limit=1,sort=nearest,tag=protection_shrine] unless entity @s[tag=shrine_fail_incompatible] run function kiwi:enchantment_shrines/protection/shrine_of_protection_enchant

execute if entity @e[tag=enchantment_shrine,tag=shrine_player_interacted_with,limit=1,sort=nearest,tag=sharpness_shrine] unless items entity @s weapon.mainhand #minecraft:enchantable/sharp_weapons unless items entity @s weapon.mainhand *[minecraft:enchantments={levels:{"minecraft:smite":1}}] unless items entity @s weapon.mainhand *[minecraft:enchantments={levels:{"minecraft:bane_of_arthropods":1}}] run tag @s add shrine_fail_incompatible
execute if entity @e[tag=enchantment_shrine,tag=shrine_player_interacted_with,limit=1,sort=nearest,tag=sharpness_shrine] unless entity @s[tag=shrine_fail_incompatible] run function kiwi:enchantment_shrines/sharpness/shrine_of_sharpness_enchant

execute if entity @e[tag=enchantment_shrine,tag=shrine_player_interacted_with,limit=1,sort=nearest,tag=unbreaking_shrine] unless items entity @s weapon.mainhand #minecraft:enchantable/durability run tag @s add shrine_fail_incompatible
execute if entity @e[tag=enchantment_shrine,tag=shrine_player_interacted_with,limit=1,sort=nearest,tag=unbreaking_shrine] unless entity @s[tag=shrine_fail_incompatible] run function kiwi:enchantment_shrines/unbreaking/shrine_of_unbreaking_enchant

execute if entity @e[tag=enchantment_shrine,tag=shrine_player_interacted_with,limit=1,sort=nearest,tag=mending_shrine] unless items entity @s weapon.mainhand #minecraft:enchantable/durability unless items entity @s weapon.mainhand *[minecraft:enchantments={levels:{"minecraft:infinity":1}}] run tag @s add shrine_fail_incompatible
execute if entity @e[tag=enchantment_shrine,tag=shrine_player_interacted_with,limit=1,sort=nearest,tag=mending_shrine] unless entity @s[tag=shrine_fail_incompatible] run function kiwi:enchantment_shrines/mending/shrine_of_mending_enchant

execute if entity @e[tag=enchantment_shrine,tag=shrine_player_interacted_with,limit=1,sort=nearest,tag=feather_falling_shrine] unless items entity @s weapon.mainhand #minecraft:enchantable/foot_armor run tag @s add shrine_fail_incompatible
execute if entity @e[tag=enchantment_shrine,tag=shrine_player_interacted_with,limit=1,sort=nearest,tag=feather_falling_shrine] unless entity @s[tag=shrine_fail_incompatible] run function kiwi:enchantment_shrines/feather_falling/shrine_of_feather_falling_enchant

execute if entity @e[tag=enchantment_shrine,tag=shrine_player_interacted_with,limit=1,sort=nearest,tag=thorns_shrine] unless items entity @s weapon.mainhand #minecraft:enchantable/armor run tag @s add shrine_fail_incompatible
execute if entity @e[tag=enchantment_shrine,tag=shrine_player_interacted_with,limit=1,sort=nearest,tag=thorns_shrine] unless entity @s[tag=shrine_fail_incompatible] run function kiwi:enchantment_shrines/thorns/shrine_of_thorns_enchant

execute if entity @e[tag=enchantment_shrine,tag=shrine_player_interacted_with,limit=1,sort=nearest,tag=power_shrine] unless items entity @s weapon.mainhand #minecraft:enchantable/bow run tag @s add shrine_fail_incompatible
execute if entity @e[tag=enchantment_shrine,tag=shrine_player_interacted_with,limit=1,sort=nearest,tag=power_shrine] unless entity @s[tag=shrine_fail_incompatible] run function kiwi:enchantment_shrines/power/shrine_of_power_enchant

execute if entity @e[tag=enchantment_shrine,tag=shrine_player_interacted_with,limit=1,sort=nearest,tag=growth_shrine] unless items entity @s weapon.mainhand #minecraft:enchantable/armor run tag @s add shrine_fail_incompatible
execute if entity @e[tag=enchantment_shrine,tag=shrine_player_interacted_with,limit=1,sort=nearest,tag=growth_shrine] unless entity @s[tag=shrine_fail_incompatible] run function kiwi:enchantment_shrines/growth/shrine_of_growth_enchant

# Accept / Fail Handling
execute if entity @s[tag=shrine_accept] run playsound block.enchantment_table.use neutral @a[distance=..4] ~ ~ ~ 100 0
execute if entity @s[tag=shrine_accept] at @e[tag=enchantment_shrine,limit=1,sort=nearest] run particle minecraft:end_rod ~ ~1 ~ 0 0 0 0.5 80
execute if entity @s[tag=shrine_accept] at @e[tag=enchantment_shrine,limit=1,sort=nearest] run particle minecraft:ominous_spawning ~ ~1 ~ 0 0 0 0.5 200
execute if entity @s[tag=shrine_accept] run tellraw @s {"text": "The shrine accepts your offering...","color": "green"}

execute if entity @s[tag=shrine_fail_incompatible] run playsound entity.allay.item_thrown neutral @a[distance=..4] ~ ~ ~ 100 0
execute if entity @s[tag=shrine_fail_incompatible] at @e[tag=enchantment_shrine,limit=1,sort=nearest] run particle minecraft:dust{color:[1.0,0.0,0.0],scale:1.5} ~ ~1 ~ 0.7 0.7 0.7 0.5 200
execute if entity @s[tag=shrine_fail_incompatible] run tellraw @s [{"text": " This item cannot receive this enchantment...","color": "red"}]

execute if entity @s[tag=shrine_fail_no_xp_0] run playsound entity.allay.item_thrown neutral @a[distance=..4] ~ ~ ~ 100 0
execute if entity @s[tag=shrine_fail_no_xp_0] at @e[tag=enchantment_shrine,limit=1,sort=nearest] run particle minecraft:dust{color:[1.0,1.0,1.0],scale:1.5} ~ ~1 ~ 0.7 0.7 0.7 0.5 200
execute if entity @s[tag=shrine_fail_no_xp_0] run tellraw @s [{"text": " This upgrade requires ","color": "red"},{"text": "[4]","color": "yellow"},{"text":" experience levels...","color": "red"}]

execute if entity @s[tag=shrine_fail_no_xp_1] run playsound entity.allay.item_thrown neutral @a[distance=..4] ~ ~ ~ 100 0
execute if entity @s[tag=shrine_fail_no_xp_1] at @e[tag=enchantment_shrine,limit=1,sort=nearest] run particle minecraft:dust{color:[1.0,1.0,1.0],scale:1.5} ~ ~1 ~ 0.7 0.7 0.7 0.5 200
execute if entity @s[tag=shrine_fail_no_xp_1] run tellraw @s [{"text": " This upgrade requires ","color": "red"},{"text": "[8]","color": "yellow"},{"text":" experience levels...","color": "red"}]

execute if entity @s[tag=shrine_fail_no_xp_2] run playsound entity.allay.item_thrown neutral @a[distance=..4] ~ ~ ~ 100 0
execute if entity @s[tag=shrine_fail_no_xp_2] at @e[tag=enchantment_shrine,limit=1,sort=nearest] run particle minecraft:dust{color:[1.0,1.0,1.0],scale:1.5} ~ ~1 ~ 0.7 0.7 0.7 0.5 200
execute if entity @s[tag=shrine_fail_no_xp_2] run tellraw @s [{"text": " This upgrade requires ","color": "red"},{"text": "[12]","color": "yellow"},{"text":" experience levels...","color": "red"}]

execute if entity @s[tag=shrine_fail_no_xp_3] run playsound entity.allay.item_thrown neutral @a[distance=..4] ~ ~ ~ 100 0
execute if entity @s[tag=shrine_fail_no_xp_3] at @e[tag=enchantment_shrine,limit=1,sort=nearest] run particle minecraft:dust{color:[1.0,1.0,1.0],scale:1.5} ~ ~1 ~ 0.7 0.7 0.7 0.5 200
execute if entity @s[tag=shrine_fail_no_xp_3] run tellraw @s [{"text": " This upgrade requires ","color": "red"},{"text": "[16]","color": "yellow"},{"text":" experience levels...","color": "red"}]

execute if entity @s[tag=shrine_fail_no_xp_4] run playsound entity.allay.item_thrown neutral @a[distance=..4] ~ ~ ~ 100 0
execute if entity @s[tag=shrine_fail_no_xp_4] at @e[tag=enchantment_shrine,limit=1,sort=nearest] run particle minecraft:dust{color:[1.0,1.0,1.0],scale:1.5} ~ ~1 ~ 0.7 0.7 0.7 0.5 200
execute if entity @s[tag=shrine_fail_no_xp_4] run tellraw @s [{"text": " This upgrade requires ","color": "red"},{"text": "[20]","color": "yellow"},{"text":" experience levels...","color": "red"}]

execute if entity @s[tag=shrine_max_level] run playsound entity.allay.item_thrown neutral @a[distance=..4] ~ ~ ~ 100 0
execute if entity @s[tag=shrine_max_level] at @e[tag=enchantment_shrine,limit=1,sort=nearest] run particle minecraft:dust{color:[0.0,1.0,0.0],scale:1.5} ~ ~1 ~ 0.7 0.7 0.7 0.5 200
execute if entity @s[tag=shrine_max_level] run tellraw @s [{"text": " This item has the max level of the current enchantment.","color": "red"}]

# Deallocate
tag @s remove shrine_fail_incompatible
tag @s remove shrine_fail_no_xp_0
tag @s remove shrine_fail_no_xp_1
tag @s remove shrine_fail_no_xp_2
tag @s remove shrine_fail_no_xp_3
tag @s remove shrine_fail_no_xp_4
tag @s remove shrine_max_level
tag @s remove shrine_accept
tag @e remove shrine_player_interacted_with
advancement revoke @s only kiwi:enchantment_shrine_interact

# 4 Levels -> 40 XP
# 8 Levels -> 112 XP
# 12 Levels -> 217 XP
# 16 Levels -> 352 XP
# Cumulative XP needed for one lvl 4 upgrade -> 40+112+217+352 = 721 XP
# Almost half what you would need to get from normal enchants.
# 30 Levels -> 1395 XP