    001    package net.minecraft.inventory;
    002
    003    import cpw.mods.fml.relauncher.Side;
    004    import cpw.mods.fml.relauncher.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                boolean var8 = false;
    096                int var19 = var3 + var1.getRepairCost() + (var6 == null ? 0 : var6.getRepairCost());
    097                this.field_82856_l = 0;
    098                int var9;
    099                int var10;
    100                int var11;
    101                int var13;
    102                int var14;
    103                Iterator var21;
    104                Enchantment var22;
    105
    106                if (var6 != null)
    107                {
    108                    var8 = var6.itemID == Item.field_92053_bW.shiftedIndex && Item.field_92053_bW.func_92056_g(var6).tagCount() > 0;
    109
    110                    if (var5.isItemStackDamageable() && Item.itemsList[var5.itemID].getIsRepairable(var1, var6))
    111                    {
    112                        var9 = Math.min(var5.getItemDamageForDisplay(), var5.getMaxDamage() / 4);
    113
    114                        if (var9 <= 0)
    115                        {
    116                            this.outputSlot.setInventorySlotContents(0, (ItemStack)null);
    117                            this.maximumCost = 0;
    118                            return;
    119                        }
    120
    121                        for (var10 = 0; var9 > 0 && var10 < var6.stackSize; ++var10)
    122                        {
    123                            var11 = var5.getItemDamageForDisplay() - var9;
    124                            var5.setItemDamage(var11);
    125                            var2 += Math.max(1, var9 / 100) + var7.size();
    126                            var9 = Math.min(var5.getItemDamageForDisplay(), var5.getMaxDamage() / 4);
    127                        }
    128
    129                        this.field_82856_l = var10;
    130                    }
    131                    else
    132                    {
    133                        if (!var8 && (var5.itemID != var6.itemID || !var5.isItemStackDamageable()))
    134                        {
    135                            this.outputSlot.setInventorySlotContents(0, (ItemStack)null);
    136                            this.maximumCost = 0;
    137                            return;
    138                        }
    139
    140                        if (var5.isItemStackDamageable() && !var8)
    141                        {
    142                            var9 = var1.getMaxDamage() - var1.getItemDamageForDisplay();
    143                            var10 = var6.getMaxDamage() - var6.getItemDamageForDisplay();
    144                            var11 = var10 + var5.getMaxDamage() * 12 / 100;
    145                            int var12 = var9 + var11;
    146                            var13 = var5.getMaxDamage() - var12;
    147
    148                            if (var13 < 0)
    149                            {
    150                                var13 = 0;
    151                            }
    152
    153                            if (var13 < var5.getItemDamage())
    154                            {
    155                                var5.setItemDamage(var13);
    156                                var2 += Math.max(1, var11 / 100);
    157                            }
    158                        }
    159
    160                        Map var20 = EnchantmentHelper.getEnchantments(var6);
    161                        var21 = var20.keySet().iterator();
    162
    163                        while (var21.hasNext())
    164                        {
    165                            var11 = ((Integer)var21.next()).intValue();
    166                            var22 = Enchantment.enchantmentsList[var11];
    167                            var13 = var7.containsKey(Integer.valueOf(var11)) ? ((Integer)var7.get(Integer.valueOf(var11))).intValue() : 0;
    168                            var14 = ((Integer)var20.get(Integer.valueOf(var11))).intValue();
    169                            int var10000;
    170
    171                            if (var13 == var14)
    172                            {
    173                                ++var14;
    174                                var10000 = var14;
    175                            }
    176                            else
    177                            {
    178                                var10000 = Math.max(var14, var13);
    179                            }
    180
    181                            var14 = var10000;
    182                            int var15 = var14 - var13;
    183                            boolean var16 = var22.func_92037_a(var1);
    184
    185                            if (this.thePlayer.capabilities.isCreativeMode)
    186                            {
    187                                var16 = true;
    188                            }
    189
    190                            Iterator var17 = var7.keySet().iterator();
    191
    192                            while (var17.hasNext())
    193                            {
    194                                int var18 = ((Integer)var17.next()).intValue();
    195
    196                                if (var18 != var11 && !var22.canApplyTogether(Enchantment.enchantmentsList[var18]))
    197                                {
    198                                    var16 = false;
    199                                    var2 += var15;
    200                                }
    201                            }
    202
    203                            if (var16)
    204                            {
    205                                if (var14 > var22.getMaxLevel())
    206                                {
    207                                    var14 = var22.getMaxLevel();
    208                                }
    209
    210                                var7.put(Integer.valueOf(var11), Integer.valueOf(var14));
    211                                int var23 = 0;
    212
    213                                switch (var22.getWeight())
    214                                {
    215                                    case 1:
    216                                        var23 = 8;
    217                                        break;
    218                                    case 2:
    219                                        var23 = 4;
    220                                    case 3:
    221                                    case 4:
    222                                    case 6:
    223                                    case 7:
    224                                    case 8:
    225                                    case 9:
    226                                    default:
    227                                        break;
    228                                    case 5:
    229                                        var23 = 2;
    230                                        break;
    231                                    case 10:
    232                                        var23 = 1;
    233                                }
    234
    235                                if (var8)
    236                                {
    237                                    var23 = Math.max(1, var23 / 2);
    238                                }
    239
    240                                var2 += var23 * var15;
    241                            }
    242                        }
    243                    }
    244                }
    245
    246                if (this.field_82857_m != null && !this.field_82857_m.equalsIgnoreCase(var1.getDisplayName()) && this.field_82857_m.length() > 0)
    247                {
    248                    var4 = var1.isItemStackDamageable() ? 7 : var1.stackSize * 5;
    249                    var2 += var4;
    250
    251                    if (var1.hasDisplayName())
    252                    {
    253                        var19 += var4 / 2;
    254                    }
    255
    256                    var5.setItemName(this.field_82857_m);
    257                }
    258
    259                var9 = 0;
    260
    261                for (var21 = var7.keySet().iterator(); var21.hasNext(); var19 += var9 + var13 * var14)
    262                {
    263                    var11 = ((Integer)var21.next()).intValue();
    264                    var22 = Enchantment.enchantmentsList[var11];
    265                    var13 = ((Integer)var7.get(Integer.valueOf(var11))).intValue();
    266                    var14 = 0;
    267                    ++var9;
    268
    269                    switch (var22.getWeight())
    270                    {
    271                        case 1:
    272                            var14 = 8;
    273                            break;
    274                        case 2:
    275                            var14 = 4;
    276                        case 3:
    277                        case 4:
    278                        case 6:
    279                        case 7:
    280                        case 8:
    281                        case 9:
    282                        default:
    283                            break;
    284                        case 5:
    285                            var14 = 2;
    286                            break;
    287                        case 10:
    288                            var14 = 1;
    289                    }
    290
    291                    if (var8)
    292                    {
    293                        var14 = Math.max(1, var14 / 2);
    294                    }
    295                }
    296
    297                if (var8)
    298                {
    299                    var19 = Math.max(1, var19 / 2);
    300                }
    301
    302                this.maximumCost = var19 + var2;
    303
    304                if (var2 <= 0)
    305                {
    306                    var5 = null;
    307                }
    308
    309                if (var4 == var2 && var4 > 0 && this.maximumCost >= 40)
    310                {
    311                    System.out.println("Naming an item only, cost too high; giving discount to cap cost to 39 levels");
    312                    this.maximumCost = 39;
    313                }
    314
    315                if (this.maximumCost >= 40 && !this.thePlayer.capabilities.isCreativeMode)
    316                {
    317                    var5 = null;
    318                }
    319
    320                if (var5 != null)
    321                {
    322                    var10 = var5.getRepairCost();
    323
    324                    if (var6 != null && var10 < var6.getRepairCost())
    325                    {
    326                        var10 = var6.getRepairCost();
    327                    }
    328
    329                    if (var5.hasDisplayName())
    330                    {
    331                        var10 -= 9;
    332                    }
    333
    334                    if (var10 < 0)
    335                    {
    336                        var10 = 0;
    337                    }
    338
    339                    var10 += 2;
    340                    var5.setRepairCost(var10);
    341                    EnchantmentHelper.setEnchantments(var7, var5);
    342                }
    343
    344                this.outputSlot.setInventorySlotContents(0, var5);
    345                this.updateCraftingResults();
    346            }
    347        }
    348
    349        public void addCraftingToCrafters(ICrafting par1ICrafting)
    350        {
    351            super.addCraftingToCrafters(par1ICrafting);
    352            par1ICrafting.sendProgressBarUpdate(this, 0, this.maximumCost);
    353        }
    354
    355        @SideOnly(Side.CLIENT)
    356        public void updateProgressBar(int par1, int par2)
    357        {
    358            if (par1 == 0)
    359            {
    360                this.maximumCost = par2;
    361            }
    362        }
    363
    364        /**
    365         * Callback for when the crafting gui is closed.
    366         */
    367        public void onCraftGuiClosed(EntityPlayer par1EntityPlayer)
    368        {
    369            super.onCraftGuiClosed(par1EntityPlayer);
    370
    371            if (!this.theWorld.isRemote)
    372            {
    373                for (int var2 = 0; var2 < this.inputSlots.getSizeInventory(); ++var2)
    374                {
    375                    ItemStack var3 = this.inputSlots.getStackInSlotOnClosing(var2);
    376
    377                    if (var3 != null)
    378                    {
    379                        par1EntityPlayer.dropPlayerItem(var3);
    380                    }
    381                }
    382            }
    383        }
    384
    385        public boolean canInteractWith(EntityPlayer par1EntityPlayer)
    386        {
    387            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;
    388        }
    389
    390        /**
    391         * Called when a player shift-clicks on a slot. You must override this or you will crash when someone does that.
    392         */
    393        public ItemStack transferStackInSlot(EntityPlayer par1EntityPlayer, int par2)
    394        {
    395            ItemStack var3 = null;
    396            Slot var4 = (Slot)this.inventorySlots.get(par2);
    397
    398            if (var4 != null && var4.getHasStack())
    399            {
    400                ItemStack var5 = var4.getStack();
    401                var3 = var5.copy();
    402
    403                if (par2 == 2)
    404                {
    405                    if (!this.mergeItemStack(var5, 3, 39, true))
    406                    {
    407                        return null;
    408                    }
    409
    410                    var4.onSlotChange(var5, var3);
    411                }
    412                else if (par2 != 0 && par2 != 1)
    413                {
    414                    if (par2 >= 3 && par2 < 39 && !this.mergeItemStack(var5, 0, 2, false))
    415                    {
    416                        return null;
    417                    }
    418                }
    419                else if (!this.mergeItemStack(var5, 3, 39, false))
    420                {
    421                    return null;
    422                }
    423
    424                if (var5.stackSize == 0)
    425                {
    426                    var4.putStack((ItemStack)null);
    427                }
    428                else
    429                {
    430                    var4.onSlotChanged();
    431                }
    432
    433                if (var5.stackSize == var3.stackSize)
    434                {
    435                    return null;
    436                }
    437
    438                var4.onPickupFromSlot(par1EntityPlayer, var5);
    439            }
    440
    441            return var3;
    442        }
    443
    444        public void func_82850_a(String par1Str)
    445        {
    446            this.field_82857_m = par1Str;
    447
    448            if (this.getSlot(2).getHasStack())
    449            {
    450                this.getSlot(2).getStack().setItemName(this.field_82857_m);
    451            }
    452
    453            this.func_82848_d();
    454        }
    455
    456        static IInventory func_82851_a(ContainerRepair par0ContainerRepair)
    457        {
    458            return par0ContainerRepair.inputSlots;
    459        }
    460
    461        static int func_82849_b(ContainerRepair par0ContainerRepair)
    462        {
    463            return par0ContainerRepair.field_82856_l;
    464        }
    465    }