Java Code Examples for net.minecraft.enchantment.Enchantment

The following are top voted examples for showing how to use net.minecraft.enchantment.Enchantment. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: pnc-repressurized   File: GuiSearcher.java   Source Code and License 6 votes vote down vote up
/**
 * Lazy cache.
 * @return
 */
private Stream<SearchEntry> getSearchEntries(){
    if(cachedSearchEntries == null){
        NonNullList<ItemStack> itemList = NonNullList.create();

        for(Item item : Item.REGISTRY){
            if (item != null && item.getCreativeTab() != null) {
                item.getSubItems(item.getCreativeTab(), itemList);
            }
        }

        for (Enchantment enchantment : Enchantment.REGISTRY) {
            if (enchantment != null && enchantment.type != null) {
                getAllEnchantedBooks(enchantment, itemList);
            }
        }

        cachedSearchEntries = itemList.stream().map(SearchEntry::new).collect(Collectors.toList());
    }
    return cachedSearchEntries.stream();
}
 
Example 2
Project: pnc-repressurized   File: PressureChamberPressureEnchantHandler.java   Source Code and License 6 votes vote down vote up
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 3
Project: pnc-repressurized   File: PressureChamberVacuumEnchantHandler.java   Source Code and License 6 votes vote down vote up
@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 4
Project: Industrial-Foregoing   File: EnchantmentExtractorTile.java   Source Code and License 6 votes vote down vote up
@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 5
Project: CustomWorldGen   File: EnchantRandomly.java   Source Code and License 6 votes vote down vote up
public void serialize(JsonObject object, EnchantRandomly functionClazz, JsonSerializationContext serializationContext)
{
    if (functionClazz.enchantments != null && !functionClazz.enchantments.isEmpty())
    {
        JsonArray jsonarray = new JsonArray();

        for (Enchantment enchantment : functionClazz.enchantments)
        {
            ResourceLocation resourcelocation = (ResourceLocation)Enchantment.REGISTRY.getNameForObject(enchantment);

            if (resourcelocation == null)
            {
                throw new IllegalArgumentException("Don\'t know how to serialize enchantment " + enchantment);
            }

            jsonarray.add(new JsonPrimitive(resourcelocation.toString()));
        }

        object.add("enchantments", jsonarray);
    }
}
 
Example 6
Project: DecompiledMinecraft   File: EntitySkeleton.java   Source Code and License 6 votes vote down vote up
/**
 * Attack the specified entity using a ranged attack.
 */
public void attackEntityWithRangedAttack(EntityLivingBase p_82196_1_, float p_82196_2_)
{
    EntityArrow entityarrow = new EntityArrow(this.worldObj, this, p_82196_1_, 1.6F, (float)(14 - this.worldObj.getDifficulty().getDifficultyId() * 4));
    int i = EnchantmentHelper.getEnchantmentLevel(Enchantment.power.effectId, this.getHeldItem());
    int j = EnchantmentHelper.getEnchantmentLevel(Enchantment.punch.effectId, this.getHeldItem());
    entityarrow.setDamage((double)(p_82196_2_ * 2.0F) + this.rand.nextGaussian() * 0.25D + (double)((float)this.worldObj.getDifficulty().getDifficultyId() * 0.11F));

    if (i > 0)
    {
        entityarrow.setDamage(entityarrow.getDamage() + (double)i * 0.5D + 0.5D);
    }

    if (j > 0)
    {
        entityarrow.setKnockbackStrength(j);
    }

    if (EnchantmentHelper.getEnchantmentLevel(Enchantment.flame.effectId, this.getHeldItem()) > 0 || this.getSkeletonType() == 1)
    {
        entityarrow.setFire(100);
    }

    this.playSound("random.bow", 1.0F, 1.0F / (this.getRNG().nextFloat() * 0.4F + 0.8F));
    this.worldObj.spawnEntityInWorld(entityarrow);
}
 
Example 7
Project: DecompiledMinecraft   File: ItemEnchantedBook.java   Source Code and License 6 votes vote down vote up
/**
 * allows items to add custom lines of information to the mouseover description
 */
public void addInformation(ItemStack stack, EntityPlayer playerIn, List<String> tooltip, boolean advanced)
{
    super.addInformation(stack, playerIn, tooltip, advanced);
    NBTTagList nbttaglist = this.getEnchantments(stack);

    if (nbttaglist != null)
    {
        for (int i = 0; i < nbttaglist.tagCount(); ++i)
        {
            int j = nbttaglist.getCompoundTagAt(i).getShort("id");
            int k = nbttaglist.getCompoundTagAt(i).getShort("lvl");

            if (Enchantment.getEnchantmentById(j) != null)
            {
                tooltip.add(Enchantment.getEnchantmentById(j).getTranslatedName(k));
            }
        }
    }
}
 
Example 8
Project: CustomWorldGen   File: Bootstrap.java   Source Code and License 6 votes vote down vote up
/**
 * Registers blocks, items, stats, etc.
 */
public static void register()
{
    if (!alreadyRegistered)
    {
        alreadyRegistered = true;

        if (LOGGER.isDebugEnabled())
        {
            redirectOutputToLog();
        }

        SoundEvent.registerSounds();
        Block.registerBlocks();
        BlockFire.init();
        Potion.registerPotions();
        Enchantment.registerEnchantments();
        Item.registerItems();
        PotionType.registerPotionTypes();
        PotionHelper.init();
        StatList.init();
        Biome.registerBiomes();
        registerDispenserBehaviors();
        net.minecraftforge.fml.common.registry.GameData.vanillaSnapshot();
    }
}
 
Example 9
Project: DecompiledMinecraft   File: CreativeTabs.java   Source Code and License 6 votes vote down vote up
/**
 * Adds the enchantment books from the supplied EnumEnchantmentType to the given list.
 */
public void addEnchantmentBooksToList(List<ItemStack> itemList, EnumEnchantmentType... enchantmentType)
{
    for (Enchantment enchantment : Enchantment.enchantmentsBookList)
    {
        if (enchantment != null && enchantment.type != null)
        {
            boolean flag = false;

            for (int i = 0; i < enchantmentType.length && !flag; ++i)
            {
                if (enchantment.type == enchantmentType[i])
                {
                    flag = true;
                }
            }

            if (flag)
            {
                itemList.add(Items.enchanted_book.getEnchantedItemStack(new EnchantmentData(enchantment, enchantment.getMaxLevel())));
            }
        }
    }
}
 
Example 10
Project: NemesisSystem   File: NemesisUtil.java   Source Code and License 6 votes vote down vote up
private static boolean improveEnchants(ItemStack item) {
	boolean improved = false;
	Map<Enchantment, Integer> enchantments = EnchantmentHelper.getEnchantments(item);

	if (enchantments.isEmpty()) {
		return false;
	}

	for (Entry<Enchantment, Integer> enchant : enchantments.entrySet()) {
		if (shouldImproveEnchantment(rand, enchant.getKey(), enchant.getValue())) {
			enchantments.put(enchant.getKey(), enchant.getValue() + 1);
			improved = true;
		}
	}

	if (improved) {
		EnchantmentHelper.setEnchantments(enchantments, item);
	}

	return improved;
}
 
Example 11
Project: CustomWorldGen   File: EntityVillager.java   Source Code and License 6 votes vote down vote up
/**
 * Affects the given MerchantRecipeList to possibly add or remove MerchantRecipes.
 */
public void modifyMerchantRecipeList(MerchantRecipeList recipeList, Random random)
{
    Enchantment enchantment = (Enchantment)Enchantment.REGISTRY.getRandomObject(random);
    int i = MathHelper.getRandomIntegerInRange(random, enchantment.getMinLevel(), enchantment.getMaxLevel());
    ItemStack itemstack = Items.ENCHANTED_BOOK.getEnchantedItemStack(new EnchantmentData(enchantment, i));
    int j = 2 + random.nextInt(5 + i * 10) + 3 * i;

    if (enchantment.isTreasureEnchantment())
    {
        j *= 2;
    }

    if (j > 64)
    {
        j = 64;
    }

    recipeList.add(new MerchantRecipe(new ItemStack(Items.BOOK), new ItemStack(Items.EMERALD, j), itemstack));
}
 
Example 12
Project: BaseClient   File: ItemEnchantedBook.java   Source Code and License 6 votes vote down vote up
/**
 * allows items to add custom lines of information to the mouseover description
 */
public void addInformation(ItemStack stack, EntityPlayer playerIn, List<String> tooltip, boolean advanced)
{
    super.addInformation(stack, playerIn, tooltip, advanced);
    NBTTagList nbttaglist = this.getEnchantments(stack);

    if (nbttaglist != null)
    {
        for (int i = 0; i < nbttaglist.tagCount(); ++i)
        {
            int j = nbttaglist.getCompoundTagAt(i).getShort("id");
            int k = nbttaglist.getCompoundTagAt(i).getShort("lvl");

            if (Enchantment.getEnchantmentById(j) != null)
            {
                tooltip.add(Enchantment.getEnchantmentById(j).getTranslatedName(k));
            }
        }
    }
}
 
Example 13
Project: BaseClient   File: ItemStack.java   Source Code and License 6 votes vote down vote up
/**
 * Adds an enchantment with a desired level on the ItemStack.
 */
public void addEnchantment(Enchantment ench, int level)
{
    if (this.stackTagCompound == null)
    {
        this.setTagCompound(new NBTTagCompound());
    }

    if (!this.stackTagCompound.hasKey("ench", 9))
    {
        this.stackTagCompound.setTag("ench", new NBTTagList());
    }

    NBTTagList nbttaglist = this.stackTagCompound.getTagList("ench", 10);
    NBTTagCompound nbttagcompound = new NBTTagCompound();
    nbttagcompound.setShort("id", (short)ench.effectId);
    nbttagcompound.setShort("lvl", (short)((byte)level));
    nbttaglist.appendTag(nbttagcompound);
}
 
Example 14
Project: CustomWorldGen   File: ItemStack.java   Source Code and License 6 votes vote down vote up
/**
 * Adds an enchantment with a desired level on the ItemStack.
 */
public void addEnchantment(Enchantment ench, int level)
{
    if (this.stackTagCompound == null)
    {
        this.setTagCompound(new NBTTagCompound());
    }

    if (!this.stackTagCompound.hasKey("ench", 9))
    {
        this.stackTagCompound.setTag("ench", new NBTTagList());
    }

    NBTTagList nbttaglist = this.stackTagCompound.getTagList("ench", 10);
    NBTTagCompound nbttagcompound = new NBTTagCompound();
    nbttagcompound.setShort("id", (short)Enchantment.getEnchantmentID(ench));
    nbttagcompound.setShort("lvl", (short)((byte)level));
    nbttaglist.appendTag(nbttagcompound);
}
 
Example 15
Project: BaseClient   File: EntitySkeleton.java   Source Code and License 6 votes vote down vote up
/**
 * Attack the specified entity using a ranged attack.
 */
public void attackEntityWithRangedAttack(EntityLivingBase p_82196_1_, float p_82196_2_)
{
    EntityArrow entityarrow = new EntityArrow(this.worldObj, this, p_82196_1_, 1.6F, (float)(14 - this.worldObj.getDifficulty().getDifficultyId() * 4));
    int i = EnchantmentHelper.getEnchantmentLevel(Enchantment.power.effectId, this.getHeldItem());
    int j = EnchantmentHelper.getEnchantmentLevel(Enchantment.punch.effectId, this.getHeldItem());
    entityarrow.setDamage((double)(p_82196_2_ * 2.0F) + this.rand.nextGaussian() * 0.25D + (double)((float)this.worldObj.getDifficulty().getDifficultyId() * 0.11F));

    if (i > 0)
    {
        entityarrow.setDamage(entityarrow.getDamage() + (double)i * 0.5D + 0.5D);
    }

    if (j > 0)
    {
        entityarrow.setKnockbackStrength(j);
    }

    if (EnchantmentHelper.getEnchantmentLevel(Enchantment.flame.effectId, this.getHeldItem()) > 0 || this.getSkeletonType() == 1)
    {
        entityarrow.setFire(100);
    }

    this.playSound("random.bow", 1.0F, 1.0F / (this.getRNG().nextFloat() * 0.4F + 0.8F));
    this.worldObj.spawnEntityInWorld(entityarrow);
}
 
Example 16
Project: BaseClient   File: ItemEnchantedBook.java   Source Code and License 6 votes vote down vote up
/**
 * allows items to add custom lines of information to the mouseover description
 */
public void addInformation(ItemStack stack, EntityPlayer playerIn, List<String> tooltip, boolean advanced)
{
    super.addInformation(stack, playerIn, tooltip, advanced);
    NBTTagList nbttaglist = this.getEnchantments(stack);

    if (nbttaglist != null)
    {
        for (int i = 0; i < nbttaglist.tagCount(); ++i)
        {
            int j = nbttaglist.getCompoundTagAt(i).getShort("id");
            int k = nbttaglist.getCompoundTagAt(i).getShort("lvl");

            if (Enchantment.getEnchantmentById(j) != null)
            {
                tooltip.add(Enchantment.getEnchantmentById(j).getTranslatedName(k));
            }
        }
    }
}
 
Example 17
Project: CustomWorldGen   File: EnchantRandomly.java   Source Code and License 6 votes vote down vote up
public EnchantRandomly deserialize(JsonObject object, JsonDeserializationContext deserializationContext, LootCondition[] conditionsIn)
{
    List<Enchantment> list = null;

    if (object.has("enchantments"))
    {
        list = Lists.<Enchantment>newArrayList();

        for (JsonElement jsonelement : JsonUtils.getJsonArray(object, "enchantments"))
        {
            String s = JsonUtils.getString(jsonelement, "enchantment");
            Enchantment enchantment = (Enchantment)Enchantment.REGISTRY.getObject(new ResourceLocation(s));

            if (enchantment == null)
            {
                throw new JsonSyntaxException("Unknown enchantment \'" + s + "\'");
            }

            list.add(enchantment);
        }
    }

    return new EnchantRandomly(conditionsIn, list);
}
 
Example 18
Project: BaseClient   File: ItemStack.java   Source Code and License 6 votes vote down vote up
/**
 * Adds an enchantment with a desired level on the ItemStack.
 */
public void addEnchantment(Enchantment ench, int level)
{
    if (this.stackTagCompound == null)
    {
        this.setTagCompound(new NBTTagCompound());
    }

    if (!this.stackTagCompound.hasKey("ench", 9))
    {
        this.stackTagCompound.setTag("ench", new NBTTagList());
    }

    NBTTagList nbttaglist = this.stackTagCompound.getTagList("ench", 10);
    NBTTagCompound nbttagcompound = new NBTTagCompound();
    nbttagcompound.setShort("id", (short)ench.effectId);
    nbttagcompound.setShort("lvl", (short)((byte)level));
    nbttaglist.appendTag(nbttagcompound);
}
 
Example 19
Project: CustomWorldGen   File: CreativeTabs.java   Source Code and License 6 votes vote down vote up
/**
 * Adds the enchantment books from the supplied EnumEnchantmentType to the given list.
 */
@SideOnly(Side.CLIENT)
public void addEnchantmentBooksToList(List<ItemStack> itemList, EnumEnchantmentType... enchantmentType)
{
    for (Enchantment enchantment : Enchantment.REGISTRY)
    {
        if (enchantment != null && enchantment.type != null)
        {
            boolean flag = false;

            for (int i = 0; i < enchantmentType.length && !flag; ++i)
            {
                if (enchantment.type == enchantmentType[i])
                {
                    flag = true;
                }
            }

            if (flag)
            {
                itemList.add(Items.ENCHANTED_BOOK.getEnchantedItemStack(new EnchantmentData(enchantment, enchantment.getMaxLevel())));
            }
        }
    }
}
 
Example 20
Project: BaseClient   File: CreativeTabs.java   Source Code and License 6 votes vote down vote up
/**
 * Adds the enchantment books from the supplied EnumEnchantmentType to the given list.
 */
public void addEnchantmentBooksToList(List<ItemStack> itemList, EnumEnchantmentType... enchantmentType)
{
    for (Enchantment enchantment : Enchantment.enchantmentsBookList)
    {
        if (enchantment != null && enchantment.type != null)
        {
            boolean flag = false;

            for (int i = 0; i < enchantmentType.length && !flag; ++i)
            {
                if (enchantment.type == enchantmentType[i])
                {
                    flag = true;
                }
            }

            if (flag)
            {
                itemList.add(Items.enchanted_book.getEnchantedItemStack(new EnchantmentData(enchantment, enchantment.getMaxLevel())));
            }
        }
    }
}
 
Example 21
Project: mc-Slingshot   File: RecipeLooting.java   Source Code and License 6 votes vote down vote up
static int getBookLootingLevel(ItemStack stack) {
   NBTTagList nbttaglist = Items.ENCHANTED_BOOK.getEnchantments(stack);

   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");
         if (Enchantments.LOOTING == enchantment)
            return j;
      }
   }

   return 0;
}
 
Example 22
Project: SerenityCE   File: AutoTool.java   Source Code and License 6 votes vote down vote up
private int getBestSlotMining(Block block) {
    int bestSlot = -1;
    float bestHardness = 1F;

    for (int index = 36; index < 45; index++) {
        ItemStack stack = mc.thePlayer.inventoryContainer.getSlot(index).getStack();
        if (stack != null) {
            float str = stack.getStrVsBlock(block);
            if (str > 1F) {
                int efficiencyLevel = EnchantmentHelper.getEnchantmentLevel(Enchantment.efficiency.effectId, stack);
                str += (efficiencyLevel * efficiencyLevel + 1);
            }

            if (str > bestHardness) {
                bestHardness = str;
                bestSlot = index;
            }
        }
    }

    return bestSlot;
}
 
Example 23
Project: Backmemed   File: EnchantRandomly.java   Source Code and License 6 votes vote down vote up
public void serialize(JsonObject object, EnchantRandomly functionClazz, JsonSerializationContext serializationContext)
{
    if (!functionClazz.enchantments.isEmpty())
    {
        JsonArray jsonarray = new JsonArray();

        for (Enchantment enchantment : functionClazz.enchantments)
        {
            ResourceLocation resourcelocation = (ResourceLocation)Enchantment.REGISTRY.getNameForObject(enchantment);

            if (resourcelocation == null)
            {
                throw new IllegalArgumentException("Don\'t know how to serialize enchantment " + enchantment);
            }

            jsonarray.add(new JsonPrimitive(resourcelocation.toString()));
        }

        object.add("enchantments", jsonarray);
    }
}
 
Example 24
Project: Technical   File: TechnicalTools.java   Source Code and License 6 votes vote down vote up
public static ItemStack enchantTool(ItemStack tool, ToolMaterial material) {
	Item itemTool = tool.getItem();
	if(itemTool instanceof TechnicalTool) {
		if(material == UraniumDioxide) {
			tool.addEnchantment(Technical.enchantmantRadioactivity, 1);
			tool.addEnchantment(Enchantment.unbreaking, 2);
		} else if(material == Barium)
			tool.addEnchantment(Enchantment.unbreaking, 3);
		else if(material == Chromium)
			tool.addEnchantment(Enchantment.unbreaking, 4);
		else if(material == Tungsten)
			tool.addEnchantment(Enchantment.unbreaking, 5);
		else if(material == Beryllium)
			tool.addEnchantment(Enchantment.unbreaking, 10);
	}
	return tool;
}
 
Example 25
Project: Backmemed   File: Bootstrap.java   Source Code and License 6 votes vote down vote up
/**
 * Registers blocks, items, stats, etc.
 */
public static void register()
{
    if (!alreadyRegistered)
    {
        alreadyRegistered = true;
        redirectOutputToLog();
        SoundEvent.registerSounds();
        Block.registerBlocks();
        BlockFire.init();
        Potion.registerPotions();
        Enchantment.registerEnchantments();
        Item.registerItems();
        PotionType.registerPotionTypes();
        PotionHelper.init();
        EntityList.init();
        StatList.init();
        Biome.registerBiomes();
        registerDispenserBehaviors();
    }
}
 
Example 26
Project: Backmemed   File: ItemEnchantedBook.java   Source Code and License 6 votes vote down vote up
/**
 * allows items to add custom lines of information to the mouseover description
 */
public void addInformation(ItemStack stack, EntityPlayer playerIn, List<String> tooltip, boolean advanced)
{
    super.addInformation(stack, playerIn, tooltip, advanced);
    NBTTagList nbttaglist = this.getEnchantments(stack);

    if (nbttaglist != null)
    {
        for (int i = 0; i < nbttaglist.tagCount(); ++i)
        {
            int j = nbttaglist.getCompoundTagAt(i).getShort("id");
            int k = nbttaglist.getCompoundTagAt(i).getShort("lvl");

            if (Enchantment.getEnchantmentByID(j) != null)
            {
                tooltip.add(Enchantment.getEnchantmentByID(j).getTranslatedName(k));
            }
        }
    }
}
 
Example 27
Project: IceMod   File: IceFragment.java   Source Code and License 6 votes vote down vote up
/** Makes your Item Enchanted when it is crafted */
public void onCreated(ItemStack item, World world, EntityPlayer player) 
{
    item.addEnchantment(Enchantment.sharpness, 5);
    // Replace the "." after "Enchantment" to see options
    // The number is the Enchantment Level
}
 
Example 28
Project: Backmemed   File: ItemStack.java   Source Code and License 6 votes vote down vote up
/**
 * Adds an enchantment with a desired level on the ItemStack.
 */
public void addEnchantment(Enchantment ench, int level)
{
    if (this.stackTagCompound == null)
    {
        this.setTagCompound(new NBTTagCompound());
    }

    if (!this.stackTagCompound.hasKey("ench", 9))
    {
        this.stackTagCompound.setTag("ench", new NBTTagList());
    }

    NBTTagList nbttaglist = this.stackTagCompound.getTagList("ench", 10);
    NBTTagCompound nbttagcompound = new NBTTagCompound();
    nbttagcompound.setShort("id", (short)Enchantment.getEnchantmentID(ench));
    nbttagcompound.setShort("lvl", (short)((byte)level));
    nbttaglist.appendTag(nbttagcompound);
}
 
Example 29
Project: pnc-repressurized   File: PressureChamberPressureEnchantHandler.java   Source Code and License 5 votes vote down vote up
@Override
public NonNullList<ItemStack> craftRecipe(ItemStackHandler inputStacks) {
    ItemStack[] recipeIngredients = getRecipeIngredients(inputStacks);
    ItemStack enchantedTool = recipeIngredients[0];
    ItemStack enchantedBook = recipeIngredients[1];
    
    Map<Enchantment, Integer> bookMap = EnchantmentHelper.getEnchantments(enchantedBook);
    bookMap.forEach((enchant, lvl) -> enchantedTool.addEnchantment(enchant, lvl));
    
    enchantedBook.shrink(1);
    return NonNullList.from(ItemStack.EMPTY, new ItemStack(Items.BOOK));
}
 
Example 30
Project: AdvancedCombat   File: RecipeEnchantmentUpgrade.java   Source Code and License 5 votes vote down vote up
protected RecipeEnchantmentUpgrade(@Nonnull ItemStack upgrade, Enchantment toUpgrade, int minLevel, int maxLevel) {
	this.upgrade = upgrade;
	this.enchant = toUpgrade;
	this.minLevel = minLevel;
	this.maxLevel = maxLevel;
	allUpgrades.add(this);
}
 
Example 31
Project: Randores2   File: EmpoweredEnchantment.java   Source Code and License 5 votes vote down vote up
public static boolean appliedTo(ItemStack stack) {
    for (Enchantment en : EnchantmentHelper.getEnchantments(stack).keySet()) {
        if (en instanceof EmpoweredEnchantment) {
            return true;
        }
    }

    return false;
}
 
Example 32
Project: DecompiledMinecraft   File: EntityVillager.java   Source Code and License 5 votes vote down vote up
public void modifyMerchantRecipeList(MerchantRecipeList recipeList, Random random)
{
    Enchantment enchantment = Enchantment.enchantmentsBookList[random.nextInt(Enchantment.enchantmentsBookList.length)];
    int i = MathHelper.getRandomIntegerInRange(random, enchantment.getMinLevel(), enchantment.getMaxLevel());
    ItemStack itemstack = Items.enchanted_book.getEnchantedItemStack(new EnchantmentData(enchantment, i));
    int j = 2 + random.nextInt(5 + i * 10) + 3 * i;

    if (j > 64)
    {
        j = 64;
    }

    recipeList.add(new MerchantRecipe(new ItemStack(Items.book), new ItemStack(Items.emerald, j), itemstack));
}
 
Example 33
Project: DecompiledMinecraft   File: ItemStack.java   Source Code and License 5 votes vote down vote up
/**
 * Attempts to damage the ItemStack with par1 amount of damage, If the ItemStack has the Unbreaking enchantment
 * there is a chance for each point of damage to be negated. Returns true if it takes more damage than
 * getMaxDamage(). Returns false otherwise or if the ItemStack can't be damaged or if all points of damage are
 * negated.
 */
public boolean attemptDamageItem(int amount, Random rand)
{
    if (!this.isItemStackDamageable())
    {
        return false;
    }
    else
    {
        if (amount > 0)
        {
            int i = EnchantmentHelper.getEnchantmentLevel(Enchantment.unbreaking.effectId, this);
            int j = 0;

            for (int k = 0; i > 0 && k < amount; ++k)
            {
                if (EnchantmentDurability.negateDamage(this, i, rand))
                {
                    ++j;
                }
            }

            amount -= j;

            if (amount <= 0)
            {
                return false;
            }
        }

        this.itemDamage += amount;
        return this.itemDamage > this.getMaxDamage();
    }
}
 
Example 34
Project: BetterBeginningsReborn   File: InfusionRepairUtil.java   Source Code and License 5 votes vote down vote up
public static List<RecipeElement> getEnchantmentItems(NBTTagList enchants)
{
	List<RecipeElement> requiredItems = new ArrayList<>();

	for (int i = 0; i < enchants.tagCount(); i++)
	{
		NBTTagCompound enchant = enchants.getCompoundTagAt(i);
		int enchIdInt = enchant.getInteger("id");
		int level = enchant.getInteger("lvl");
		RecipeElement associated;
		if(enchIdInt != 0)
		{
			associated = enchantmentMapping.get(Enchantment.REGISTRY.getObjectById(enchIdInt).getRegistryName());
		}
		else
		{
			String enchIdString = enchant.getString("id");
			associated = enchantmentMapping.get(new ResourceLocation(enchIdString));
		}

		if (associated != null)
		{
			RecipeElement req = associated.copy();
			req.setStackSize(req.getStackSize() * level);
			requiredItems.add(req);
		}
		else
		{
			//LogUtil.log(Level.ERROR, "No repair infusion item associated with enchantment #" + enchId); // This spams the console
		}
	}

	return requiredItems;
}
 
Example 35
Project: DecompiledMinecraft   File: EntityVillager.java   Source Code and License 5 votes vote down vote up
public void modifyMerchantRecipeList(MerchantRecipeList recipeList, Random random)
{
    Enchantment enchantment = Enchantment.enchantmentsBookList[random.nextInt(Enchantment.enchantmentsBookList.length)];
    int i = MathHelper.getRandomIntegerInRange(random, enchantment.getMinLevel(), enchantment.getMaxLevel());
    ItemStack itemstack = Items.enchanted_book.getEnchantedItemStack(new EnchantmentData(enchantment, i));
    int j = 2 + random.nextInt(5 + i * 10) + 3 * i;

    if (j > 64)
    {
        j = 64;
    }

    recipeList.add(new MerchantRecipe(new ItemStack(Items.book), new ItemStack(Items.emerald, j), itemstack));
}
 
Example 36
Project: DecompiledMinecraft   File: ItemStack.java   Source Code and License 5 votes vote down vote up
/**
 * Attempts to damage the ItemStack with par1 amount of damage, If the ItemStack has the Unbreaking enchantment
 * there is a chance for each point of damage to be negated. Returns true if it takes more damage than
 * getMaxDamage(). Returns false otherwise or if the ItemStack can't be damaged or if all points of damage are
 * negated.
 */
public boolean attemptDamageItem(int amount, Random rand)
{
    if (!this.isItemStackDamageable())
    {
        return false;
    }
    else
    {
        if (amount > 0)
        {
            int i = EnchantmentHelper.getEnchantmentLevel(Enchantment.unbreaking.effectId, this);
            int j = 0;

            for (int k = 0; i > 0 && k < amount; ++k)
            {
                if (EnchantmentDurability.negateDamage(this, i, rand))
                {
                    ++j;
                }
            }

            amount -= j;

            if (amount <= 0)
            {
                return false;
            }
        }

        this.itemDamage += amount;
        return this.itemDamage > this.getMaxDamage();
    }
}
 
Example 37
Project: IceMod   File: FrozenBeef.java   Source Code and License 5 votes vote down vote up
/** Makes your Item Enchanted when it is crafted */
public void onCreated(ItemStack item, World world, EntityPlayer player) 
{
    item.addEnchantment(Enchantment.knockback, 10);
    // Replace the "." after "Enchantment" to see options
    // The number is the Enchantment Level
}
 
Example 38
Project: BaseClient   File: EntityVillager.java   Source Code and License 5 votes vote down vote up
public void modifyMerchantRecipeList(MerchantRecipeList recipeList, Random random)
{
    Enchantment enchantment = Enchantment.enchantmentsBookList[random.nextInt(Enchantment.enchantmentsBookList.length)];
    int i = MathHelper.getRandomIntegerInRange(random, enchantment.getMinLevel(), enchantment.getMaxLevel());
    ItemStack itemstack = Items.enchanted_book.getEnchantedItemStack(new EnchantmentData(enchantment, i));
    int j = 2 + random.nextInt(5 + i * 10) + 3 * i;

    if (j > 64)
    {
        j = 64;
    }

    recipeList.add(new MerchantRecipe(new ItemStack(Items.book), new ItemStack(Items.emerald, j), itemstack));
}
 
Example 39
Project: IceMod   File: FrozenStick.java   Source Code and License 5 votes vote down vote up
/** Makes your Item Enchanted when it is crafted */
//Ice can be very sharp
        public void onCreated(ItemStack item, World world, EntityPlayer player) 
        {
            item.addEnchantment(Enchantment.sharpness, 7);
            // Replace the "." after "Enchantment" to see options
            // The number is the Enchantment Level
        }
 
Example 40
Project: BaseClient   File: ItemStack.java   Source Code and License 5 votes vote down vote up
/**
 * Attempts to damage the ItemStack with par1 amount of damage, If the ItemStack has the Unbreaking enchantment
 * there is a chance for each point of damage to be negated. Returns true if it takes more damage than
 * getMaxDamage(). Returns false otherwise or if the ItemStack can't be damaged or if all points of damage are
 * negated.
 */
public boolean attemptDamageItem(int amount, Random rand)
{
    if (!this.isItemStackDamageable())
    {
        return false;
    }
    else
    {
        if (amount > 0)
        {
            int i = EnchantmentHelper.getEnchantmentLevel(Enchantment.unbreaking.effectId, this);
            int j = 0;

            for (int k = 0; i > 0 && k < amount; ++k)
            {
                if (EnchantmentDurability.negateDamage(this, i, rand))
                {
                    ++j;
                }
            }

            amount -= j;

            if (amount <= 0)
            {
                return false;
            }
        }

        this.itemDamage += amount;
        return this.itemDamage > this.getMaxDamage();
    }
}