Unsafe Enchantments on Anvil

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

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

    Armandozetaxx

    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 these modified books on new tools or one that was previously modified by this books. Here is the code, but it only works with books, can somebody guide me or tell me what i need to do? thanks in advanced. I tried letting users use this books without identifying them but a big problem was that they were not able to add normal enchantments and it was a really big mess so i decided to do the lore.

    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);
                        }
    
                    }
                }
            }
        }
     
  2. Offline

    Zombie_Striker

    @Armandozetaxx
    Why are you checking if a boolean equals a boolean? You only need one boolean. Remove the ==true.

    Why does it not work with other items? Does it throw an exception? If so, post the error.
     
  3. Offline

    Armandozetaxx

    @Zombie_Striker

    It doesn't throw me any errors, what i notice is that if the enchanted book is in the second slot it does not work, only works on the first one and if it is combined with a book. I hope you understand me with these little examples:

    Anvil:
    [modified] + [book] -> [works!] (Works great, would combine with other non modified books and it would keep the lore. This is exactly what i want to achieve with the items.)

    [modified] + [item] -> [doesn't work] (Does not generate output)

    [item] + [modified] -> [doesn't work] (would apply the max default vanilla level of the enchantment)
     
Thread Status:
Not open for further replies.

Share This Page