Search in sources :

Example 46 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 (isEmpty(input1original)) {
        outputSlot.setInventorySlotContents(0, emptyStack());
        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 && ItemEnchantedBook.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 < sizeOf(input2); ++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.getOrDefault(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.isCompatibleWith(input1Enchantment) && input1Enchantment.isCompatibleWith(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(org.jetbrains.annotations.Nullable)

Example 47 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 48 with Enchantment

use of net.minecraft.enchantment.Enchantment in project BetterStorage by copygirl.

the class EnchantmentBetterStorage method initialize.

public static void initialize() {
    Map<String, EnumEnchantmentType> types = BetterStorageEnchantment.enchantmentTypes;
    Map<String, Enchantment> enchs = BetterStorageEnchantment.enchantments;
    // Add key enchantments
    if (BetterStorageItems.key != null) {
        EnumEnchantmentType key = EnumHelper.addEnchantmentType("key");
        EnchantmentBetterStorage unlocking = conditialNew("unlocking", key, GlobalConfig.enchUnlockingId, 8, 5, 5, 10, 30, 0);
        EnchantmentBetterStorage lockpicking = conditialNew("lockpicking", key, GlobalConfig.enchLockpickingId, 6, 5, 5, 8, 30, 0);
        EnchantmentBetterStorage morphing = conditialNew("morphing", key, GlobalConfig.enchMorphingId, 1, 5, 10, 12, 30, 0);
        if (lockpicking != null)
            lockpicking.setIncompatible(morphing);
        if (morphing != null)
            morphing.setIncompatible(lockpicking);
        types.put("key", key);
        enchs.put("unlocking", unlocking);
        enchs.put("lockpicking", lockpicking);
        enchs.put("morphing", morphing);
    }
    // Add lock enchantments
    if (BetterStorageItems.lock != null) {
        EnumEnchantmentType lock = EnumHelper.addEnchantmentType("lock");
        EnchantmentBetterStorage persistance = conditialNew("persistance", lock, GlobalConfig.enchPersistanceId, 20, 5, 1, 8, 30, 0);
        EnchantmentBetterStorage security = conditialNew("security", lock, GlobalConfig.enchSecurityId, 16, 5, 1, 10, 30, 0);
        EnchantmentBetterStorage shock = conditialNew("shock", lock, GlobalConfig.enchShockId, 5, 3, 5, 15, 30, 0);
        EnchantmentBetterStorage trigger = conditialNew("trigger", lock, GlobalConfig.enchTriggerId, 10, 1, 15, 0, 30, 0);
        types.put("lock", lock);
        enchs.put("persistance", persistance);
        enchs.put("security", security);
        enchs.put("shock", shock);
        enchs.put("trigger", trigger);
    }
}
Also used : Enchantment(net.minecraft.enchantment.Enchantment) BetterStorageEnchantment(net.mcft.copy.betterstorage.api.BetterStorageEnchantment) EnumEnchantmentType(net.minecraft.enchantment.EnumEnchantmentType)

Example 49 with Enchantment

use of net.minecraft.enchantment.Enchantment in project BetterStorage by copygirl.

the class ItemMasterKey method getSubItems.

@Override
@SideOnly(Side.CLIENT)
public void getSubItems(Item item, CreativeTabs creativeTab, List list) {
    ItemStack stack = new ItemStack(item, 1, 0);
    Enchantment ench = BetterStorageEnchantment.get("unlocking");
    if (ench != null)
        stack.addEnchantment(ench, 10);
    list.add(stack);
}
Also used : ItemStack(net.minecraft.item.ItemStack) Enchantment(net.minecraft.enchantment.Enchantment) BetterStorageEnchantment(net.mcft.copy.betterstorage.api.BetterStorageEnchantment) SideOnly(cpw.mods.fml.relauncher.SideOnly)

Aggregations

Enchantment (net.minecraft.enchantment.Enchantment)49 ItemStack (net.minecraft.item.ItemStack)28 ArrayList (java.util.ArrayList)8 Item (net.minecraft.item.Item)7 Map (java.util.Map)6 EnchantmentData (net.minecraft.enchantment.EnchantmentData)6 NBTTagList (net.minecraft.nbt.NBTTagList)6 HashMap (java.util.HashMap)5 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)5 Iterator (java.util.Iterator)4 ModelResourceLocation (net.minecraft.client.renderer.block.model.ModelResourceLocation)3 EntityLivingBase (net.minecraft.entity.EntityLivingBase)3 ResourceLocation (net.minecraft.util.ResourceLocation)3 ImmutableList (com.google.common.collect.ImmutableList)2 Iterables (com.google.common.collect.Iterables)2 LinkedHashMultimap (com.google.common.collect.LinkedHashMultimap)2 Lists (com.google.common.collect.Lists)2 Multimap (com.google.common.collect.Multimap)2 Collection (java.util.Collection)2 List (java.util.List)2