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