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