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