Combine sharpness 10 book with a sword in a anvil

If you use commands to get a book with an enchantment past its max level, and then you try to combine it with a sword or other tool in an anvil, the anvil will only give you the maximum level of that enchantment, even if the level on the book is higher. Is there any way to bypass this?

The only way I know of is making a custom system where you would drop the enchanted book on the ground. But if I have many levels of enchantment books in my map it will require way too many command blocks unless there already is a datapack for it. There is probably a mod that will make this work, but I don't want to take the path of using mods unless this problem is unsolvable in vanilla.

See example video below:


Here's a setup that works for one enchantment on the enchanted book only.

execute as @e[type=item,nbt={Item:{id:"minecraft:diamond_sword"}}] if entity @e[type=item,nbt={Item:{id:"minecraft:enchanted_book"}}] if entity @s[nbt={Item:{tag:{Enchantments:[]}}}] run data modify entity @s Item.tag.Enchantments set value []
execute as @e[type=item,nbt={Item:{id:"minecraft:diamond_sword"}}] at @s run data modify entity @s Item.tag.Enchantments append from entity @e[type=item,limit=1,distance=0..0.5,nbt={Item:{id:"minecraft:enchanted_book"}}] Item.tag.StoredEnchantments[0]
execute as @e[type=item,nbt={Item:{id:"minecraft:enchanted_book"}}] at @s if entity @e[type=item,nbt={Item:{id:"minecraft:diamond_sword"}},distance=0..0.5] run kill @s

Explanation:

  1. If there is a diamond sword next to an enchanted book, make sure that the diamond sword has the Enchantments tag. If it doesn't, add it.
  2. Tell the diamond sword to modify its NBT data to append the 1st enchantment from the book onto its own enchantment list.
  3. Get rid of the enchanted book if it is near a diamond sword.