ContainerRepair .java

net.minecraft.inventory.ContainerRepair (class): members: updateRepairOutput, repairedItemName, updateItemName, materialCost, selfPosition, maximumCost.

net.minecraft.inventoryclass507 linescommonextends Container17.0 KB

API Surface (12)

void markDirty()
boolean isItemValid(ItemStack stack)
boolean canTakeStack(EntityPlayer playerIn)
void onPickupFromSlot(EntityPlayer playerIn, ItemStack stack)
void onCraftMatrixChanged(IInventory inventoryIn)
void updateRepairOutput()
void onCraftGuiOpened(ICrafting listener)
void updateProgressBar(int id, int data)
void onContainerClosed(EntityPlayer playerIn)
boolean canInteractWith(EntityPlayer playerIn)
ItemStack transferStackInSlot(EntityPlayer playerIn, int index)
void updateItemName(String newName)

Fields & Constants (9)

logger
outputSlot
inputSlots
theWorld
selfPosition
maximumCost
materialCost
repairedItemName
thePlayer

String literals (1)

"Repair"

Full Source

ContainerRepair.java507 lines · MCP 1.8.9 · verbatimOriginal ↗
1
package net.minecraft.inventory;
2
3
import java.util.Iterator;
4
import java.util.Map;
5
import net.minecraft.block.BlockAnvil;
6
import net.minecraft.block.state.IBlockState;
7
import net.minecraft.enchantment.Enchantment;
8
import net.minecraft.enchantment.EnchantmentHelper;
9
import net.minecraft.entity.player.EntityPlayer;
10
import net.minecraft.entity.player.InventoryPlayer;
11
import net.minecraft.init.Blocks;
12
import net.minecraft.init.Items;
13
import net.minecraft.item.ItemStack;
14
import net.minecraft.util.BlockPos;
15
import net.minecraft.world.World;
16
import org.apache.commons.lang3.StringUtils;
17
import org.apache.logging.log4j.LogManager;
18
import org.apache.logging.log4j.Logger;
19
20
public class ContainerRepair extends Container
21
{
22
    private static final Logger logger = LogManager.getLogger();
23
24
    /** Here comes out item you merged and/or renamed. */
25
    private IInventory outputSlot;
26
27
    /**
28
     * The 2slots where you put your items in that you want to merge and/or rename.
29
     */
30
    private IInventory inputSlots;
31
    private World theWorld;
32
    private BlockPos selfPosition;
33
34
    /** The maximum cost of repairing/renaming in the anvil. */
35
    public int maximumCost;
36
37
    /** determined by damage of input item and stackSize of repair materials */
38
    private int materialCost;
39
    private String repairedItemName;
40
41
    /** The player that has this container open. */
42
    private final EntityPlayer thePlayer;
43
44
    public ContainerRepair(InventoryPlayer playerInventory, World worldIn, EntityPlayer player)
45
    {
46
        this(playerInventory, worldIn, BlockPos.ORIGIN, player);
47
    }
48
49
    public ContainerRepair(InventoryPlayer playerInventory, final World worldIn, final BlockPos blockPosIn, EntityPlayer player)
50
    {
51
        this.outputSlot = new InventoryCraftResult();
52
        this.inputSlots = new InventoryBasic("Repair", true, 2)
53
        {
54
            public void markDirty()
55
            {
56
                super.markDirty();
57
                ContainerRepair.this.onCraftMatrixChanged(this);
58
            }
59
        };
60
        this.selfPosition = blockPosIn;
61
        this.theWorld = worldIn;
62
        this.thePlayer = player;
63
        this.addSlotToContainer(new Slot(this.inputSlots, 0, 27, 47));
64
        this.addSlotToContainer(new Slot(this.inputSlots, 1, 76, 47));
65
        this.addSlotToContainer(new Slot(this.outputSlot, 2, 134, 47)
66
        {
67
            public boolean isItemValid(ItemStack stack)
68
            {
69
                return false;
70
            }
71
            public boolean canTakeStack(EntityPlayer playerIn)
72
            {
73
                return (playerIn.capabilities.isCreativeMode || playerIn.experienceLevel >= ContainerRepair.this.maximumCost) && ContainerRepair.this.maximumCost > 0 && this.getHasStack();
74
            }
75
            public void onPickupFromSlot(EntityPlayer playerIn, ItemStack stack)
76
            {
77
                if (!playerIn.capabilities.isCreativeMode)
78
                {
79
                    playerIn.addExperienceLevel(-ContainerRepair.this.maximumCost);
80
                }
81
82
                ContainerRepair.this.inputSlots.setInventorySlotContents(0, (ItemStack)null);
83
84
                if (ContainerRepair.this.materialCost > 0)
85
                {
86
                    ItemStack itemstack = ContainerRepair.this.inputSlots.getStackInSlot(1);
87
88
                    if (itemstack != null && itemstack.stackSize > ContainerRepair.this.materialCost)
89
                    {
90
                        itemstack.stackSize -= ContainerRepair.this.materialCost;
91
                        ContainerRepair.this.inputSlots.setInventorySlotContents(1, itemstack);
92
                    }
93
                    else
94
                    {
95
                        ContainerRepair.this.inputSlots.setInventorySlotContents(1, (ItemStack)null);
96
                    }
97
                }
98
                else
99
                {
100
                    ContainerRepair.this.inputSlots.setInventorySlotContents(1, (ItemStack)null);
101
                }
102
103
                ContainerRepair.this.maximumCost = 0;
104
                IBlockState iblockstate = worldIn.getBlockState(blockPosIn);
105
106
                if (!playerIn.capabilities.isCreativeMode && !worldIn.isRemote && iblockstate.getBlock() == Blocks.anvil && playerIn.getRNG().nextFloat() < 0.12F)
107
                {
108
                    int l = ((Integer)iblockstate.getValue(BlockAnvil.DAMAGE)).intValue();
109
                    ++l;
110
111
                    if (l > 2)
112
                    {
113
                        worldIn.setBlockToAir(blockPosIn);
114
                        worldIn.playAuxSFX(1020, blockPosIn, 0);
115
                    }
116
                    else
117
                    {
118
                        worldIn.setBlockState(blockPosIn, iblockstate.withProperty(BlockAnvil.DAMAGE, Integer.valueOf(l)), 2);
119
                        worldIn.playAuxSFX(1021, blockPosIn, 0);
120
                    }
121
                }
122
                else if (!worldIn.isRemote)
123
                {
124
                    worldIn.playAuxSFX(1021, blockPosIn, 0);
125
                }
126
            }
127
        });
128
129
        for (int i = 0; i < 3; ++i)
130
        {
131
            for (int j = 0; j < 9; ++j)
132
            {
133
                this.addSlotToContainer(new Slot(playerInventory, j + i * 9 + 9, 8 + j * 18, 84 + i * 18));
134
            }
135
        }
136
137
        for (int k = 0; k < 9; ++k)
138
        {
139
            this.addSlotToContainer(new Slot(playerInventory, k, 8 + k * 18, 142));
140
        }
141
    }
142
143
    /**
144
     * Callback for when the crafting matrix is changed.
145
     */
146
    public void onCraftMatrixChanged(IInventory inventoryIn)
147
    {
148
        super.onCraftMatrixChanged(inventoryIn);
149
150
        if (inventoryIn == this.inputSlots)
151
        {
152
            this.updateRepairOutput();
153
        }
154
    }
155
156
    /**
157
     * called when the Anvil Input Slot changes, calculates the new result and puts it in the output slot
158
     */
159
    public void updateRepairOutput()
160
    {
161
        int i = 0;
162
        int j = 1;
163
        int k = 1;
164
        int l = 1;
165
        int i1 = 2;
166
        int j1 = 1;
167
        int k1 = 1;
168
        ItemStack itemstack = this.inputSlots.getStackInSlot(0);
169
        this.maximumCost = 1;
170
        int l1 = 0;
171
        int i2 = 0;
172
        int j2 = 0;
173
174
        if (itemstack == null)
175
        {
176
            this.outputSlot.setInventorySlotContents(0, (ItemStack)null);
177
            this.maximumCost = 0;
178
        }
179
        else
180
        {
181
            ItemStack itemstack1 = itemstack.copy();
182
            ItemStack itemstack2 = this.inputSlots.getStackInSlot(1);
183
            Map<Integer, Integer> map = EnchantmentHelper.getEnchantments(itemstack1);
184
            boolean flag = false;
185
            i2 = i2 + itemstack.getRepairCost() + (itemstack2 == null ? 0 : itemstack2.getRepairCost());
186
            this.materialCost = 0;
187
188
            if (itemstack2 != null)
189
            {
190
                flag = itemstack2.getItem() == Items.enchanted_book && Items.enchanted_book.getEnchantments(itemstack2).tagCount() > 0;
191
192
                if (itemstack1.isItemStackDamageable() && itemstack1.getItem().getIsRepairable(itemstack, itemstack2))
193
                {
194
                    int j4 = Math.min(itemstack1.getItemDamage(), itemstack1.getMaxDamage() / 4);
195
196
                    if (j4 <= 0)
197
                    {
198
                        this.outputSlot.setInventorySlotContents(0, (ItemStack)null);
199
                        this.maximumCost = 0;
200
                        return;
201
                    }
202
203
                    int l4;
204
205
                    for (l4 = 0; j4 > 0 && l4 < itemstack2.stackSize; ++l4)
206
                    {
207
                        int j5 = itemstack1.getItemDamage() - j4;
208
                        itemstack1.setItemDamage(j5);
209
                        ++l1;
210
                        j4 = Math.min(itemstack1.getItemDamage(), itemstack1.getMaxDamage() / 4);
211
                    }
212
213
                    this.materialCost = l4;
214
                }
215
                else
216
                {
217
                    if (!flag && (itemstack1.getItem() != itemstack2.getItem() || !itemstack1.isItemStackDamageable()))
218
                    {
219
                        this.outputSlot.setInventorySlotContents(0, (ItemStack)null);
220
                        this.maximumCost = 0;
221
                        return;
222
                    }
223
224
                    if (itemstack1.isItemStackDamageable() && !flag)
225
                    {
226
                        int k2 = itemstack.getMaxDamage() - itemstack.getItemDamage();
227
                        int l2 = itemstack2.getMaxDamage() - itemstack2.getItemDamage();
228
                        int i3 = l2 + itemstack1.getMaxDamage() * 12 / 100;
229
                        int j3 = k2 + i3;
230
                        int k3 = itemstack1.getMaxDamage() - j3;
231
232
                        if (k3 < 0)
233
                        {
234
                            k3 = 0;
235
                        }
236
237
                        if (k3 < itemstack1.getMetadata())
238
                        {
239
                            itemstack1.setItemDamage(k3);
240
                            l1 += 2;
241
                        }
242
                    }
243
244
                    Map<Integer, Integer> map1 = EnchantmentHelper.getEnchantments(itemstack2);
245
                    Iterator iterator1 = map1.keySet().iterator();
246
247
                    while (iterator1.hasNext())
248
                    {
249
                        int i5 = ((Integer)iterator1.next()).intValue();
250
                        Enchantment enchantment = Enchantment.getEnchantmentById(i5);
251
252
                        if (enchantment != null)
253
                        {
254
                            int k5 = map.containsKey(Integer.valueOf(i5)) ? ((Integer)map.get(Integer.valueOf(i5))).intValue() : 0;
255
                            int l3 = ((Integer)map1.get(Integer.valueOf(i5))).intValue();
256
                            int i6;
257
258
                            if (k5 == l3)
259
                            {
260
                                ++l3;
261
                                i6 = l3;
262
                            }
263
                            else
264
                            {
265
                                i6 = Math.max(l3, k5);
266
                            }
267
268
                            l3 = i6;
269
                            boolean flag1 = enchantment.canApply(itemstack);
270
271
                            if (this.thePlayer.capabilities.isCreativeMode || itemstack.getItem() == Items.enchanted_book)
272
                            {
273
                                flag1 = true;
274
                            }
275
276
                            Iterator iterator = map.keySet().iterator();
277
278
                            while (iterator.hasNext())
279
                            {
280
                                int i4 = ((Integer)iterator.next()).intValue();
281
282
                                if (i4 != i5 && !enchantment.canApplyTogether(Enchantment.getEnchantmentById(i4)))
283
                                {
284
                                    flag1 = false;
285
                                    ++l1;
286
                                }
287
                            }
288
289
                            if (flag1)
290
                            {
291
                                if (l3 > enchantment.getMaxLevel())
292
                                {
293
                                    l3 = enchantment.getMaxLevel();
294
                                }
295
296
                                map.put(Integer.valueOf(i5), Integer.valueOf(l3));
297
                                int l5 = 0;
298
299
                                switch (enchantment.getWeight())
300
                                {
301
                                    case 1:
302
                                        l5 = 8;
303
                                        break;
304
305
                                    case 2:
306
                                        l5 = 4;
307
308
                                    case 3:
309
                                    case 4:
310
                                    case 6:
311
                                    case 7:
312
                                    case 8:
313
                                    case 9:
314
                                    default:
315
                                        break;
316
317
                                    case 5:
318
                                        l5 = 2;
319
                                        break;
320
321
                                    case 10:
322
                                        l5 = 1;
323
                                }
324
325
                                if (flag)
326
                                {
327
                                    l5 = Math.max(1, l5 / 2);
328
                                }
329
330
                                l1 += l5 * l3;
331
                            }
332
                        }
333
                    }
334
                }
335
            }
336
337
            if (StringUtils.isBlank(this.repairedItemName))
338
            {
339
                if (itemstack.hasDisplayName())
340
                {
341
                    j2 = 1;
342
                    l1 += j2;
343
                    itemstack1.clearCustomName();
344
                }
345
            }
346
            else if (!this.repairedItemName.equals(itemstack.getDisplayName()))
347
            {
348
                j2 = 1;
349
                l1 += j2;
350
                itemstack1.setStackDisplayName(this.repairedItemName);
351
            }
352
353
            this.maximumCost = i2 + l1;
354
355
            if (l1 <= 0)
356
            {
357
                itemstack1 = null;
358
            }
359
360
            if (j2 == l1 && j2 > 0 && this.maximumCost >= 40)
361
            {
362
                this.maximumCost = 39;
363
            }
364
365
            if (this.maximumCost >= 40 && !this.thePlayer.capabilities.isCreativeMode)
366
            {
367
                itemstack1 = null;
368
            }
369
370
            if (itemstack1 != null)
371
            {
372
                int k4 = itemstack1.getRepairCost();
373
374
                if (itemstack2 != null && k4 < itemstack2.getRepairCost())
375
                {
376
                    k4 = itemstack2.getRepairCost();
377
                }
378
379
                k4 = k4 * 2 + 1;
380
                itemstack1.setRepairCost(k4);
381
                EnchantmentHelper.setEnchantments(map, itemstack1);
382
            }
383
384
            this.outputSlot.setInventorySlotContents(0, itemstack1);
385
            this.detectAndSendChanges();
386
        }
387
    }
388
389
    public void onCraftGuiOpened(ICrafting listener)
390
    {
391
        super.onCraftGuiOpened(listener);
392
        listener.sendProgressBarUpdate(this, 0, this.maximumCost);
393
    }
394
395
    public void updateProgressBar(int id, int data)
396
    {
397
        if (id == 0)
398
        {
399
            this.maximumCost = data;
400
        }
401
    }
402
403
    /**
404
     * Called when the container is closed.
405
     */
406
    public void onContainerClosed(EntityPlayer playerIn)
407
    {
408
        super.onContainerClosed(playerIn);
409
410
        if (!this.theWorld.isRemote)
411
        {
412
            for (int i = 0; i < this.inputSlots.getSizeInventory(); ++i)
413
            {
414
                ItemStack itemstack = this.inputSlots.removeStackFromSlot(i);
415
416
                if (itemstack != null)
417
                {
418
                    playerIn.dropPlayerItemWithRandomChoice(itemstack, false);
419
                }
420
            }
421
        }
422
    }
423
424
    public boolean canInteractWith(EntityPlayer playerIn)
425
    {
426
        return this.theWorld.getBlockState(this.selfPosition).getBlock() != Blocks.anvil ? false : playerIn.getDistanceSq((double)this.selfPosition.getX() + 0.5D, (double)this.selfPosition.getY() + 0.5D, (double)this.selfPosition.getZ() + 0.5D) <= 64.0D;
427
    }
428
429
    /**
430
     * Take a stack from the specified inventory slot.
431
     */
432
    public ItemStack transferStackInSlot(EntityPlayer playerIn, int index)
433
    {
434
        ItemStack itemstack = null;
435
        Slot slot = (Slot)this.inventorySlots.get(index);
436
437
        if (slot != null && slot.getHasStack())
438
        {
439
            ItemStack itemstack1 = slot.getStack();
440
            itemstack = itemstack1.copy();
441
442
            if (index == 2)
443
            {
444
                if (!this.mergeItemStack(itemstack1, 3, 39, true))
445
                {
446
                    return null;
447
                }
448
449
                slot.onSlotChange(itemstack1, itemstack);
450
            }
451
            else if (index != 0 && index != 1)
452
            {
453
                if (index >= 3 && index < 39 && !this.mergeItemStack(itemstack1, 0, 2, false))
454
                {
455
                    return null;
456
                }
457
            }
458
            else if (!this.mergeItemStack(itemstack1, 3, 39, false))
459
            {
460
                return null;
461
            }
462
463
            if (itemstack1.stackSize == 0)
464
            {
465
                slot.putStack((ItemStack)null);
466
            }
467
            else
468
            {
469
                slot.onSlotChanged();
470
            }
471
472
            if (itemstack1.stackSize == itemstack.stackSize)
473
            {
474
                return null;
475
            }
476
477
            slot.onPickupFromSlot(playerIn, itemstack1);
478
        }
479
480
        return itemstack;
481
    }
482
483
    /**
484
     * used by the Anvil GUI to update the Item Name being typed by the player
485
     */
486
    public void updateItemName(String newName)
487
    {
488
        this.repairedItemName = newName;
489
490
        if (this.getSlot(2).getHasStack())
491
        {
492
            ItemStack itemstack = this.getSlot(2).getStack();
493
494
            if (StringUtils.isBlank(newName))
495
            {
496
                itemstack.clearCustomName();
497
            }
498
            else
499
            {
500
                itemstack.setStackDisplayName(this.repairedItemName);
501
            }
502
        }
503
504
        this.updateRepairOutput();
505
    }
506
}
507

Deobfuscated MCP 1.8.9 source of ContainerRepair from Marcelektro/MavenMCP-1.8.9 (net/minecraft/inventory/ContainerRepair.java), stored verbatim.