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