Unsafe Enchantments on Anvil Problem

Discussion in 'Plugin Development' started by Armandozetaxx, Jan 15, 2017.

Thread Status:
Not open for further replies.
  1. Offline

    Armandozetaxx

    Hello, on this code i'm trying to modified the anvil so it can allow players to enchant their items with unsafe enchantments, however I want this to work only with items that has the lore "Modified". I try this:

    Code:
        @EventHandler
        public void onPrepareAnvil(PrepareAnvilEvent e)
        {
            if(e.getInventory().getItem(0) != null && e.getInventory().getItem(1) != null)
            {
                ItemStack result = new ItemStack(e.getInventory().getItem(0));
                ItemMeta info = result.getItemMeta();
                if (info.getLore().contains("Modified")) {
                    EnchantmentStorageMeta bookmeta = (EnchantmentStorageMeta) e.getInventory().getItem(1).getItemMeta();
                    Map<Enchantment, Integer> enchantments = bookmeta.getStoredEnchants();
                    result.addUnsafeEnchantments(enchantments);
                    result.addUnsafeEnchantments(e.getInventory().getItem(1).getEnchantments());
                    e.setResult(result);
                }
            }
        }
    and it works but it throws me this error on console when items don't have the lore or the item with the lore is in the second space of the anvil. Thanks for your time, hope somebody can help me and explain what i'm doing wrong.
     
  2. Offline

    JanTuck

  3. Offline

    Armandozetaxx


    Thank you, does not throw errors on console anymore, however the code only works with books and i'm trying to enchant a chestplate (Without the lore) with an enchanted book (With the lore) and it does not apply the unsafe enchantment, only the max default for vanilla.

    Code:
        @EventHandler
        public void onPrepareAnvil(PrepareAnvilEvent e)
        {
            if(e.getInventory().getItem(0) != null && e.getInventory().getItem(1) != null)
            {
                ItemStack result = new ItemStack(e.getInventory().getItem(0));
                if(result.hasItemMeta() == true)
                {
                    ItemMeta info = result.getItemMeta();
                    if(info.hasLore() == true) {
                        if (info.getLore().contains("Modified")) {
                            EnchantmentStorageMeta bookmeta = (EnchantmentStorageMeta) e.getInventory().getItem(1).getItemMeta();
                            Map<Enchantment, Integer> enchantments = bookmeta.getStoredEnchants();
                            result.addUnsafeEnchantments(enchantments);
                            result.addUnsafeEnchantments(e.getInventory().getItem(1).getEnchantments());
                            e.setResult(result);
                        }
    
                    }
                }
            }
        }
    Basically on my server players can get unsafe enchanted books (That are identified with the lore "Modified") but they are not able to enchant normal items with these books. What i'm trying to achieve is that players can only use this books and tools that were previously modified with unsafe books.
     
    Last edited: Jan 15, 2017
Thread Status:
Not open for further replies.

Share This Page