Example usage for net.minecraftforge.common ForgeHooks onAnvilChange

List of usage examples for net.minecraftforge.common ForgeHooks onAnvilChange

Introduction

In this page you can find the example usage for net.minecraftforge.common ForgeHooks onAnvilChange.

Prototype

public static boolean onAnvilChange(RepairContainer container, @Nonnull ItemStack left,
            @Nonnull ItemStack right, IInventory outputSlot, String name, int baseCost) 

Source Link

Usage

From source file:mods.railcraft.common.gui.containers.ContainerAnvil.java

License:Open Source License

@Override
public void updateRepairOutput() {
    ItemStack inputA = inputSlots.getStackInSlot(0);
    this.maximumCost = 0;
    int i = 0;/* ww  w  .  ja va 2  s.  c  o m*/
    byte b0 = 0;
    int j = 0;

    if (inputA == null) {
        this.outputSlot.setInventorySlotContents(0, (ItemStack) null);
        this.maximumCost = 0;
    } else {
        ItemStack inputACopy = inputA.copy();
        ItemStack inputB = this.inputSlots.getStackInSlot(1);
        Map enchantments = EnchantmentHelper.getEnchantments(inputACopy);
        boolean isBook = false;
        int baseCost = b0 + inputA.getRepairCost() + (inputB == null ? 0 : inputB.getRepairCost());
        stackSizeToBeUsedInRepair = 0;
        int l;
        int i1;
        int j1;
        int k1;
        int l1;
        Iterator iterator;
        Enchantment enchantment;

        if (inputB != null) {
            if (!ForgeHooks.onAnvilChange(this, inputA, inputB, outputSlot, repairedItemName, baseCost))
                return;
            isBook = inputB.getItem() == Items.enchanted_book
                    && Items.enchanted_book.func_92110_g(inputB).tagCount() > 0;

            if (inputACopy.isItemStackDamageable() && inputACopy.getItem().getIsRepairable(inputA, inputB)) {
                l = Math.min(inputACopy.getItemDamageForDisplay(), inputACopy.getMaxDamage() / 4);

                if (l <= 0) {
                    this.outputSlot.setInventorySlotContents(0, (ItemStack) null);
                    this.maximumCost = 0;
                    return;
                }

                for (i1 = 0; l > 0 && i1 < inputB.stackSize; ++i1) {
                    j1 = inputACopy.getItemDamageForDisplay() - l;
                    inputACopy.setItemDamage(j1);
                    i += Math.max(1, l / 100) + enchantments.size();
                    l = Math.min(inputACopy.getItemDamageForDisplay(), inputACopy.getMaxDamage() / 4);
                }

                this.stackSizeToBeUsedInRepair = i1;
            } else {
                if (!isBook
                        && (inputACopy.getItem() != inputB.getItem() || !inputACopy.isItemStackDamageable())) {
                    this.outputSlot.setInventorySlotContents(0, (ItemStack) null);
                    this.maximumCost = 0;
                    return;
                }

                if (inputACopy.isItemStackDamageable() && !isBook) {
                    l = inputA.getMaxDamage() - inputA.getItemDamageForDisplay();
                    i1 = inputB.getMaxDamage() - inputB.getItemDamageForDisplay();
                    j1 = i1 + inputACopy.getMaxDamage() * 12 / 100;
                    int i2 = l + j1;
                    k1 = inputACopy.getMaxDamage() - i2;

                    if (k1 < 0)
                        k1 = 0;

                    if (k1 < inputACopy.getItemDamage()) {
                        inputACopy.setItemDamage(k1);
                        i += Math.max(1, j1 / 100);
                    }
                }

                Map map1 = EnchantmentHelper.getEnchantments(inputB);
                iterator = map1.keySet().iterator();

                while (iterator.hasNext()) {
                    j1 = ((Integer) iterator.next()).intValue();
                    enchantment = Enchantment.enchantmentsList[j1];
                    k1 = enchantments.containsKey(Integer.valueOf(j1))
                            ? ((Integer) enchantments.get(Integer.valueOf(j1))).intValue()
                            : 0;
                    l1 = ((Integer) map1.get(Integer.valueOf(j1))).intValue();
                    int j2;

                    if (k1 == l1) {
                        ++l1;
                        j2 = l1;
                    } else
                        j2 = Math.max(l1, k1);

                    l1 = j2;
                    int k2 = l1 - k1;
                    boolean isEnchantmentValid = enchantment.canApply(inputA);

                    if (player.capabilities.isCreativeMode || inputA.getItem() == Items.enchanted_book)
                        isEnchantmentValid = true;

                    Iterator iterator1 = enchantments.keySet().iterator();

                    while (iterator1.hasNext()) {
                        int l2 = ((Integer) iterator1.next()).intValue();

                        if (l2 != j1 && !enchantment.canApplyTogether(Enchantment.enchantmentsList[l2])) {
                            isEnchantmentValid = false;
                            i += k2;
                        }
                    }

                    if (isEnchantmentValid) {
                        if (l1 > enchantment.getMaxLevel())
                            l1 = enchantment.getMaxLevel();

                        enchantments.put(Integer.valueOf(j1), Integer.valueOf(l1));
                        int i3 = 0;

                        switch (enchantment.getWeight()) {
                        case 1:
                            i3 = 8;
                            break;
                        case 2:
                            i3 = 4;
                        case 3:
                        case 4:
                        case 6:
                        case 7:
                        case 8:
                        case 9:
                        default:
                            break;
                        case 5:
                            i3 = 2;
                            break;
                        case 10:
                            i3 = 1;
                        }

                        if (isBook)
                            i3 = Math.max(1, i3 / 2);

                        i += i3 * k2;
                    }
                }
            }
        }

        if (StringUtils.isBlank(this.repairedItemName)) {
            if (inputA.hasDisplayName()) {
                j = inputA.isItemStackDamageable() ? 7 : inputA.stackSize * 5;
                i += j;
                inputACopy.func_135074_t();
            }
        } else if (!this.repairedItemName.equals(inputA.getDisplayName())) {
            j = inputA.isItemStackDamageable() ? 7 : inputA.stackSize * 5;
            i += j;

            if (inputA.hasDisplayName())
                baseCost += j / 2;

            inputACopy.setStackDisplayName(this.repairedItemName);
        }

        l = 0;

        for (iterator = enchantments.keySet().iterator(); iterator.hasNext(); baseCost += l + k1 * l1) {
            j1 = ((Integer) iterator.next()).intValue();
            enchantment = Enchantment.enchantmentsList[j1];
            k1 = ((Integer) enchantments.get(Integer.valueOf(j1))).intValue();
            l1 = 0;
            ++l;

            switch (enchantment.getWeight()) {
            case 1:
                l1 = 8;
                break;
            case 2:
                l1 = 4;
            case 3:
            case 4:
            case 6:
            case 7:
            case 8:
            case 9:
            default:
                break;
            case 5:
                l1 = 2;
                break;
            case 10:
                l1 = 1;
            }

            if (isBook)
                l1 = Math.max(1, l1 / 2);
        }

        if (isBook)
            baseCost = Math.max(1, baseCost / 2);

        if (isBook && inputACopy != null && !inputACopy.getItem().isBookEnchantable(inputACopy, inputB))
            inputACopy = null;

        this.maximumCost = baseCost + i;

        if (i <= 0)
            inputACopy = null;

        // Railcraft changes max cost from 39 to 50
        if (j == i && j > 0 && this.maximumCost > 50)
            this.maximumCost = 50;

        // Here too
        if (this.maximumCost > 50 && !player.capabilities.isCreativeMode)
            inputACopy = null;

        if (inputACopy != null) {
            int repairCost = inputACopy.getRepairCost();

            if (inputB != null && repairCost < inputB.getRepairCost())
                repairCost = inputB.getRepairCost();

            if (inputACopy.hasDisplayName())
                repairCost -= 9;

            if (repairCost < 0)
                repairCost = 0;

            repairCost += 2;
            inputACopy.setRepairCost(repairCost);
            EnchantmentHelper.setEnchantments(enchantments, inputACopy);
        }

        this.outputSlot.setInventorySlotContents(0, inputACopy);
        this.detectAndSendChanges();
    }
}