Solved Using overleveled enchantments in anvils

Discussion in 'Spigot Plugin Development' started by stelioshah, Jun 11, 2021.

  1. When anviling an overleveled enchanted book, you will not get the enchantment of the book but the maximum enchantment level for the specific enchantment. E.g. anviling an unbreaking IV book will give you an unbreaking III enchantment.

    I could not find anything in server files to enable this. I thought of adding an anvil event to basically set the appropriate level when anviling, but is this really necessary? Isn't there a direct way to do this?

    The same thing goes with items. You cannot anvil an unbreaking III pickaxe with an unbreaking IV and get the unbreaking IV enchantment.
  2. I am trying to avoid using PrepareAnvilEvent because it will mess with combining books (if you combine 2 unbreaking II books for example you get an unbreaking III book), I do not want players to be able to combine books to get overleveled books. I implement the overleveled enchantments into the game in another way.. If no other solution is suggested I will be forced to use the event.
  3. Sorry, I misread your post.

    Code (Text):
    ItemStack item = /* Let's say an enchanted book with unbreaking 4 */

            for(Map.Entry<Enchantment, Integer> enc: Objects.requireNonNull(item.getItemMeta()).getEnchants().entrySet()){
                int maxLevel = enc.getKey().getKey().getMaxLevel();
                /* Do something like change your item's enchant level to the max level or so */
                //For example
               boolean overleveled = meta.getEnchantLevel(enc.getKey()) > maxLevel
    #4 Dolphin2410, Jun 11, 2021
    Last edited: Jun 12, 2021
    • Like Like x 1
  4. If you wanted to use prepareAnvilEvent, could you not just check if the two items are enchanted books, then don't change anything about the result if they are?
  5. The problem with that would be that if a player tried to combine 2 unbreaking IV books to try to get an unbreaking V the resulting book would be an unbreaking III instead of keeping the unbreaking IV enchantment. I need to check if one book has an overleveled enchantment, save the specific enchantment and then add the levels needed.

    I had not realized earlier that a getMaxLevel() method existed which made me think that I would have to do all of the above by hand, that's why I was trying to avoid it. It still needs some work though to get all the cases right, I can now simply check if it is more than max level and then always set the enchantment to that no matter the combination.

    Yes I know.. Pretty stupid of me. Thanks for troubling though.