Java Code Examples for net.minecraft.init.Items.ENCHANTED_BOOK

The following are Jave code examples for showing how to use ENCHANTED_BOOK of the net.minecraft.init.Items class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: pnc-repressurized   File: PressureChamberPressureEnchantHandler.java   View Source Code Vote up 6 votes
private ItemStack[] getRecipeIngredients(ItemStackHandler inputStacks) {
    List<ItemStack> enchantedBooks = new ItemStackHandlerIterable(inputStacks)
                                                .stream()
                                                .filter(book -> book.getItem() == Items.ENCHANTED_BOOK)
                                                .collect(Collectors.toList());

    if (enchantedBooks.isEmpty()) return null;

    for (ItemStack inputStack : new ItemStackHandlerIterable(inputStacks)) {
        if ((inputStack.isItemEnchantable() || inputStack.isItemEnchanted()) && inputStack.getItem() != Items.ENCHANTED_BOOK) {
            for (ItemStack enchantedBook : enchantedBooks) {
                Map<Enchantment, Integer> bookMap = EnchantmentHelper.getEnchantments(enchantedBook);
                for (Map.Entry<Enchantment, Integer> entry : bookMap.entrySet()) {
                    if (entry.getKey().canApply(inputStack)) {
                        return new ItemStack[]{ inputStack, enchantedBook};
                    }
                }
            }
        }
    }
    return null;
}
 
Example 2
Project: pnc-repressurized   File: PressureChamberVacuumEnchantHandler.java   View Source Code Vote up 6 votes
@Override
public NonNullList<ItemStack> craftRecipe(ItemStackHandler inputStacks) {
    ItemStack enchantedStack = getDisenchantableItem(inputStacks);
    getBook(inputStacks).shrink(1);
    
    // take a random enchantment off the enchanted item...
    Map<Enchantment, Integer> enchantments = EnchantmentHelper.getEnchantments(enchantedStack);
    List<Enchantment> l = new ArrayList<>(enchantments.keySet());
    Enchantment strippedEnchantment = l.get(new Random().nextInt(l.size()));
    int level = enchantments.get(strippedEnchantment);
    enchantments.remove(strippedEnchantment);
    EnchantmentHelper.setEnchantments(enchantments, enchantedStack);

    // ...and create an enchanted book with it
    ItemStack enchantedBook = new ItemStack(Items.ENCHANTED_BOOK);
    EnchantmentHelper.setEnchantments(ImmutableMap.of(strippedEnchantment, level), enchantedBook);

    return NonNullList.from(ItemStack.EMPTY, enchantedBook);
}
 
Example 3
Project: Industrial-Foregoing   File: EnchantmentExtractorTile.java   View Source Code Vote up 6 votes
@Override
protected float performWork() {
    if (WorkUtils.isDisabled(this.getBlockType())) return 0;

    if (!hasBooks() || getItem().isEmpty()) return 0;
    ItemStack enchantedItem = getItem();
    ItemStack enchantedBook = new ItemStack(Items.ENCHANTED_BOOK);
    if (ItemHandlerHelper.insertItem(outEnchanted, enchantedBook, true).isEmpty() && ItemHandlerHelper.insertItem(outItem, enchantedItem, true).isEmpty()) {
        NBTTagCompound base = (NBTTagCompound) enchantedItem.getEnchantmentTagList().get(0);
        ((ItemEnchantedBook) Items.ENCHANTED_BOOK).addEnchantment(enchantedBook, new EnchantmentData(Enchantment.getEnchantmentByID(base.getShort("id")), base.getShort("lvl")));
        enchantedItem.getEnchantmentTagList().removeTag(0);
        ItemHandlerHelper.insertItem(outEnchanted, enchantedBook, false);
        ItemHandlerHelper.insertItem(outItem, enchantedItem.copy(), false);
        inBook.getStackInSlot(0).setCount(inBook.getStackInSlot(0).getCount() - 1);
        enchantedItem.setCount(enchantedItem.getCount() - 1);
        return 500;
    }

    return 0;
}
 
Example 4
Project: mc-Slingshot   File: RecipeLooting.java   View Source Code Vote up 6 votes
@Override
public boolean matches(InventoryCrafting inv, World worldIn) {
   boolean hasSlingshot = false, hasBook = false;
   for (int i = inv.getSizeInventory(); --i >= 0; ) {
      ItemStack stack = inv.getStackInSlot(i);
      if (null != stack) {
         Item item = stack.getItem();
         if (item == ModItems.slingshot) {
            if (hasSlingshot)
               return false;
            hasSlingshot = true;
         } else if (item == Items.ENCHANTED_BOOK) {
            if (hasBook)
               return false;
            if (getBookLootingLevel(stack) <= 0)
               return false;
            hasBook = true;
         } else
            return false;
      }
   }

   return hasSlingshot && hasBook;
}
 
Example 5
Project: Backmemed   File: EnchantmentHelper.java   View Source Code Vote up 6 votes
public static Map<Enchantment, Integer> getEnchantments(ItemStack stack)
{
    Map<Enchantment, Integer> map = Maps.<Enchantment, Integer>newLinkedHashMap();
    NBTTagList nbttaglist = stack.getItem() == Items.ENCHANTED_BOOK ? Items.ENCHANTED_BOOK.getEnchantments(stack) : stack.getEnchantmentTagList();

    if (nbttaglist != null)
    {
        for (int i = 0; i < nbttaglist.tagCount(); ++i)
        {
            Enchantment enchantment = Enchantment.getEnchantmentByID(nbttaglist.getCompoundTagAt(i).getShort("id"));
            int j = nbttaglist.getCompoundTagAt(i).getShort("lvl");
            map.put(enchantment, Integer.valueOf(j));
        }
    }

    return map;
}
 
Example 6
Project: Backmemed   File: CustomItems.java   View Source Code Vote up 6 votes
private static int[][] getEnchantmentIdLevels(ItemStack p_getEnchantmentIdLevels_0_)
{
    Item item = p_getEnchantmentIdLevels_0_.getItem();
    NBTTagList nbttaglist = item == Items.ENCHANTED_BOOK ? Items.ENCHANTED_BOOK.getEnchantments(p_getEnchantmentIdLevels_0_) : p_getEnchantmentIdLevels_0_.getEnchantmentTagList();

    if (nbttaglist != null && nbttaglist.tagCount() > 0)
    {
        int[][] aint = new int[nbttaglist.tagCount()][2];

        for (int i = 0; i < nbttaglist.tagCount(); ++i)
        {
            NBTTagCompound nbttagcompound = nbttaglist.getCompoundTagAt(i);
            int j = nbttagcompound.getShort("id");
            int k = nbttagcompound.getShort("lvl");
            aint[i][0] = j;
            aint[i][1] = k;
        }

        return aint;
    }
    else
    {
        return EMPTY_INT2_ARRAY;
    }
}
 
Example 7
Project: CustomWorldGen   File: EnchantmentHelper.java   View Source Code Vote up 6 votes
public static Map<Enchantment, Integer> getEnchantments(ItemStack stack)
{
    Map<Enchantment, Integer> map = Maps.<Enchantment, Integer>newLinkedHashMap();
    NBTTagList nbttaglist = stack.getItem() == Items.ENCHANTED_BOOK ? Items.ENCHANTED_BOOK.getEnchantments(stack) : stack.getEnchantmentTagList();

    if (nbttaglist != null)
    {
        for (int i = 0; i < nbttaglist.tagCount(); ++i)
        {
            Enchantment enchantment = Enchantment.getEnchantmentByID(nbttaglist.getCompoundTagAt(i).getShort("id"));
            int j = nbttaglist.getCompoundTagAt(i).getShort("lvl");
            map.put(enchantment, Integer.valueOf(j));
        }
    }

    return map;
}
 
Example 8
Project: pnc-repressurized   File: GuiSearcher.java   View Source Code Vote up 5 votes
private void getAllEnchantedBooks(Enchantment enchantment, NonNullList<ItemStack> list) {
    for (int i = enchantment.getMinLevel(); i <= enchantment.getMaxLevel(); ++i) {
        ItemStack itemstack = new ItemStack(Items.ENCHANTED_BOOK);
        itemstack.addEnchantment(enchantment, i);
        list.add(itemstack);
    }
}
 
Example 9
Project: VanillaExtras   File: TileEntityBlockBreaker.java   View Source Code Vote up 5 votes
public void updateCooldownCap() {
	int cap = this.worker.getMaxWork();
	if (this.world.getBlockState(pos).getValue(BlockBreaker.TYPE) == ChipTypes.BASIC)
		cap = VExConfig.machineCooldownBasic;
	else
		cap = VExConfig.machineCooldownAdvanced;
	if (this.handler.getStackInSlot(9).getItem() == Items.ENCHANTED_BOOK) {
		Map<Enchantment, Integer> enchantments = EnchantmentHelper.getEnchantments(this.handler.getStackInSlot(9));
		if (enchantments.containsKey(Enchantments.EFFICIENCY)) {
			cap -= Math.pow(enchantments.get(Enchantments.EFFICIENCY), 2) % cap;
		}
	}
	this.worker.setMaxCooldown(cap);
}
 
Example 10
Project: VanillaExtras   File: ContainerBlockBreaker.java   View Source Code Vote up 5 votes
@Override
public ItemStack transferStackInSlot(EntityPlayer playerIn, int fromSlot) {
	ItemStack previous = ItemStack.EMPTY;
	Slot slot = (Slot) this.inventorySlots.get(fromSlot);

	if (slot != null && slot.getHasStack()) {
		ItemStack current = slot.getStack();
		previous = current.copy();

		if (fromSlot < this.handler.getSlots()) {
			// From the block breaker inventory to player's inventory
			if (!this.mergeItemStack(current, handler.getSlots(), handler.getSlots() + 36, true))
				return ItemStack.EMPTY;
		} else {
			// From the player's inventory to block breaker's inventory
			if (current.getItem() == Items.ENCHANTED_BOOK) {
				if (!this.mergeItemStack(current, 9, handler.getSlots(), false))
					return ItemStack.EMPTY;
			}
			if (!this.mergeItemStack(current, 0, handler.getSlots(), false))
				return ItemStack.EMPTY;
		}

		if (current.getCount() == 0)
			slot.putStack(ItemStack.EMPTY);
		else
			slot.onSlotChanged();

		if (current.getCount() == previous.getCount())
			return null;
		slot.onTake(playerIn, current);
	}
	return previous;
}
 
Example 11
Project: VillagerTrades   File: TradeHandlers.java   View Source Code Vote up 5 votes
@Override
public ItemStack getSellingStack(ITradeList t)
{
	ListEnchantedBookForEmeralds trade = (ListEnchantedBookForEmeralds)t;
	ItemStack stack = new ItemStack(Items.ENCHANTED_BOOK, 1, 0);
	return stack;
}
 
Example 12
Project: Backmemed   File: EnchantmentHelper.java   View Source Code Vote up 5 votes
/**
 * Set the enchantments for the specified stack.
 */
public static void setEnchantments(Map<Enchantment, Integer> enchMap, ItemStack stack)
{
    NBTTagList nbttaglist = new NBTTagList();

    for (Entry<Enchantment, Integer> entry : enchMap.entrySet())
    {
        Enchantment enchantment = (Enchantment)entry.getKey();

        if (enchantment != null)
        {
            int i = ((Integer)entry.getValue()).intValue();
            NBTTagCompound nbttagcompound = new NBTTagCompound();
            nbttagcompound.setShort("id", (short)Enchantment.getEnchantmentID(enchantment));
            nbttagcompound.setShort("lvl", (short)i);
            nbttaglist.appendTag(nbttagcompound);

            if (stack.getItem() == Items.ENCHANTED_BOOK)
            {
                Items.ENCHANTED_BOOK.addEnchantment(stack, new EnchantmentData(enchantment, i));
            }
        }
    }

    if (nbttaglist.hasNoTags())
    {
        if (stack.hasTagCompound())
        {
            stack.getTagCompound().removeTag("ench");
        }
    }
    else if (stack.getItem() != Items.ENCHANTED_BOOK)
    {
        stack.setTagInfo("ench", nbttaglist);
    }
}
 
Example 13
Project: CustomWorldGen   File: EnchantmentHelper.java   View Source Code Vote up 5 votes
/**
 * Set the enchantments for the specified stack.
 */
public static void setEnchantments(Map<Enchantment, Integer> enchMap, ItemStack stack)
{
    NBTTagList nbttaglist = new NBTTagList();

    for (Entry<Enchantment, Integer> entry : enchMap.entrySet())
    {
        Enchantment enchantment = (Enchantment)entry.getKey();

        if (enchantment != null)
        {
            int i = ((Integer)entry.getValue()).intValue();
            NBTTagCompound nbttagcompound = new NBTTagCompound();
            nbttagcompound.setShort("id", (short)Enchantment.getEnchantmentID(enchantment));
            nbttagcompound.setShort("lvl", (short)i);
            nbttaglist.appendTag(nbttagcompound);

            if (stack.getItem() == Items.ENCHANTED_BOOK)
            {
                Items.ENCHANTED_BOOK.addEnchantment(stack, new EnchantmentData(enchantment, i));
            }
        }
    }

    if (nbttaglist.hasNoTags())
    {
        if (stack.hasTagCompound())
        {
            stack.getTagCompound().removeTag("ench");
        }
    }
    else if (stack.getItem() != Items.ENCHANTED_BOOK)
    {
        stack.setTagInfo("ench", nbttaglist);
    }
}
 
Example 14
Project: VanillaExtras   File: TileEntityBlockBreaker.java   View Source Code Vote up 4 votes
@SuppressWarnings("deprecation")
public void breakBlock(EnumFacing facing) {
	BlockPos newPos = pos.offset(facing, 1);
	IBlockState state = this.world.getBlockState(newPos);
	Block block = state.getBlock();
	if (!block.isAir(state, this.world, newPos) && block.getBlockHardness(state, this.world, newPos) >= 0
			&& !(block instanceof BlockDynamicLiquid) && !(block instanceof BlockStaticLiquid)) {
		// Creates a fake player which will berak the block
		EntityPlayer player = new EntityPlayer(world, new GameProfile(null, "BlockBreaker")) {

			@Override
			public boolean isSpectator() {
				return true;
			}

			@Override
			public boolean isCreative() {
				return false;
			}
		};
		List<ItemStack> drops = new ArrayList<ItemStack>();
		boolean customDrops = false;
		if (this.handler.getStackInSlot(9).getItem() == Items.ENCHANTED_BOOK) {
			ItemStack enchantedBook = this.handler.getStackInSlot(9);
			Map<Enchantment, Integer> enchantments = EnchantmentHelper.getEnchantments(enchantedBook);
			if (enchantments.containsKey(Enchantments.FORTUNE)) {
				int fortune = enchantments.get(Enchantments.FORTUNE);
				drops.add(new ItemStack(block.getItemDropped(state, this.random, fortune),
						block.quantityDroppedWithBonus(fortune, this.random), block.damageDropped(state)));
				customDrops = true;
			}
			if (enchantments.containsKey(Enchantments.SILK_TOUCH)
					&& block.canSilkHarvest(world, newPos, state, player)) {
				// HARD FIX FOR LAPIS
				if (block == Blocks.LAPIS_ORE)
					drops.add(new ItemStack(block, 1));
				else
					drops.add(new ItemStack(block, 1, block.damageDropped(state)));
				customDrops = true;
			}
		}
		if (!customDrops)
			drops = block.getDrops(world, newPos, state, 0);
		for (ItemStack stack : drops) {
			Utils.addStackToInventory(this.handler, 9, stack, false);
		}
		if (!Utils.isInventoryFull(this.handler, 9)) {
			this.world.playEvent(2001, pos, Block.getStateId(state));
			this.world.playSound(null, pos, block.getSoundType(state, world, newPos, player).getBreakSound(),
					SoundCategory.BLOCKS, 1, 1);
			this.world.setBlockToAir(newPos);
			if (block == Blocks.ICE)
				this.world.setBlockState(newPos, Blocks.FLOWING_WATER.getDefaultState());
		}
	}
}
 
Example 15
Project: Backmemed   File: GuiContainerCreative.java   View Source Code Vote up 4 votes
protected void renderToolTip(ItemStack stack, int x, int y)
{
    if (selectedTabIndex == CreativeTabs.SEARCH.getTabIndex())
    {
        List<String> list = stack.getTooltip(this.mc.player, this.mc.gameSettings.advancedItemTooltips);
        CreativeTabs creativetabs = stack.getItem().getCreativeTab();

        if (creativetabs == null && stack.getItem() == Items.ENCHANTED_BOOK)
        {
            Map<Enchantment, Integer> map = EnchantmentHelper.getEnchantments(stack);

            if (map.size() == 1)
            {
                Enchantment enchantment = (Enchantment)map.keySet().iterator().next();

                for (CreativeTabs creativetabs1 : CreativeTabs.CREATIVE_TAB_ARRAY)
                {
                    if (creativetabs1.hasRelevantEnchantmentType(enchantment.type))
                    {
                        creativetabs = creativetabs1;
                        break;
                    }
                }
            }
        }

        if (creativetabs != null)
        {
            list.add(1, "" + TextFormatting.BOLD + TextFormatting.BLUE + I18n.format(creativetabs.getTranslatedTabLabel(), new Object[0]));
        }

        for (int i = 0; i < list.size(); ++i)
        {
            if (i == 0)
            {
                list.set(i, stack.getRarity().rarityColor + (String)list.get(i));
            }
            else
            {
                list.set(i, TextFormatting.GRAY + (String)list.get(i));
            }
        }

        this.drawHoveringText(list, x, y);
    }
    else
    {
        super.renderToolTip(stack, x, y);
    }
}
 
Example 16
Project: CustomWorldGen   File: GuiContainerCreative.java   View Source Code Vote up 4 votes
protected void renderToolTip(ItemStack stack, int x, int y)
{
    if (selectedTabIndex == CreativeTabs.SEARCH.getTabIndex())
    {
        List<String> list = stack.getTooltip(this.mc.thePlayer, this.mc.gameSettings.advancedItemTooltips);
        CreativeTabs creativetabs = stack.getItem().getCreativeTab();

        if (creativetabs == null && stack.getItem() == Items.ENCHANTED_BOOK)
        {
            Map<Enchantment, Integer> map = EnchantmentHelper.getEnchantments(stack);

            if (map.size() == 1)
            {
                Enchantment enchantment = (Enchantment)map.keySet().iterator().next();

                for (CreativeTabs creativetabs1 : CreativeTabs.CREATIVE_TAB_ARRAY)
                {
                    if (creativetabs1.hasRelevantEnchantmentType(enchantment.type))
                    {
                        creativetabs = creativetabs1;
                        break;
                    }
                }
            }
        }

        if (creativetabs != null)
        {
            list.add(1, "" + TextFormatting.BOLD + TextFormatting.BLUE + I18n.format(creativetabs.getTranslatedTabLabel(), new Object[0]));
        }

        for (int i = 0; i < list.size(); ++i)
        {
            if (i == 0)
            {
                list.set(i, stack.getRarity().rarityColor + (String)list.get(i));
            }
            else
            {
                list.set(i, TextFormatting.GRAY + (String)list.get(i));
            }
        }

        net.minecraftforge.fml.client.config.GuiUtils.preItemToolTip(stack);
        this.drawHoveringText(list, x, y);
        net.minecraftforge.fml.client.config.GuiUtils.postItemToolTip();
    }
    else
    {
        super.renderToolTip(stack, x, y);
    }
}