Java Code Examples for net.minecraft.enchantment.Enchantment#isCompatibleWith()

The following examples show how to use net.minecraft.enchantment.Enchantment#isCompatibleWith() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NEIServerUtils.java    From NotEnoughItems with MIT License 5 votes vote down vote up
public static boolean doesEnchantmentConflict(List<int[]> enchantments, Enchantment enchantment) {
    for (int[] ai : enchantments) {
        if (!enchantment.isCompatibleWith(Enchantment.getEnchantmentByID(ai[0]))) {
            return true;
        }
    }

    return false;
}
 
Example 2
Source File: AnvilUpdateEventHandler.java    From enderutilities with GNU Lesser General Public License v3.0 4 votes vote down vote up
private void enchantItem(AnvilUpdateEvent event, IAnvilRepairable repairable, ItemEnchantedBook book)
{
    ItemStack toolStack = event.getLeft().copy();
    ItemStack bookStack = event.getRight().copy();
    NBTTagList bookEnchantmentList = ItemEnchantedBook.getEnchantments(bookStack);

    if (bookEnchantmentList.tagCount() <= 0 || toolStack.getItem().isBookEnchantable(toolStack, bookStack) == false)
    {
        return;
    }

    Map<Enchantment, Integer> oldEnchantments = EnchantmentHelper.getEnchantments(toolStack);
    Map<Enchantment, Integer> bookEnchantments = EnchantmentHelper.getEnchantments(bookStack);
    Iterator<Map.Entry<Enchantment, Integer>> iterBookEnchantments = bookEnchantments.entrySet().iterator();
    int cost = oldEnchantments.size() * 2;
    boolean levelIncreased = false;

    while (iterBookEnchantments.hasNext())
    {
        Map.Entry<Enchantment, Integer> enchantmentEntry = iterBookEnchantments.next();
        Enchantment enchBook = enchantmentEntry.getKey();

        if (enchBook != null)
        {
            int oldEnchLvl = oldEnchantments.containsKey(enchBook) ? oldEnchantments.get(enchBook).intValue() : 0;
            int bookEnchLvl = enchantmentEntry.getValue();
            int newLvl = bookEnchLvl == oldEnchLvl ? oldEnchLvl + 1 : Math.max(oldEnchLvl, bookEnchLvl);
            newLvl = Math.min(newLvl, enchBook.getMaxLevel());

            if (repairable.canApplyEnchantment(toolStack, enchBook) == false)
            {
                event.setCanceled(true);
                return;
            }

            if (newLvl > oldEnchLvl)
            {
                levelIncreased = true;
            }

            Iterator<Map.Entry<Enchantment, Integer>> iterOldEnchantments = oldEnchantments.entrySet().iterator();

            // Check that the new enchantment doesn't conflict with any of the existing enchantments
            while (iterOldEnchantments.hasNext())
            {
                Enchantment enchOld = iterOldEnchantments.next().getKey();

                if (enchOld.equals(enchBook) == false && enchOld.isCompatibleWith(enchBook) == false)
                {
                    event.setCanceled(true);
                    return;
                }
            }

            oldEnchantments.put(enchBook, newLvl);
            cost += newLvl * 2;
        }
    }

    // Check that at least some new enchantments would be added, or some of the existing levels increased
    if (levelIncreased == false)
    {
        event.setCanceled(true);
        return;
    }

    EnchantmentHelper.setEnchantments(oldEnchantments, toolStack);
    event.setOutput(toolStack);
    event.setCost(cost);
    this.updateItemName(event, toolStack);
    event.setCost(Math.min(event.getCost(), 39));
}