Search in sources :

Example 11 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 12 with Enchantment

use of net.minecraft.enchantment.Enchantment in project Railcraft by Railcraft.

the class ContainerAnvil method updateRepairOutput.

@Override
public void updateRepairOutput() {
    ItemStack input1original = inputSlots.getStackInSlot(0);
    this.maximumCost = 1;
    int enchantCost = 0;
    int baseCost = 0;
    int nameCost = 0;
    if (input1original == null) {
        outputSlot.setInventorySlotContents(0, null);
        this.maximumCost = 0;
    } else {
        ItemStack input1 = input1original.copy();
        ItemStack input2 = inputSlots.getStackInSlot(1);
        Map<Enchantment, Integer> input1Enchantments = EnchantmentHelper.getEnchantments(input1);
        baseCost = baseCost + input1original.getRepairCost() + (InvTools.isEmpty(input2) ? 0 : input2.getRepairCost());
        this.materialCost = 0;
        boolean isEnchantedBook = false;
        if (!InvTools.isEmpty(input2)) {
            if (!net.minecraftforge.common.ForgeHooks.onAnvilChange(this, input1original, input2, outputSlot, repairedItemName, baseCost))
                return;
            isEnchantedBook = input2.getItem() == Items.ENCHANTED_BOOK && Items.ENCHANTED_BOOK.getEnchantments(input2).tagCount() > 0;
            if (input1.isItemStackDamageable() && input1.getItem().getIsRepairable(input1original, input2)) {
                int damageToRepair = Math.min(input1.getItemDamage(), input1.getMaxDamage() / 4);
                if (damageToRepair <= 0) {
                    outputSlot.setInventorySlotContents(0, InvTools.emptyStack());
                    this.maximumCost = 0;
                    return;
                }
                int projectedMaterialCost;
                for (projectedMaterialCost = 0; damageToRepair > 0 && projectedMaterialCost < input2.stackSize; ++projectedMaterialCost) {
                    int repairedDamage = input1.getItemDamage() - damageToRepair;
                    input1.setItemDamage(repairedDamage);
                    ++enchantCost;
                    damageToRepair = Math.min(input1.getItemDamage(), input1.getMaxDamage() / 4);
                }
                this.materialCost = projectedMaterialCost;
            } else {
                if (!isEnchantedBook && (input1.getItem() != input2.getItem() || !input1.isItemStackDamageable())) {
                    outputSlot.setInventorySlotContents(0, InvTools.emptyStack());
                    this.maximumCost = 0;
                    return;
                }
                if (input1.isItemStackDamageable() && !isEnchantedBook) {
                    int health1 = input1original.getMaxDamage() - input1original.getItemDamage();
                    int health2 = input2.getMaxDamage() - input2.getItemDamage();
                    int repairAmount = health2 + input1.getMaxDamage() * 12 / 100;
                    int newHealth = health1 + repairAmount;
                    int damage1 = input1.getMaxDamage() - newHealth;
                    if (damage1 < 0) {
                        damage1 = 0;
                    }
                    if (damage1 < input1.getMetadata()) {
                        input1.setItemDamage(damage1);
                        enchantCost += 2;
                    }
                }
                Map<Enchantment, Integer> input2Enchantments = EnchantmentHelper.getEnchantments(input2);
                for (@Nullable Enchantment input2Enchantment : input2Enchantments.keySet()) {
                    if (input2Enchantment != null) {
                        int input1EnchantLevel = input1Enchantments.containsKey(input2Enchantment) ? input1Enchantments.get(input2Enchantment) : 0;
                        int input2EnchantLevel = input2Enchantments.get(input2Enchantment);
                        input2EnchantLevel = input1EnchantLevel == input2EnchantLevel ? input2EnchantLevel + 1 : Math.max(input2EnchantLevel, input1EnchantLevel);
                        boolean canApplyEnchants = input2Enchantment.canApply(input1original);
                        if (thePlayer.capabilities.isCreativeMode || input1original.getItem() == Items.ENCHANTED_BOOK) {
                            canApplyEnchants = true;
                        }
                        for (@Nullable Enchantment input1Enchantment : input1Enchantments.keySet()) {
                            // Null check to prevent crash
                            if (//Forge BugFix: Let Both enchantments veto being together
                            input1Enchantment != null && input1Enchantment != input2Enchantment && !(input2Enchantment.canApplyTogether(input1Enchantment) && input1Enchantment.canApplyTogether(input2Enchantment))) {
                                canApplyEnchants = false;
                                ++enchantCost;
                            }
                        }
                        if (canApplyEnchants) {
                            if (input2EnchantLevel > input2Enchantment.getMaxLevel()) {
                                input2EnchantLevel = input2Enchantment.getMaxLevel();
                            }
                            input1Enchantments.put(input2Enchantment, input2EnchantLevel);
                            int rarityMultiplier = 0;
                            switch(input2Enchantment.getRarity()) {
                                case COMMON:
                                    rarityMultiplier = 1;
                                    break;
                                case UNCOMMON:
                                    rarityMultiplier = 2;
                                    break;
                                case RARE:
                                    rarityMultiplier = 4;
                                    break;
                                case VERY_RARE:
                                    rarityMultiplier = 8;
                            }
                            if (isEnchantedBook) {
                                rarityMultiplier = Math.max(1, rarityMultiplier / 2);
                            }
                            enchantCost += rarityMultiplier * input2EnchantLevel;
                        }
                    }
                }
            }
        }
        if (isEnchantedBook && !input1.getItem().isBookEnchantable(input1, input2))
            input1 = InvTools.emptyStack();
        if (!InvTools.isEmpty(input1))
            if (StringUtils.isBlank(repairedItemName)) {
                if (input1original.hasDisplayName()) {
                    nameCost = 1;
                    enchantCost += nameCost;
                    input1.clearCustomName();
                }
            } else if (!repairedItemName.equals(input1original.getDisplayName())) {
                nameCost = 1;
                enchantCost += nameCost;
                input1.setStackDisplayName(repairedItemName);
            }
        this.maximumCost = baseCost + enchantCost;
        if (enchantCost <= 0) {
            input1 = InvTools.emptyStack();
        }
        // Railcraft changes max cost from 39 to 50
        if (nameCost == enchantCost && nameCost > 0 && maximumCost > MAX_COST) {
            this.maximumCost = MAX_COST;
        }
        // Here too
        if (maximumCost > MAX_COST && !thePlayer.capabilities.isCreativeMode) {
            input1 = InvTools.emptyStack();
        }
        if (!InvTools.isEmpty(input1)) {
            int repairCost = input1.getRepairCost();
            if (!InvTools.isEmpty(input2) && repairCost < input2.getRepairCost()) {
                repairCost = input2.getRepairCost();
            }
            if (nameCost != enchantCost || nameCost == 0) {
                repairCost = repairCost * 2 + 1;
            }
            input1.setRepairCost(repairCost);
            EnchantmentHelper.setEnchantments(input1Enchantments, input1);
        }
        outputSlot.setInventorySlotContents(0, input1);
        detectAndSendChanges();
    }
}
Also used : ItemStack(net.minecraft.item.ItemStack) Enchantment(net.minecraft.enchantment.Enchantment) Nullable(javax.annotation.Nullable)

Aggregations

Enchantment (net.minecraft.enchantment.Enchantment)12 ItemStack (net.minecraft.item.ItemStack)10 Iterator (java.util.Iterator)4 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 BetterStorageEnchantment (net.mcft.copy.betterstorage.api.BetterStorageEnchantment)2 EnchantmentData (net.minecraft.enchantment.EnchantmentData)2 ItemEnchantedBook (net.minecraft.item.ItemEnchantedBook)2 SideOnly (cpw.mods.fml.relauncher.SideOnly)1 HashMap (java.util.HashMap)1 Nullable (javax.annotation.Nullable)1 EnumEnchantmentType (net.minecraft.enchantment.EnumEnchantmentType)1 Item (net.minecraft.item.Item)1 MerchantRecipe (net.minecraft.village.MerchantRecipe)1 ContainerSearcher (pneumaticCraft.common.inventory.ContainerSearcher)1