Search in sources :

Example 51 with Enchantment

use of net.minecraft.enchantment.Enchantment in project MineFactoryReloaded by powercrystals.

the class TileEntityAutoAnvil method getAnvilOutput.

private ItemStack getAnvilOutput() {
    ItemStack startingItem = _inventory[0];
    this.maximumCost = 0;
    int totalEnchCost = 0;
    if (startingItem == null) {
        return null;
    } else {
        ItemStack outputItem = startingItem.copy();
        ItemStack addedItem = _inventory[1];
        @SuppressWarnings("unchecked") Map<Integer, Integer> existingEnchantments = EnchantmentHelper.getEnchantments(outputItem);
        boolean enchantingWithBook = false;
        int repairCost = outputItem.getRepairCost() + (addedItem == null ? 0 : addedItem.getRepairCost());
        this.stackSizeToBeUsedInRepair = 0;
        if (addedItem != null) {
            enchantingWithBook = addedItem.itemID == Item.enchantedBook.itemID && Item.enchantedBook.func_92110_g(addedItem).tagCount() > 0;
            if (outputItem.isItemStackDamageable() && Item.itemsList[outputItem.itemID].getIsRepairable(outputItem, addedItem)) {
                int currentDamage = Math.min(outputItem.getItemDamageForDisplay(), outputItem.getMaxDamage() / 4);
                if (currentDamage <= 0) {
                    return null;
                }
                int repairStackSize;
                for (repairStackSize = 0; currentDamage > 0 && repairStackSize < addedItem.stackSize; repairStackSize++) {
                    outputItem.setItemDamage(outputItem.getItemDamageForDisplay() - currentDamage);
                    totalEnchCost += Math.max(1, currentDamage / 100) + existingEnchantments.size();
                    currentDamage = Math.min(outputItem.getItemDamageForDisplay(), outputItem.getMaxDamage() / 4);
                }
                this.stackSizeToBeUsedInRepair = repairStackSize;
            } else {
                if (!enchantingWithBook && (outputItem.itemID != addedItem.itemID || !outputItem.isItemStackDamageable())) {
                    return null;
                }
                if (outputItem.isItemStackDamageable() && !enchantingWithBook) {
                    int currentDamage = outputItem.getMaxDamage() - outputItem.getItemDamageForDisplay();
                    int addedItemDamage = addedItem.getMaxDamage() - addedItem.getItemDamageForDisplay();
                    int newDamage = addedItemDamage + outputItem.getMaxDamage() * 12 / 100;
                    int leftoverDamage = currentDamage + newDamage;
                    int repairedDamage = outputItem.getMaxDamage() - leftoverDamage;
                    if (repairedDamage < 0) {
                        repairedDamage = 0;
                    }
                    if (repairedDamage < outputItem.getItemDamage()) {
                        outputItem.setItemDamage(repairedDamage);
                        totalEnchCost += Math.max(1, newDamage / 100);
                    }
                }
                @SuppressWarnings("unchecked") Map<Integer, Integer> addedEnchantments = EnchantmentHelper.getEnchantments(addedItem);
                for (Integer addedEnchId : addedEnchantments.keySet()) {
                    Enchantment enchantment = Enchantment.enchantmentsList[addedEnchId];
                    int existingEnchLevel = existingEnchantments.containsKey(addedEnchId) ? existingEnchantments.get(addedEnchId) : 0;
                    int addedEnchLevel = addedEnchantments.get(addedEnchId);
                    int newEnchLevel;
                    if (existingEnchLevel == addedEnchLevel) {
                        ++addedEnchLevel;
                        newEnchLevel = addedEnchLevel;
                    } else {
                        newEnchLevel = Math.max(addedEnchLevel, existingEnchLevel);
                    }
                    addedEnchLevel = newEnchLevel;
                    int levelDifference = addedEnchLevel - existingEnchLevel;
                    boolean canEnchantmentBeAdded = enchantment.canApply(outputItem);
                    if (outputItem.itemID == ItemEnchantedBook.enchantedBook.itemID) {
                        canEnchantmentBeAdded = true;
                    }
                    for (Integer existingEnchId : existingEnchantments.keySet()) {
                        if (existingEnchId != addedEnchId && !enchantment.canApplyTogether(Enchantment.enchantmentsList[existingEnchId])) {
                            canEnchantmentBeAdded = false;
                            totalEnchCost += levelDifference;
                        }
                    }
                    if (canEnchantmentBeAdded) {
                        if (newEnchLevel > enchantment.getMaxLevel()) {
                            newEnchLevel = enchantment.getMaxLevel();
                        }
                        existingEnchantments.put(Integer.valueOf(addedEnchId), Integer.valueOf(newEnchLevel));
                        int enchCost = 0;
                        switch(enchantment.getWeight()) {
                            case 1:
                                enchCost = 8;
                                break;
                            case 2:
                                enchCost = 4;
                            case 3:
                            case 4:
                            case 6:
                            case 7:
                            case 8:
                            case 9:
                            default:
                                break;
                            case 5:
                                enchCost = 2;
                                break;
                            case 10:
                                enchCost = 1;
                        }
                        if (enchantingWithBook) {
                            enchCost = Math.max(1, enchCost / 2);
                        }
                        totalEnchCost += enchCost * levelDifference;
                    }
                }
            }
        }
        int enchCount = 0;
        for (Integer existingEnchId : existingEnchantments.keySet()) {
            Enchantment enchantment = Enchantment.enchantmentsList[existingEnchId];
            int existingEnchLevel = existingEnchantments.get(existingEnchId);
            int enchCost = 0;
            ++enchCount;
            switch(enchantment.getWeight()) {
                case 1:
                    enchCost = 8;
                    break;
                case 2:
                    enchCost = 4;
                case 3:
                case 4:
                case 6:
                case 7:
                case 8:
                case 9:
                default:
                    break;
                case 5:
                    enchCost = 2;
                    break;
                case 10:
                    enchCost = 1;
            }
            if (enchantingWithBook) {
                enchCost = Math.max(1, enchCost / 2);
            }
            repairCost += enchCount + existingEnchLevel * enchCost;
        }
        if (enchantingWithBook) {
            repairCost = Math.max(1, repairCost / 2);
        }
        if (enchantingWithBook && !Item.itemsList[outputItem.itemID].isBookEnchantable(outputItem, addedItem)) {
            outputItem = null;
        }
        this.maximumCost = repairCost + totalEnchCost;
        if (totalEnchCost <= 0) {
            outputItem = null;
        }
        if (outputItem != null) {
            EnchantmentHelper.setEnchantments(existingEnchantments, outputItem);
        }
        return outputItem;
    }
}
Also used : ItemStack(net.minecraft.item.ItemStack) Enchantment(net.minecraft.enchantment.Enchantment)

Example 52 with Enchantment

use of net.minecraft.enchantment.Enchantment in project DefiledLands by Lykrast.

the class EntityBookWyrm method processInteract.

public boolean processInteract(EntityPlayer player, EnumHand hand) {
    if (!super.processInteract(player, hand)) {
        ItemStack itemstack = player.getHeldItem(hand);
        if (itemstack.getItem() == Items.ENCHANTED_BOOK && !isChild()) {
            Map<Enchantment, Integer> list = EnchantmentHelper.getEnchantments(itemstack);
            if (list.isEmpty())
                return false;
            int i = 0;
            for (Entry<Enchantment, Integer> e : list.entrySet()) {
                i += e.getKey().getMinEnchantability(e.getValue());
            }
            i = (int) (i * Config.conversionRate);
            if (i > 0) {
                digesting += i;
                if (digestTimer == 0)
                    digestTimer = getDigestTime();
                if (!player.capabilities.isCreativeMode) {
                    itemstack.shrink(1);
                }
                this.playSound(SoundEvents.ENTITY_PLAYER_BURP, 1.0F, this.rand.nextFloat() * 0.1F + 0.9F);
                return true;
            }
            return false;
        } else {
            return false;
        }
    } else {
        return true;
    }
}
Also used : ItemStack(net.minecraft.item.ItemStack) Enchantment(net.minecraft.enchantment.Enchantment)

Aggregations

Enchantment (net.minecraft.enchantment.Enchantment)52 ItemStack (net.minecraft.item.ItemStack)29 Item (net.minecraft.item.Item)9 ArrayList (java.util.ArrayList)8 Map (java.util.Map)7 EnchantmentData (net.minecraft.enchantment.EnchantmentData)7 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)6 NBTTagList (net.minecraft.nbt.NBTTagList)6 HashMap (java.util.HashMap)5 Iterator (java.util.Iterator)4 EntityLivingBase (net.minecraft.entity.EntityLivingBase)4 ResourceLocation (net.minecraft.util.ResourceLocation)4 List (java.util.List)3 Block (net.minecraft.block.Block)3 ModelResourceLocation (net.minecraft.client.renderer.block.model.ModelResourceLocation)3 EnchantmentHelper (net.minecraft.enchantment.EnchantmentHelper)3 Entity (net.minecraft.entity.Entity)3 EntityPlayer (net.minecraft.entity.player.EntityPlayer)3 ItemEnchantedBook (net.minecraft.item.ItemEnchantedBook)3 ImmutableList (com.google.common.collect.ImmutableList)2