InventoryPlayer .java

net.minecraft.entity.player.InventoryPlayer (class, 894 lines): members: addItemStackToInventory, consumeInventoryItem, decrementAnimations, copyInventory.

net.minecraft.entity.playerclass894 linescommon25.6 KB

Implements

API Surface (41)

ItemStack getCurrentItem()
int getHotbarSize()
int getFirstEmptyStack()
void setCurrentItem(Item itemIn, int metadataIn, boolean isMetaSpecific, boolean p_146030_4_)
void changeCurrentItem(int direction)
int clearMatchingItems(Item itemIn, int metadataIn, int removeCount, NBTTagCompound itemNBT)
void decrementAnimations()
boolean consumeInventoryItem(Item itemIn)
boolean hasItem(Item itemIn)
boolean addItemStackToInventory(final ItemStack itemStackIn)
String call()
ItemStack decrStackSize(int index, int count)
ItemStack removeStackFromSlot(int index)
void setInventorySlotContents(int index, ItemStack stack)
float getStrVsBlock(Block blockIn)
NBTTagList writeToNBT(NBTTagList nbtTagListIn)
void readFromNBT(NBTTagList nbtTagListIn)
int getSizeInventory()
ItemStack getStackInSlot(int index)
String getName()
boolean hasCustomName()
IChatComponent getDisplayName()
int getInventoryStackLimit()
boolean canHeldItemHarvest(Block blockIn)
ItemStack armorItemInSlot(int slotIn)
int getTotalArmorValue()
void damageArmor(float damage)
void dropAllItems()
void markDirty()
void setItemStack(ItemStack itemStackIn)
ItemStack getItemStack()
boolean isUseableByPlayer(EntityPlayer player)
boolean hasItemStack(ItemStack itemStackIn)
void openInventory(EntityPlayer player)
void closeInventory(EntityPlayer player)
boolean isItemValidForSlot(int index, ItemStack stack)
void copyInventory(InventoryPlayer playerInventory)
int getField(int id)
void setField(int id, int value)
int getFieldCount()
void clear()

Fields & Constants (6)

mainInventory
armorInventory
currentItem
player
itemStack
inventoryChanged

String literals (7)

"Adding item to inventory"
"Item being added"
"Item ID"
"Item data"
"Item name"
"Slot"
"container.inventory"

Full Source

InventoryPlayer.java894 lines · MCP 1.8.9 · verbatimOriginal ↗
1
package net.minecraft.entity.player;
2
3
import java.util.concurrent.Callable;
4
import net.minecraft.block.Block;
5
import net.minecraft.crash.CrashReport;
6
import net.minecraft.crash.CrashReportCategory;
7
import net.minecraft.inventory.IInventory;
8
import net.minecraft.item.Item;
9
import net.minecraft.item.ItemArmor;
10
import net.minecraft.item.ItemStack;
11
import net.minecraft.nbt.NBTTagCompound;
12
import net.minecraft.nbt.NBTTagList;
13
import net.minecraft.nbt.NBTUtil;
14
import net.minecraft.util.ChatComponentText;
15
import net.minecraft.util.ChatComponentTranslation;
16
import net.minecraft.util.IChatComponent;
17
import net.minecraft.util.ReportedException;
18
19
public class InventoryPlayer implements IInventory
20
{
21
    /**
22
     * An array of 36 item stacks indicating the main player inventory (including the visible bar).
23
     */
24
    public ItemStack[] mainInventory = new ItemStack[36];
25
26
    /** An array of 4 item stacks containing the currently worn armor pieces. */
27
    public ItemStack[] armorInventory = new ItemStack[4];
28
29
    /** The index of the currently held item (0-8). */
30
    public int currentItem;
31
32
    /** The player whose inventory this is. */
33
    public EntityPlayer player;
34
    private ItemStack itemStack;
35
36
    /**
37
     * Set true whenever the inventory changes. Nothing sets it false so you will have to write your own code to check
38
     * it and reset the value.
39
     */
40
    public boolean inventoryChanged;
41
42
    public InventoryPlayer(EntityPlayer playerIn)
43
    {
44
        this.player = playerIn;
45
    }
46
47
    /**
48
     * Returns the item stack currently held by the player.
49
     */
50
    public ItemStack getCurrentItem()
51
    {
52
        return this.currentItem < 9 && this.currentItem >= 0 ? this.mainInventory[this.currentItem] : null;
53
    }
54
55
    /**
56
     * Get the size of the player hotbar inventory
57
     */
58
    public static int getHotbarSize()
59
    {
60
        return 9;
61
    }
62
63
    private int getInventorySlotContainItem(Item itemIn)
64
    {
65
        for (int i = 0; i < this.mainInventory.length; ++i)
66
        {
67
            if (this.mainInventory[i] != null && this.mainInventory[i].getItem() == itemIn)
68
            {
69
                return i;
70
            }
71
        }
72
73
        return -1;
74
    }
75
76
    private int getInventorySlotContainItemAndDamage(Item itemIn, int metadataIn)
77
    {
78
        for (int i = 0; i < this.mainInventory.length; ++i)
79
        {
80
            if (this.mainInventory[i] != null && this.mainInventory[i].getItem() == itemIn && this.mainInventory[i].getMetadata() == metadataIn)
81
            {
82
                return i;
83
            }
84
        }
85
86
        return -1;
87
    }
88
89
    /**
90
     * stores an itemstack in the users inventory
91
     */
92
    private int storeItemStack(ItemStack itemStackIn)
93
    {
94
        for (int i = 0; i < this.mainInventory.length; ++i)
95
        {
96
            if (this.mainInventory[i] != null && this.mainInventory[i].getItem() == itemStackIn.getItem() && this.mainInventory[i].isStackable() && this.mainInventory[i].stackSize < this.mainInventory[i].getMaxStackSize() && this.mainInventory[i].stackSize < this.getInventoryStackLimit() && (!this.mainInventory[i].getHasSubtypes() || this.mainInventory[i].getMetadata() == itemStackIn.getMetadata()) && ItemStack.areItemStackTagsEqual(this.mainInventory[i], itemStackIn))
97
            {
98
                return i;
99
            }
100
        }
101
102
        return -1;
103
    }
104
105
    /**
106
     * Returns the first item stack that is empty.
107
     */
108
    public int getFirstEmptyStack()
109
    {
110
        for (int i = 0; i < this.mainInventory.length; ++i)
111
        {
112
            if (this.mainInventory[i] == null)
113
            {
114
                return i;
115
            }
116
        }
117
118
        return -1;
119
    }
120
121
    public void setCurrentItem(Item itemIn, int metadataIn, boolean isMetaSpecific, boolean p_146030_4_)
122
    {
123
        ItemStack itemstack = this.getCurrentItem();
124
        int i = isMetaSpecific ? this.getInventorySlotContainItemAndDamage(itemIn, metadataIn) : this.getInventorySlotContainItem(itemIn);
125
126
        if (i >= 0 && i < 9)
127
        {
128
            this.currentItem = i;
129
        }
130
        else if (p_146030_4_ && itemIn != null)
131
        {
132
            int j = this.getFirstEmptyStack();
133
134
            if (j >= 0 && j < 9)
135
            {
136
                this.currentItem = j;
137
            }
138
139
            if (itemstack == null || !itemstack.isItemEnchantable() || this.getInventorySlotContainItemAndDamage(itemstack.getItem(), itemstack.getItemDamage()) != this.currentItem)
140
            {
141
                int k = this.getInventorySlotContainItemAndDamage(itemIn, metadataIn);
142
                int l;
143
144
                if (k >= 0)
145
                {
146
                    l = this.mainInventory[k].stackSize;
147
                    this.mainInventory[k] = this.mainInventory[this.currentItem];
148
                }
149
                else
150
                {
151
                    l = 1;
152
                }
153
154
                this.mainInventory[this.currentItem] = new ItemStack(itemIn, l, metadataIn);
155
            }
156
        }
157
    }
158
159
    /**
160
     * Switch the current item to the next one or the previous one
161
     *  
162
     * @param direction Direction to switch (1, 0, -1). 1 (any > 0) to select item left of current (decreasing
163
     * currentItem index), -1 (any < 0) to select item right of current (increasing currentItem index). 0 has no effect.
164
     */
165
    public void changeCurrentItem(int direction)
166
    {
167
        if (direction > 0)
168
        {
169
            direction = 1;
170
        }
171
172
        if (direction < 0)
173
        {
174
            direction = -1;
175
        }
176
177
        for (this.currentItem -= direction; this.currentItem < 0; this.currentItem += 9)
178
        {
179
            ;
180
        }
181
182
        while (this.currentItem >= 9)
183
        {
184
            this.currentItem -= 9;
185
        }
186
    }
187
188
    /**
189
     * Removes matching items from the inventory.
190
     * @param itemIn The item to match, null ignores.
191
     * @param metadataIn The metadata to match, -1 ignores.
192
     * @param removeCount The number of items to remove. If less than 1, removes all matching items.
193
     * @param itemNBT The NBT data to match, null ignores.
194
     * @return The number of items removed from the inventory.
195
     */
196
    public int clearMatchingItems(Item itemIn, int metadataIn, int removeCount, NBTTagCompound itemNBT)
197
    {
198
        int i = 0;
199
200
        for (int j = 0; j < this.mainInventory.length; ++j)
201
        {
202
            ItemStack itemstack = this.mainInventory[j];
203
204
            if (itemstack != null && (itemIn == null || itemstack.getItem() == itemIn) && (metadataIn <= -1 || itemstack.getMetadata() == metadataIn) && (itemNBT == null || NBTUtil.func_181123_a(itemNBT, itemstack.getTagCompound(), true)))
205
            {
206
                int k = removeCount <= 0 ? itemstack.stackSize : Math.min(removeCount - i, itemstack.stackSize);
207
                i += k;
208
209
                if (removeCount != 0)
210
                {
211
                    this.mainInventory[j].stackSize -= k;
212
213
                    if (this.mainInventory[j].stackSize == 0)
214
                    {
215
                        this.mainInventory[j] = null;
216
                    }
217
218
                    if (removeCount > 0 && i >= removeCount)
219
                    {
220
                        return i;
221
                    }
222
                }
223
            }
224
        }
225
226
        for (int l = 0; l < this.armorInventory.length; ++l)
227
        {
228
            ItemStack itemstack1 = this.armorInventory[l];
229
230
            if (itemstack1 != null && (itemIn == null || itemstack1.getItem() == itemIn) && (metadataIn <= -1 || itemstack1.getMetadata() == metadataIn) && (itemNBT == null || NBTUtil.func_181123_a(itemNBT, itemstack1.getTagCompound(), false)))
231
            {
232
                int j1 = removeCount <= 0 ? itemstack1.stackSize : Math.min(removeCount - i, itemstack1.stackSize);
233
                i += j1;
234
235
                if (removeCount != 0)
236
                {
237
                    this.armorInventory[l].stackSize -= j1;
238
239
                    if (this.armorInventory[l].stackSize == 0)
240
                    {
241
                        this.armorInventory[l] = null;
242
                    }
243
244
                    if (removeCount > 0 && i >= removeCount)
245
                    {
246
                        return i;
247
                    }
248
                }
249
            }
250
        }
251
252
        if (this.itemStack != null)
253
        {
254
            if (itemIn != null && this.itemStack.getItem() != itemIn)
255
            {
256
                return i;
257
            }
258
259
            if (metadataIn > -1 && this.itemStack.getMetadata() != metadataIn)
260
            {
261
                return i;
262
            }
263
264
            if (itemNBT != null && !NBTUtil.func_181123_a(itemNBT, this.itemStack.getTagCompound(), false))
265
            {
266
                return i;
267
            }
268
269
            int i1 = removeCount <= 0 ? this.itemStack.stackSize : Math.min(removeCount - i, this.itemStack.stackSize);
270
            i += i1;
271
272
            if (removeCount != 0)
273
            {
274
                this.itemStack.stackSize -= i1;
275
276
                if (this.itemStack.stackSize == 0)
277
                {
278
                    this.itemStack = null;
279
                }
280
281
                if (removeCount > 0 && i >= removeCount)
282
                {
283
                    return i;
284
                }
285
            }
286
        }
287
288
        return i;
289
    }
290
291
    /**
292
     * This function stores as many items of an ItemStack as possible in a matching slot and returns the quantity of
293
     * left over items.
294
     */
295
    private int storePartialItemStack(ItemStack itemStackIn)
296
    {
297
        Item item = itemStackIn.getItem();
298
        int i = itemStackIn.stackSize;
299
        int j = this.storeItemStack(itemStackIn);
300
301
        if (j < 0)
302
        {
303
            j = this.getFirstEmptyStack();
304
        }
305
306
        if (j < 0)
307
        {
308
            return i;
309
        }
310
        else
311
        {
312
            if (this.mainInventory[j] == null)
313
            {
314
                this.mainInventory[j] = new ItemStack(item, 0, itemStackIn.getMetadata());
315
316
                if (itemStackIn.hasTagCompound())
317
                {
318
                    this.mainInventory[j].setTagCompound((NBTTagCompound)itemStackIn.getTagCompound().copy());
319
                }
320
            }
321
322
            int k = i;
323
324
            if (i > this.mainInventory[j].getMaxStackSize() - this.mainInventory[j].stackSize)
325
            {
326
                k = this.mainInventory[j].getMaxStackSize() - this.mainInventory[j].stackSize;
327
            }
328
329
            if (k > this.getInventoryStackLimit() - this.mainInventory[j].stackSize)
330
            {
331
                k = this.getInventoryStackLimit() - this.mainInventory[j].stackSize;
332
            }
333
334
            if (k == 0)
335
            {
336
                return i;
337
            }
338
            else
339
            {
340
                i = i - k;
341
                this.mainInventory[j].stackSize += k;
342
                this.mainInventory[j].animationsToGo = 5;
343
                return i;
344
            }
345
        }
346
    }
347
348
    /**
349
     * Decrement the number of animations remaining. Only called on client side. This is used to handle the animation of
350
     * receiving a block.
351
     */
352
    public void decrementAnimations()
353
    {
354
        for (int i = 0; i < this.mainInventory.length; ++i)
355
        {
356
            if (this.mainInventory[i] != null)
357
            {
358
                this.mainInventory[i].updateAnimation(this.player.worldObj, this.player, i, this.currentItem == i);
359
            }
360
        }
361
    }
362
363
    /**
364
     * removed one item of specified Item from inventory (if it is in a stack, the stack size will reduce with 1)
365
     */
366
    public boolean consumeInventoryItem(Item itemIn)
367
    {
368
        int i = this.getInventorySlotContainItem(itemIn);
369
370
        if (i < 0)
371
        {
372
            return false;
373
        }
374
        else
375
        {
376
            if (--this.mainInventory[i].stackSize <= 0)
377
            {
378
                this.mainInventory[i] = null;
379
            }
380
381
            return true;
382
        }
383
    }
384
385
    /**
386
     * Checks if a specified Item is inside the inventory
387
     */
388
    public boolean hasItem(Item itemIn)
389
    {
390
        int i = this.getInventorySlotContainItem(itemIn);
391
        return i >= 0;
392
    }
393
394
    /**
395
     * Adds the item stack to the inventory, returns false if it is impossible.
396
     */
397
    public boolean addItemStackToInventory(final ItemStack itemStackIn)
398
    {
399
        if (itemStackIn != null && itemStackIn.stackSize != 0 && itemStackIn.getItem() != null)
400
        {
401
            try
402
            {
403
                if (itemStackIn.isItemDamaged())
404
                {
405
                    int j = this.getFirstEmptyStack();
406
407
                    if (j >= 0)
408
                    {
409
                        this.mainInventory[j] = ItemStack.copyItemStack(itemStackIn);
410
                        this.mainInventory[j].animationsToGo = 5;
411
                        itemStackIn.stackSize = 0;
412
                        return true;
413
                    }
414
                    else if (this.player.capabilities.isCreativeMode)
415
                    {
416
                        itemStackIn.stackSize = 0;
417
                        return true;
418
                    }
419
                    else
420
                    {
421
                        return false;
422
                    }
423
                }
424
                else
425
                {
426
                    int i;
427
428
                    while (true)
429
                    {
430
                        i = itemStackIn.stackSize;
431
                        itemStackIn.stackSize = this.storePartialItemStack(itemStackIn);
432
433
                        if (itemStackIn.stackSize <= 0 || itemStackIn.stackSize >= i)
434
                        {
435
                            break;
436
                        }
437
                    }
438
439
                    if (itemStackIn.stackSize == i && this.player.capabilities.isCreativeMode)
440
                    {
441
                        itemStackIn.stackSize = 0;
442
                        return true;
443
                    }
444
                    else
445
                    {
446
                        return itemStackIn.stackSize < i;
447
                    }
448
                }
449
            }
450
            catch (Throwable throwable)
451
            {
452
                CrashReport crashreport = CrashReport.makeCrashReport(throwable, "Adding item to inventory");
453
                CrashReportCategory crashreportcategory = crashreport.makeCategory("Item being added");
454
                crashreportcategory.addCrashSection("Item ID", Integer.valueOf(Item.getIdFromItem(itemStackIn.getItem())));
455
                crashreportcategory.addCrashSection("Item data", Integer.valueOf(itemStackIn.getMetadata()));
456
                crashreportcategory.addCrashSectionCallable("Item name", new Callable<String>()
457
                {
458
                    public String call() throws Exception
459
                    {
460
                        return itemStackIn.getDisplayName();
461
                    }
462
                });
463
                throw new ReportedException(crashreport);
464
            }
465
        }
466
        else
467
        {
468
            return false;
469
        }
470
    }
471
472
    /**
473
     * Removes up to a specified number of items from an inventory slot and returns them in a new stack.
474
     */
475
    public ItemStack decrStackSize(int index, int count)
476
    {
477
        ItemStack[] aitemstack = this.mainInventory;
478
479
        if (index >= this.mainInventory.length)
480
        {
481
            aitemstack = this.armorInventory;
482
            index -= this.mainInventory.length;
483
        }
484
485
        if (aitemstack[index] != null)
486
        {
487
            if (aitemstack[index].stackSize <= count)
488
            {
489
                ItemStack itemstack1 = aitemstack[index];
490
                aitemstack[index] = null;
491
                return itemstack1;
492
            }
493
            else
494
            {
495
                ItemStack itemstack = aitemstack[index].splitStack(count);
496
497
                if (aitemstack[index].stackSize == 0)
498
                {
499
                    aitemstack[index] = null;
500
                }
501
502
                return itemstack;
503
            }
504
        }
505
        else
506
        {
507
            return null;
508
        }
509
    }
510
511
    /**
512
     * Removes a stack from the given slot and returns it.
513
     */
514
    public ItemStack removeStackFromSlot(int index)
515
    {
516
        ItemStack[] aitemstack = this.mainInventory;
517
518
        if (index >= this.mainInventory.length)
519
        {
520
            aitemstack = this.armorInventory;
521
            index -= this.mainInventory.length;
522
        }
523
524
        if (aitemstack[index] != null)
525
        {
526
            ItemStack itemstack = aitemstack[index];
527
            aitemstack[index] = null;
528
            return itemstack;
529
        }
530
        else
531
        {
532
            return null;
533
        }
534
    }
535
536
    /**
537
     * Sets the given item stack to the specified slot in the inventory (can be crafting or armor sections).
538
     */
539
    public void setInventorySlotContents(int index, ItemStack stack)
540
    {
541
        ItemStack[] aitemstack = this.mainInventory;
542
543
        if (index >= aitemstack.length)
544
        {
545
            index -= aitemstack.length;
546
            aitemstack = this.armorInventory;
547
        }
548
549
        aitemstack[index] = stack;
550
    }
551
552
    public float getStrVsBlock(Block blockIn)
553
    {
554
        float f = 1.0F;
555
556
        if (this.mainInventory[this.currentItem] != null)
557
        {
558
            f *= this.mainInventory[this.currentItem].getStrVsBlock(blockIn);
559
        }
560
561
        return f;
562
    }
563
564
    /**
565
     * Writes the inventory out as a list of compound tags. This is where the slot indices are used (+100 for armor, +80
566
     * for crafting).
567
     *  
568
     * @param nbtTagListIn List to append tags to
569
     */
570
    public NBTTagList writeToNBT(NBTTagList nbtTagListIn)
571
    {
572
        for (int i = 0; i < this.mainInventory.length; ++i)
573
        {
574
            if (this.mainInventory[i] != null)
575
            {
576
                NBTTagCompound nbttagcompound = new NBTTagCompound();
577
                nbttagcompound.setByte("Slot", (byte)i);
578
                this.mainInventory[i].writeToNBT(nbttagcompound);
579
                nbtTagListIn.appendTag(nbttagcompound);
580
            }
581
        }
582
583
        for (int j = 0; j < this.armorInventory.length; ++j)
584
        {
585
            if (this.armorInventory[j] != null)
586
            {
587
                NBTTagCompound nbttagcompound1 = new NBTTagCompound();
588
                nbttagcompound1.setByte("Slot", (byte)(j + 100));
589
                this.armorInventory[j].writeToNBT(nbttagcompound1);
590
                nbtTagListIn.appendTag(nbttagcompound1);
591
            }
592
        }
593
594
        return nbtTagListIn;
595
    }
596
597
    /**
598
     * Reads from the given tag list and fills the slots in the inventory with the correct items.
599
     *  
600
     * @param nbtTagListIn tagList to read from
601
     */
602
    public void readFromNBT(NBTTagList nbtTagListIn)
603
    {
604
        this.mainInventory = new ItemStack[36];
605
        this.armorInventory = new ItemStack[4];
606
607
        for (int i = 0; i < nbtTagListIn.tagCount(); ++i)
608
        {
609
            NBTTagCompound nbttagcompound = nbtTagListIn.getCompoundTagAt(i);
610
            int j = nbttagcompound.getByte("Slot") & 255;
611
            ItemStack itemstack = ItemStack.loadItemStackFromNBT(nbttagcompound);
612
613
            if (itemstack != null)
614
            {
615
                if (j >= 0 && j < this.mainInventory.length)
616
                {
617
                    this.mainInventory[j] = itemstack;
618
                }
619
620
                if (j >= 100 && j < this.armorInventory.length + 100)
621
                {
622
                    this.armorInventory[j - 100] = itemstack;
623
                }
624
            }
625
        }
626
    }
627
628
    /**
629
     * Returns the number of slots in the inventory.
630
     */
631
    public int getSizeInventory()
632
    {
633
        return this.mainInventory.length + 4;
634
    }
635
636
    /**
637
     * Returns the stack in the given slot.
638
     */
639
    public ItemStack getStackInSlot(int index)
640
    {
641
        ItemStack[] aitemstack = this.mainInventory;
642
643
        if (index >= aitemstack.length)
644
        {
645
            index -= aitemstack.length;
646
            aitemstack = this.armorInventory;
647
        }
648
649
        return aitemstack[index];
650
    }
651
652
    /**
653
     * Get the name of this object. For players this returns their username
654
     */
655
    public String getName()
656
    {
657
        return "container.inventory";
658
    }
659
660
    /**
661
     * Returns true if this thing is named
662
     */
663
    public boolean hasCustomName()
664
    {
665
        return false;
666
    }
667
668
    /**
669
     * Get the formatted ChatComponent that will be used for the sender's username in chat
670
     */
671
    public IChatComponent getDisplayName()
672
    {
673
        return (IChatComponent)(this.hasCustomName() ? new ChatComponentText(this.getName()) : new ChatComponentTranslation(this.getName(), new Object[0]));
674
    }
675
676
    /**
677
     * Returns the maximum stack size for a inventory slot. Seems to always be 64, possibly will be extended.
678
     */
679
    public int getInventoryStackLimit()
680
    {
681
        return 64;
682
    }
683
684
    public boolean canHeldItemHarvest(Block blockIn)
685
    {
686
        if (blockIn.getMaterial().isToolNotRequired())
687
        {
688
            return true;
689
        }
690
        else
691
        {
692
            ItemStack itemstack = this.getStackInSlot(this.currentItem);
693
            return itemstack != null ? itemstack.canHarvestBlock(blockIn) : false;
694
        }
695
    }
696
697
    /**
698
     * returns a player armor item (as itemstack) contained in specified armor slot.
699
     *  
700
     * @param slotIn the slot index requested
701
     */
702
    public ItemStack armorItemInSlot(int slotIn)
703
    {
704
        return this.armorInventory[slotIn];
705
    }
706
707
    /**
708
     * Based on the damage values and maximum damage values of each armor item, returns the current armor value.
709
     */
710
    public int getTotalArmorValue()
711
    {
712
        int i = 0;
713
714
        for (int j = 0; j < this.armorInventory.length; ++j)
715
        {
716
            if (this.armorInventory[j] != null && this.armorInventory[j].getItem() instanceof ItemArmor)
717
            {
718
                int k = ((ItemArmor)this.armorInventory[j].getItem()).damageReduceAmount;
719
                i += k;
720
            }
721
        }
722
723
        return i;
724
    }
725
726
    /**
727
     * Damages armor in each slot by the specified amount.
728
     */
729
    public void damageArmor(float damage)
730
    {
731
        damage = damage / 4.0F;
732
733
        if (damage < 1.0F)
734
        {
735
            damage = 1.0F;
736
        }
737
738
        for (int i = 0; i < this.armorInventory.length; ++i)
739
        {
740
            if (this.armorInventory[i] != null && this.armorInventory[i].getItem() instanceof ItemArmor)
741
            {
742
                this.armorInventory[i].damageItem((int)damage, this.player);
743
744
                if (this.armorInventory[i].stackSize == 0)
745
                {
746
                    this.armorInventory[i] = null;
747
                }
748
            }
749
        }
750
    }
751
752
    /**
753
     * Drop all armor and main inventory items.
754
     */
755
    public void dropAllItems()
756
    {
757
        for (int i = 0; i < this.mainInventory.length; ++i)
758
        {
759
            if (this.mainInventory[i] != null)
760
            {
761
                this.player.dropItem(this.mainInventory[i], true, false);
762
                this.mainInventory[i] = null;
763
            }
764
        }
765
766
        for (int j = 0; j < this.armorInventory.length; ++j)
767
        {
768
            if (this.armorInventory[j] != null)
769
            {
770
                this.player.dropItem(this.armorInventory[j], true, false);
771
                this.armorInventory[j] = null;
772
            }
773
        }
774
    }
775
776
    /**
777
     * For tile entities, ensures the chunk containing the tile entity is saved to disk later - the game won't think it
778
     * hasn't changed and skip it.
779
     */
780
    public void markDirty()
781
    {
782
        this.inventoryChanged = true;
783
    }
784
785
    /**
786
     * Set the stack helds by mouse, used in GUI/Container
787
     */
788
    public void setItemStack(ItemStack itemStackIn)
789
    {
790
        this.itemStack = itemStackIn;
791
    }
792
793
    /**
794
     * Stack helds by mouse, used in GUI and Containers
795
     */
796
    public ItemStack getItemStack()
797
    {
798
        return this.itemStack;
799
    }
800
801
    /**
802
     * Do not make give this method the name canInteractWith because it clashes with Container
803
     */
804
    public boolean isUseableByPlayer(EntityPlayer player)
805
    {
806
        return this.player.isDead ? false : player.getDistanceSqToEntity(this.player) <= 64.0D;
807
    }
808
809
    /**
810
     * Returns true if the specified ItemStack exists in the inventory.
811
     */
812
    public boolean hasItemStack(ItemStack itemStackIn)
813
    {
814
        for (int i = 0; i < this.armorInventory.length; ++i)
815
        {
816
            if (this.armorInventory[i] != null && this.armorInventory[i].isItemEqual(itemStackIn))
817
            {
818
                return true;
819
            }
820
        }
821
822
        for (int j = 0; j < this.mainInventory.length; ++j)
823
        {
824
            if (this.mainInventory[j] != null && this.mainInventory[j].isItemEqual(itemStackIn))
825
            {
826
                return true;
827
            }
828
        }
829
830
        return false;
831
    }
832
833
    public void openInventory(EntityPlayer player)
834
    {
835
    }
836
837
    public void closeInventory(EntityPlayer player)
838
    {
839
    }
840
841
    /**
842
     * Returns true if automation is allowed to insert the given stack (ignoring stack size) into the given slot.
843
     */
844
    public boolean isItemValidForSlot(int index, ItemStack stack)
845
    {
846
        return true;
847
    }
848
849
    /**
850
     * Copy the ItemStack contents from another InventoryPlayer instance
851
     */
852
    public void copyInventory(InventoryPlayer playerInventory)
853
    {
854
        for (int i = 0; i < this.mainInventory.length; ++i)
855
        {
856
            this.mainInventory[i] = ItemStack.copyItemStack(playerInventory.mainInventory[i]);
857
        }
858
859
        for (int j = 0; j < this.armorInventory.length; ++j)
860
        {
861
            this.armorInventory[j] = ItemStack.copyItemStack(playerInventory.armorInventory[j]);
862
        }
863
864
        this.currentItem = playerInventory.currentItem;
865
    }
866
867
    public int getField(int id)
868
    {
869
        return 0;
870
    }
871
872
    public void setField(int id, int value)
873
    {
874
    }
875
876
    public int getFieldCount()
877
    {
878
        return 0;
879
    }
880
881
    public void clear()
882
    {
883
        for (int i = 0; i < this.mainInventory.length; ++i)
884
        {
885
            this.mainInventory[i] = null;
886
        }
887
888
        for (int j = 0; j < this.armorInventory.length; ++j)
889
        {
890
            this.armorInventory[j] = null;
891
        }
892
    }
893
}
894

Deobfuscated MCP 1.8.9 source of InventoryPlayer from Marcelektro/MavenMCP-1.8.9 (net/minecraft/entity/player/InventoryPlayer.java), stored verbatim.