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