net.minecraft.item.ItemEnchantedBook Java Examples

The following examples show how to use net.minecraft.item.ItemEnchantedBook. 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: AnvilUpdateEventHandler.java    From enderutilities with GNU Lesser General Public License v3.0 6 votes vote down vote up
@SubscribeEvent
public void onAnvilUpdate(AnvilUpdateEvent event)
{
    ItemStack left = event.getLeft();
    ItemStack right = event.getRight();

    if (left.getItem() instanceof IAnvilRepairable)
    {
        IAnvilRepairable repairable = (IAnvilRepairable) left.getItem();

        // Advanced Ender Alloy
        if (repairable.isRepairItem(left, right))
        {
            this.fullyRepairItem(event, repairable, 1, 15);
        }
        else if (right.getItem() == Items.ENCHANTED_BOOK)
        {
            this.enchantItem(event, repairable, (ItemEnchantedBook) right.getItem());
        }
        else
        {
            // Cancel vanilla behaviour, otherwise it would allow repairing tools with another tool (and lose the modules)
            event.setCanceled(true);
        }
    }
}
 
Example #2
Source File: GTHelperStack.java    From GT-Classic with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static int getBookEnchantmentLevel(ItemStack stack) {
	if (!stack.isEmpty() && stack.getItem() instanceof ItemEnchantedBook) {
		NBTTagList nbttaglist = ItemEnchantedBook.getEnchantments(stack);
		for (int i = 0; i < nbttaglist.tagCount(); ++i) {
			NBTTagCompound nbttagcompound = nbttaglist.getCompoundTagAt(i);
			int j = nbttagcompound.getShort("id");
			Enchantment enchantment = Enchantment.getEnchantmentByID(j);
			if (enchantment != null) {
				return nbttagcompound.getShort("lvl");
			}
		}
	}
	return 0;
}
 
Example #3
Source File: ShopkeeperTradesForEarth.java    From ToroQuest with GNU General Public License v3.0 4 votes vote down vote up
private static ItemStack silkTouch() {
	ItemStack stack = new ItemStack(Items.ENCHANTED_BOOK);
	ItemEnchantedBook.addEnchantment(stack, new EnchantmentData(Enchantment.REGISTRY.getObjectById(33), 1));
	return stack;
}
 
Example #4
Source File: ShopkeeperTradesForFire.java    From ToroQuest with GNU General Public License v3.0 4 votes vote down vote up
private static ItemStack level1FireProtection() {
	ItemStack stack = new ItemStack(Items.ENCHANTED_BOOK);
	stack.setStackDisplayName("Fire Protection I");
	ItemEnchantedBook.addEnchantment(stack, new EnchantmentData(Enchantment.REGISTRY.getObjectById(1), 1));
	return stack;
}
 
Example #5
Source File: ShopkeeperTradesForFire.java    From ToroQuest with GNU General Public License v3.0 4 votes vote down vote up
private static ItemStack level2FireProtection() {
	ItemStack stack = new ItemStack(Items.ENCHANTED_BOOK);
	stack.setStackDisplayName("Fire Protection II");
	ItemEnchantedBook.addEnchantment(stack, new EnchantmentData(Enchantment.REGISTRY.getObjectById(1), 2));
	return stack;
}
 
Example #6
Source File: ShopkeeperTradesForFire.java    From ToroQuest with GNU General Public License v3.0 4 votes vote down vote up
private static ItemStack level3FireProtection() {
	ItemStack stack = new ItemStack(Items.ENCHANTED_BOOK);
	stack.setStackDisplayName("Fire Protection III");
	ItemEnchantedBook.addEnchantment(stack, new EnchantmentData(Enchantment.REGISTRY.getObjectById(1), 3));
	return stack;
}
 
Example #7
Source File: ShopkeeperTradesForFire.java    From ToroQuest with GNU General Public License v3.0 4 votes vote down vote up
private static ItemStack level4FireProtection() {
	ItemStack stack = new ItemStack(Items.ENCHANTED_BOOK);
	stack.setStackDisplayName("Fire Protection IV");
	ItemEnchantedBook.addEnchantment(stack, new EnchantmentData(Enchantment.REGISTRY.getObjectById(1), 4));
	return stack;
}
 
Example #8
Source File: ShopkeeperTradesForFire.java    From ToroQuest with GNU General Public License v3.0 4 votes vote down vote up
private static ItemStack level5FireProtection() {
	ItemStack stack = new ItemStack(Items.ENCHANTED_BOOK);
	stack.setStackDisplayName("Fire Protection V");
	ItemEnchantedBook.addEnchantment(stack, new EnchantmentData(Enchantment.REGISTRY.getObjectById(1), 5));
	return stack;
}
 
Example #9
Source File: ShopkeeperTradesForSun.java    From ToroQuest with GNU General Public License v3.0 4 votes vote down vote up
private static ItemStack level1BlastProtection() {
	ItemStack stack = new ItemStack(Items.ENCHANTED_BOOK);
	ItemEnchantedBook.addEnchantment(stack, new EnchantmentData(Enchantment.REGISTRY.getObjectById(3), 1));
	return stack;
}
 
Example #10
Source File: ShopkeeperTradesForSun.java    From ToroQuest with GNU General Public License v3.0 4 votes vote down vote up
private static ItemStack level2BlastProtection() {
	ItemStack stack = new ItemStack(Items.ENCHANTED_BOOK);
	ItemEnchantedBook.addEnchantment(stack, new EnchantmentData(Enchantment.REGISTRY.getObjectById(3), 2));
	return stack;
}
 
Example #11
Source File: ShopkeeperTradesForSun.java    From ToroQuest with GNU General Public License v3.0 4 votes vote down vote up
private static ItemStack level3BlastProtection() {
	ItemStack stack = new ItemStack(Items.ENCHANTED_BOOK);
	ItemEnchantedBook.addEnchantment(stack, new EnchantmentData(Enchantment.REGISTRY.getObjectById(3), 3));
	return stack;
}
 
Example #12
Source File: ShopkeeperTradesForSun.java    From ToroQuest with GNU General Public License v3.0 4 votes vote down vote up
private static ItemStack level4BlastProtection() {
	ItemStack stack = new ItemStack(Items.ENCHANTED_BOOK);
	ItemEnchantedBook.addEnchantment(stack, new EnchantmentData(Enchantment.REGISTRY.getObjectById(3), 4));
	return stack;
}
 
Example #13
Source File: EnchantedBookMetaProvider.java    From OpenPeripheral-Integration with MIT License 4 votes vote down vote up
@Override
public Object getMeta(ItemEnchantedBook target, ItemStack stack) {
	NBTTagList ench = target.func_92110_g(stack);
	return ench != null? ModuleVanilla.listEnchantments(ench) : null;
}
 
Example #14
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));
}
 
Example #15
Source File: EnchantmentUtils.java    From OpenModsLib with MIT License 4 votes vote down vote up
public static void addAllBooks(Enchantment enchantment, List<ItemStack> items) {
	for (int i = enchantment.getMinLevel(); i <= enchantment.getMaxLevel(); i++)
		items.add(ItemEnchantedBook.getEnchantedItemStack(new EnchantmentData(enchantment, i)));
}