execute store result score @s ShrinePlayerCurrentXP run xp query @s levels

execute unless items entity @s weapon.mainhand *[minecraft:enchantments={levels:{"minecraft:mending":1}}] run scoreboard players set @s ShrineXPRequired 20

execute unless items entity @s weapon.mainhand *[minecraft:enchantments~[{enchantments:["mending"]}]] if score @s ShrinePlayerCurrentXP < @s ShrineXPRequired run tag @s add shrine_fail_no_xp_4
execute if items entity @s weapon.mainhand *[minecraft:enchantments~[{enchantments:["mending"]}]] run tag @s add shrine_max_level

execute unless items entity @s weapon.mainhand *[minecraft:enchantments={levels:{"minecraft:mending":1}}] if score @s ShrinePlayerCurrentXP >= @s ShrineXPRequired run tag @s add shrine_accept

execute unless items entity @s weapon.mainhand *[minecraft:enchantments={levels:{"minecraft:mending":1}}] if score @s ShrinePlayerCurrentXP >= @s ShrineXPRequired run xp add @s -20 levels

execute unless entity @s[tag=shrine_fail_no_xp_4] unless entity @s[tag=shrine_max_level] if entity @s[tag=shrine_accept] run item modify entity @s weapon.mainhand kiwi:mending_upgrade