    001package net.minecraft.inventory;
    002
    003import cpw.mods.fml.relauncher.Side;
    004import cpw.mods.fml.relauncher.SideOnly;
    005import java.util.Iterator;
    006import java.util.Map;
    007import net.minecraft.block.Block;
    008import net.minecraft.enchantment.Enchantment;
    009import net.minecraft.enchantment.EnchantmentHelper;
    010import net.minecraft.entity.player.EntityPlayer;
    011import net.minecraft.entity.player.InventoryPlayer;
    012import net.minecraft.item.Item;
    013import net.minecraft.item.ItemStack;
    014import net.minecraft.world.World;
    015
    016public class ContainerRepair extends Container
    017{
    018    /** Here comes out item you merged and/or renamed. */
    019    private IInventory outputSlot = new InventoryCraftResult();
    020
    021    /**
    022     * The 2slots where you put your items in that you want to merge and/or rename.
    023     */
    024    private IInventory inputSlots = new InventoryRepair(this, "Repair", 2);
    025    private World theWorld;
    026    private int field_82861_i;
    027    private int field_82858_j;
    028    private int field_82859_k;
    029
    030    /** The maximum cost of repairing/renaming in the anvil. */
    031    public int maximumCost = 0;
    032
    033    /** determined by damage of input item and stackSize of repair materials */
    034    private int stackSizeToBeUsedInRepair = 0;
    035    private String repairedItemName;
    036
    037    /** The player that has this container open. */
    038    private final EntityPlayer thePlayer;
    039
    040    public ContainerRepair(InventoryPlayer par1InventoryPlayer, World par2World, int par3, int par4, int par5, EntityPlayer par6EntityPlayer)
    041    {
    042        this.theWorld = par2World;
    043        this.field_82861_i = par3;
    044        this.field_82858_j = par4;
    045        this.field_82859_k = par5;
    046        this.thePlayer = par6EntityPlayer;
    047        this.addSlotToContainer(new Slot(this.inputSlots, 0, 27, 47));
    048        this.addSlotToContainer(new Slot(this.inputSlots, 1, 76, 47));
    049        this.addSlotToContainer(new SlotRepair(this, this.outputSlot, 2, 134, 47, par2World, par3, par4, par5));
    050        int var7;
    051
    052        for (var7 = 0; var7 < 3; ++var7)
    053        {
    054            for (int var8 = 0; var8 < 9; ++var8)
    055            {
    056                this.addSlotToContainer(new Slot(par1InventoryPlayer, var8 + var7 * 9 + 9, 8 + var8 * 18, 84 + var7 * 18));
    057            }
    058        }
    059
    060        for (var7 = 0; var7 < 9; ++var7)
    061        {
    062            this.addSlotToContainer(new Slot(par1InventoryPlayer, var7, 8 + var7 * 18, 142));
    063        }
    064    }
    065
    066    /**
    067     * Callback for when the crafting matrix is changed.
    068     */
    069    public void onCraftMatrixChanged(IInventory par1IInventory)
    070    {
    071        super.onCraftMatrixChanged(par1IInventory);
    072
    073        if (par1IInventory == this.inputSlots)
    074        {
    075            this.updateRepairOutput();
    076        }
    077    }
    078
    079    /**
    080     * called when the Anvil Input Slot changes, calculates the new result and puts it in the output slot
    081     */
    082    public void updateRepairOutput()
    083    {
    084        ItemStack var1 = this.inputSlots.getStackInSlot(0);
    085        this.maximumCost = 0;
    086        int var2 = 0;
    087        byte var3 = 0;
    088        int var4 = 0;
    089
    090        if (var1 == null)
    091        {
    092            this.outputSlot.setInventorySlotContents(0, (ItemStack)null);
    093            this.maximumCost = 0;
    094        }
    095        else
    096        {
    097            ItemStack var5 = var1.copy();
    098            ItemStack var6 = this.inputSlots.getStackInSlot(1);
    099            Map var7 = EnchantmentHelper.getEnchantments(var5);
    100            boolean var8 = false;
    101            int var19 = var3 + var1.getRepairCost() + (var6 == null ? 0 : var6.getRepairCost());
    102            this.stackSizeToBeUsedInRepair = 0;
    103            int var9;
    104            int var10;
    105            int var11;
    106            int var13;
    107            int var14;
    108            Iterator var21;
    109            Enchantment var22;
    110
    111            if (var6 != null)
    112            {
    113                var8 = var6.itemID == Item.enchantedBook.itemID && Item.enchantedBook.func_92110_g(var6).tagCount() > 0;
    114
    115                if (var5.isItemStackDamageable() && Item.itemsList[var5.itemID].getIsRepairable(var1, var6))
    116                {
    117                    var9 = Math.min(var5.getItemDamageForDisplay(), var5.getMaxDamage() / 4);
    118
    119                    if (var9 <= 0)
    120                    {
    121                        this.outputSlot.setInventorySlotContents(0, (ItemStack)null);
    122                        this.maximumCost = 0;
    123                        return;
    124                    }
    125
    126                    for (var10 = 0; var9 > 0 && var10 < var6.stackSize; ++var10)
    127                    {
    128                        var11 = var5.getItemDamageForDisplay() - var9;
    129                        var5.setItemDamage(var11);
    130                        var2 += Math.max(1, var9 / 100) + var7.size();
    131                        var9 = Math.min(var5.getItemDamageForDisplay(), var5.getMaxDamage() / 4);
    132                    }
    133
    134                    this.stackSizeToBeUsedInRepair = var10;
    135                }
    136                else
    137                {
    138                    if (!var8 && (var5.itemID != var6.itemID || !var5.isItemStackDamageable()))
    139                    {
    140                        this.outputSlot.setInventorySlotContents(0, (ItemStack)null);
    141                        this.maximumCost = 0;
    142                        return;
    143                    }
    144
    145                    if (var5.isItemStackDamageable() && !var8)
    146                    {
    147                        var9 = var1.getMaxDamage() - var1.getItemDamageForDisplay();
    148                        var10 = var6.getMaxDamage() - var6.getItemDamageForDisplay();
    149                        var11 = var10 + var5.getMaxDamage() * 12 / 100;
    150                        int var12 = var9 + var11;
    151                        var13 = var5.getMaxDamage() - var12;
    152
    153                        if (var13 < 0)
    154                        {
    155                            var13 = 0;
    156                        }
    157
    158                        if (var13 < var5.getItemDamage())
    159                        {
    160                            var5.setItemDamage(var13);
    161                            var2 += Math.max(1, var11 / 100);
    162                        }
    163                    }
    164
    165                    Map var20 = EnchantmentHelper.getEnchantments(var6);
    166                    var21 = var20.keySet().iterator();
    167
    168                    while (var21.hasNext())
    169                    {
    170                        var11 = ((Integer)var21.next()).intValue();
    171                        var22 = Enchantment.enchantmentsList[var11];
    172                        var13 = var7.containsKey(Integer.valueOf(var11)) ? ((Integer)var7.get(Integer.valueOf(var11))).intValue() : 0;
    173                        var14 = ((Integer)var20.get(Integer.valueOf(var11))).intValue();
    174                        int var10000;
    175
    176                        if (var13 == var14)
    177                        {
    178                            ++var14;
    179                            var10000 = var14;
    180                        }
    181                        else
    182                        {
    183                            var10000 = Math.max(var14, var13);
    184                        }
    185
    186                        var14 = var10000;
    187                        int var15 = var14 - var13;
    188                        boolean var16 = var22.func_92089_a(var1);
    189
    190                        if (this.thePlayer.capabilities.isCreativeMode)
    191                        {
    192                            var16 = true;
    193                        }
    194
    195                        Iterator var17 = var7.keySet().iterator();
    196
    197                        while (var17.hasNext())
    198                        {
    199                            int var18 = ((Integer)var17.next()).intValue();
    200
    201                            if (var18 != var11 && !var22.canApplyTogether(Enchantment.enchantmentsList[var18]))
    202                            {
    203                                var16 = false;
    204                                var2 += var15;
    205                            }
    206                        }
    207
    208                        if (var16)
    209                        {
    210                            if (var14 > var22.getMaxLevel())
    211                            {
    212                                var14 = var22.getMaxLevel();
    213                            }
    214
    215                            var7.put(Integer.valueOf(var11), Integer.valueOf(var14));
    216                            int var23 = 0;
    217
    218                            switch (var22.getWeight())
    219                            {
    220                                case 1:
    221                                    var23 = 8;
    222                                    break;
    223                                case 2:
    224                                    var23 = 4;
    225                                case 3:
    226                                case 4:
    227                                case 6:
    228                                case 7:
    229                                case 8:
    230                                case 9:
    231                                default:
    232                                    break;
    233                                case 5:
    234                                    var23 = 2;
    235                                    break;
    236                                case 10:
    237                                    var23 = 1;
    238                            }
    239
    240                            if (var8)
    241                            {
    242                                var23 = Math.max(1, var23 / 2);
    243                            }
    244
    245                            var2 += var23 * var15;
    246                        }
    247                    }
    248                }
    249            }
    250
    251            if (this.repairedItemName != null && !this.repairedItemName.equalsIgnoreCase(var1.getDisplayName()) && this.repairedItemName.length() > 0)
    252            {
    253                var4 = var1.isItemStackDamageable() ? 7 : var1.stackSize * 5;
    254                var2 += var4;
    255
    256                if (var1.hasDisplayName())
    257                {
    258                    var19 += var4 / 2;
    259                }
    260
    261                var5.setItemName(this.repairedItemName);
    262            }
    263
    264            var9 = 0;
    265
    266            for (var21 = var7.keySet().iterator(); var21.hasNext(); var19 += var9 + var13 * var14)
    267            {
    268                var11 = ((Integer)var21.next()).intValue();
    269                var22 = Enchantment.enchantmentsList[var11];
    270                var13 = ((Integer)var7.get(Integer.valueOf(var11))).intValue();
    271                var14 = 0;
    272                ++var9;
    273
    274                switch (var22.getWeight())
    275                {
    276                    case 1:
    277                        var14 = 8;
    278                        break;
    279                    case 2:
    280                        var14 = 4;
    281                    case 3:
    282                    case 4:
    283                    case 6:
    284                    case 7:
    285                    case 8:
    286                    case 9:
    287                    default:
    288                        break;
    289                    case 5:
    290                        var14 = 2;
    291                        break;
    292                    case 10:
    293                        var14 = 1;
    294                }
    295
    296                if (var8)
    297                {
    298                    var14 = Math.max(1, var14 / 2);
    299                }
    300            }
    301
    302            if (var8)
    303            {
    304                var19 = Math.max(1, var19 / 2);
    305            }
    306
    307            this.maximumCost = var19 + var2;
    308
    309            if (var2 <= 0)
    310            {
    311                var5 = null;
    312            }
    313
    314            if (var4 == var2 && var4 > 0 && this.maximumCost >= 40)
    315            {
    316                System.out.println("Naming an item only, cost too high; giving discount to cap cost to 39 levels");
    317                this.maximumCost = 39;
    318            }
    319
    320            if (this.maximumCost >= 40 && !this.thePlayer.capabilities.isCreativeMode)
    321            {
    322                var5 = null;
    323            }
    324
    325            if (var5 != null)
    326            {
    327                var10 = var5.getRepairCost();
    328
    329                if (var6 != null && var10 < var6.getRepairCost())
    330                {
    331                    var10 = var6.getRepairCost();
    332                }
    333
    334                if (var5.hasDisplayName())
    335                {
    336                    var10 -= 9;
    337                }
    338
    339                if (var10 < 0)
    340                {
    341                    var10 = 0;
    342                }
    343
    344                var10 += 2;
    345                var5.setRepairCost(var10);
    346                EnchantmentHelper.setEnchantments(var7, var5);
    347            }
    348
    349            this.outputSlot.setInventorySlotContents(0, var5);
    350            this.detectAndSendChanges();
    351        }
    352    }
    353
    354    public void addCraftingToCrafters(ICrafting par1ICrafting)
    355    {
    356        super.addCraftingToCrafters(par1ICrafting);
    357        par1ICrafting.sendProgressBarUpdate(this, 0, this.maximumCost);
    358    }
    359
    360    @SideOnly(Side.CLIENT)
    361    public void updateProgressBar(int par1, int par2)
    362    {
    363        if (par1 == 0)
    364        {
    365            this.maximumCost = par2;
    366        }
    367    }
    368
    369    /**
    370     * Callback for when the crafting gui is closed.
    371     */
    372    public void onCraftGuiClosed(EntityPlayer par1EntityPlayer)
    373    {
    374        super.onCraftGuiClosed(par1EntityPlayer);
    375
    376        if (!this.theWorld.isRemote)
    377        {
    378            for (int var2 = 0; var2 < this.inputSlots.getSizeInventory(); ++var2)
    379            {
    380                ItemStack var3 = this.inputSlots.getStackInSlotOnClosing(var2);
    381
    382                if (var3 != null)
    383                {
    384                    par1EntityPlayer.dropPlayerItem(var3);
    385                }
    386            }
    387        }
    388    }
    389
    390    public boolean canInteractWith(EntityPlayer par1EntityPlayer)
    391    {
    392        return this.theWorld.getBlockId(this.field_82861_i, this.field_82858_j, this.field_82859_k) != Block.anvil.blockID ? false : par1EntityPlayer.getDistanceSq((double)this.field_82861_i + 0.5D, (double)this.field_82858_j + 0.5D, (double)this.field_82859_k + 0.5D) <= 64.0D;
    393    }
    394
    395    /**
    396     * Called when a player shift-clicks on a slot. You must override this or you will crash when someone does that.
    397     */
    398    public ItemStack transferStackInSlot(EntityPlayer par1EntityPlayer, int par2)
    399    {
    400        ItemStack var3 = null;
    401        Slot var4 = (Slot)this.inventorySlots.get(par2);
    402
    403        if (var4 != null && var4.getHasStack())
    404        {
    405            ItemStack var5 = var4.getStack();
    406            var3 = var5.copy();
    407
    408            if (par2 == 2)
    409            {
    410                if (!this.mergeItemStack(var5, 3, 39, true))
    411                {
    412                    return null;
    413                }
    414
    415                var4.onSlotChange(var5, var3);
    416            }
    417            else if (par2 != 0 && par2 != 1)
    418            {
    419                if (par2 >= 3 && par2 < 39 && !this.mergeItemStack(var5, 0, 2, false))
    420                {
    421                    return null;
    422                }
    423            }
    424            else if (!this.mergeItemStack(var5, 3, 39, false))
    425            {
    426                return null;
    427            }
    428
    429            if (var5.stackSize == 0)
    430            {
    431                var4.putStack((ItemStack)null);
    432            }
    433            else
    434            {
    435                var4.onSlotChanged();
    436            }
    437
    438            if (var5.stackSize == var3.stackSize)
    439            {
    440                return null;
    441            }
    442
    443            var4.onPickupFromSlot(par1EntityPlayer, var5);
    444        }
    445
    446        return var3;
    447    }
    448
    449    /**
    450     * used by the Anvil GUI to update the Item Name being typed by the player
    451     */
    452    public void updateItemName(String par1Str)
    453    {
    454        this.repairedItemName = par1Str;
    455
    456        if (this.getSlot(2).getHasStack())
    457        {
    458            this.getSlot(2).getStack().setItemName(this.repairedItemName);
    459        }
    460
    461        this.updateRepairOutput();
    462    }
    463
    464    static IInventory getRepairInputInventory(ContainerRepair par0ContainerRepair)
    465    {
    466        return par0ContainerRepair.inputSlots;
    467    }
    468
    469    static int getStackSizeUsedInRepair(ContainerRepair par0ContainerRepair)
    470    {
    471        return par0ContainerRepair.stackSizeToBeUsedInRepair;
    472    }
    473}