TileEntityChest .java

net.minecraft.tileentity.TileEntityChest (class): members: checkForAdjacentChests, adjacentChestChecked, adjacentChestXNeg, adjacentChestXPos, lidAngle.

net.minecraft.tileentityclass525 linescommonextends TileEntityLockable14.9 KB

Implements

API Surface (28)

int getSizeInventory()
ItemStack getStackInSlot(int index)
ItemStack decrStackSize(int index, int count)
ItemStack removeStackFromSlot(int index)
void setInventorySlotContents(int index, ItemStack stack)
String getName()
boolean hasCustomName()
void setCustomName(String name)
void readFromNBT(NBTTagCompound compound)
void writeToNBT(NBTTagCompound compound)
int getInventoryStackLimit()
boolean isUseableByPlayer(EntityPlayer player)
void updateContainingBlockInfo()
void checkForAdjacentChests()
TileEntityChest getAdjacentChest(EnumFacing side)
void update()
boolean receiveClientEvent(int id, int type)
void openInventory(EntityPlayer player)
void closeInventory(EntityPlayer player)
boolean isItemValidForSlot(int index, ItemStack stack)
void invalidate()
int getChestType()
String getGuiID()
Container createContainer(InventoryPlayer playerInventory, EntityPlayer playerIn)
int getField(int id)
void setField(int id, int value)
int getFieldCount()
void clear()

Fields & Constants (12)

chestContents
adjacentChestChecked
adjacentChestZNeg
adjacentChestXPos
adjacentChestXNeg
adjacentChestZPos
lidAngle
prevLidAngle
numPlayersUsing
ticksSinceSync
cachedChestType
customName

String literals (8)

"container.chest"
"Items"
"CustomName"
"Slot"
"incomplete-switch"
"random.chestopen"
"random.chestclosed"
"minecraft:chest"

Full Source

TileEntityChest.java525 lines · MCP 1.8.9 · verbatimOriginal ↗
1
package net.minecraft.tileentity;
2
3
import net.minecraft.block.Block;
4
import net.minecraft.block.BlockChest;
5
import net.minecraft.entity.player.EntityPlayer;
6
import net.minecraft.entity.player.InventoryPlayer;
7
import net.minecraft.inventory.Container;
8
import net.minecraft.inventory.ContainerChest;
9
import net.minecraft.inventory.IInventory;
10
import net.minecraft.inventory.InventoryLargeChest;
11
import net.minecraft.item.ItemStack;
12
import net.minecraft.nbt.NBTTagCompound;
13
import net.minecraft.nbt.NBTTagList;
14
import net.minecraft.util.AxisAlignedBB;
15
import net.minecraft.util.BlockPos;
16
import net.minecraft.util.EnumFacing;
17
import net.minecraft.util.ITickable;
18
19
public class TileEntityChest extends TileEntityLockable implements ITickable, IInventory
20
{
21
    private ItemStack[] chestContents = new ItemStack[27];
22
23
    /** Determines if the check for adjacent chests has taken place. */
24
    public boolean adjacentChestChecked;
25
26
    /** Contains the chest tile located adjacent to this one (if any) */
27
    public TileEntityChest adjacentChestZNeg;
28
29
    /** Contains the chest tile located adjacent to this one (if any) */
30
    public TileEntityChest adjacentChestXPos;
31
32
    /** Contains the chest tile located adjacent to this one (if any) */
33
    public TileEntityChest adjacentChestXNeg;
34
35
    /** Contains the chest tile located adjacent to this one (if any) */
36
    public TileEntityChest adjacentChestZPos;
37
38
    /** The current angle of the lid (between 0 and 1) */
39
    public float lidAngle;
40
41
    /** The angle of the lid last tick */
42
    public float prevLidAngle;
43
44
    /** The number of players currently using this chest */
45
    public int numPlayersUsing;
46
47
    /** Server sync counter (once per 20 ticks) */
48
    private int ticksSinceSync;
49
    private int cachedChestType;
50
    private String customName;
51
52
    public TileEntityChest()
53
    {
54
        this.cachedChestType = -1;
55
    }
56
57
    public TileEntityChest(int chestType)
58
    {
59
        this.cachedChestType = chestType;
60
    }
61
62
    /**
63
     * Returns the number of slots in the inventory.
64
     */
65
    public int getSizeInventory()
66
    {
67
        return 27;
68
    }
69
70
    /**
71
     * Returns the stack in the given slot.
72
     */
73
    public ItemStack getStackInSlot(int index)
74
    {
75
        return this.chestContents[index];
76
    }
77
78
    /**
79
     * Removes up to a specified number of items from an inventory slot and returns them in a new stack.
80
     */
81
    public ItemStack decrStackSize(int index, int count)
82
    {
83
        if (this.chestContents[index] != null)
84
        {
85
            if (this.chestContents[index].stackSize <= count)
86
            {
87
                ItemStack itemstack1 = this.chestContents[index];
88
                this.chestContents[index] = null;
89
                this.markDirty();
90
                return itemstack1;
91
            }
92
            else
93
            {
94
                ItemStack itemstack = this.chestContents[index].splitStack(count);
95
96
                if (this.chestContents[index].stackSize == 0)
97
                {
98
                    this.chestContents[index] = null;
99
                }
100
101
                this.markDirty();
102
                return itemstack;
103
            }
104
        }
105
        else
106
        {
107
            return null;
108
        }
109
    }
110
111
    /**
112
     * Removes a stack from the given slot and returns it.
113
     */
114
    public ItemStack removeStackFromSlot(int index)
115
    {
116
        if (this.chestContents[index] != null)
117
        {
118
            ItemStack itemstack = this.chestContents[index];
119
            this.chestContents[index] = null;
120
            return itemstack;
121
        }
122
        else
123
        {
124
            return null;
125
        }
126
    }
127
128
    /**
129
     * Sets the given item stack to the specified slot in the inventory (can be crafting or armor sections).
130
     */
131
    public void setInventorySlotContents(int index, ItemStack stack)
132
    {
133
        this.chestContents[index] = stack;
134
135
        if (stack != null && stack.stackSize > this.getInventoryStackLimit())
136
        {
137
            stack.stackSize = this.getInventoryStackLimit();
138
        }
139
140
        this.markDirty();
141
    }
142
143
    /**
144
     * Get the name of this object. For players this returns their username
145
     */
146
    public String getName()
147
    {
148
        return this.hasCustomName() ? this.customName : "container.chest";
149
    }
150
151
    /**
152
     * Returns true if this thing is named
153
     */
154
    public boolean hasCustomName()
155
    {
156
        return this.customName != null && this.customName.length() > 0;
157
    }
158
159
    public void setCustomName(String name)
160
    {
161
        this.customName = name;
162
    }
163
164
    public void readFromNBT(NBTTagCompound compound)
165
    {
166
        super.readFromNBT(compound);
167
        NBTTagList nbttaglist = compound.getTagList("Items", 10);
168
        this.chestContents = new ItemStack[this.getSizeInventory()];
169
170
        if (compound.hasKey("CustomName", 8))
171
        {
172
            this.customName = compound.getString("CustomName");
173
        }
174
175
        for (int i = 0; i < nbttaglist.tagCount(); ++i)
176
        {
177
            NBTTagCompound nbttagcompound = nbttaglist.getCompoundTagAt(i);
178
            int j = nbttagcompound.getByte("Slot") & 255;
179
180
            if (j >= 0 && j < this.chestContents.length)
181
            {
182
                this.chestContents[j] = ItemStack.loadItemStackFromNBT(nbttagcompound);
183
            }
184
        }
185
    }
186
187
    public void writeToNBT(NBTTagCompound compound)
188
    {
189
        super.writeToNBT(compound);
190
        NBTTagList nbttaglist = new NBTTagList();
191
192
        for (int i = 0; i < this.chestContents.length; ++i)
193
        {
194
            if (this.chestContents[i] != null)
195
            {
196
                NBTTagCompound nbttagcompound = new NBTTagCompound();
197
                nbttagcompound.setByte("Slot", (byte)i);
198
                this.chestContents[i].writeToNBT(nbttagcompound);
199
                nbttaglist.appendTag(nbttagcompound);
200
            }
201
        }
202
203
        compound.setTag("Items", nbttaglist);
204
205
        if (this.hasCustomName())
206
        {
207
            compound.setString("CustomName", this.customName);
208
        }
209
    }
210
211
    /**
212
     * Returns the maximum stack size for a inventory slot. Seems to always be 64, possibly will be extended.
213
     */
214
    public int getInventoryStackLimit()
215
    {
216
        return 64;
217
    }
218
219
    /**
220
     * Do not make give this method the name canInteractWith because it clashes with Container
221
     */
222
    public boolean isUseableByPlayer(EntityPlayer player)
223
    {
224
        return this.worldObj.getTileEntity(this.pos) != this ? false : player.getDistanceSq((double)this.pos.getX() + 0.5D, (double)this.pos.getY() + 0.5D, (double)this.pos.getZ() + 0.5D) <= 64.0D;
225
    }
226
227
    public void updateContainingBlockInfo()
228
    {
229
        super.updateContainingBlockInfo();
230
        this.adjacentChestChecked = false;
231
    }
232
233
    @SuppressWarnings("incomplete-switch")
234
    private void func_174910_a(TileEntityChest chestTe, EnumFacing side)
235
    {
236
        if (chestTe.isInvalid())
237
        {
238
            this.adjacentChestChecked = false;
239
        }
240
        else if (this.adjacentChestChecked)
241
        {
242
            switch (side)
243
            {
244
                case NORTH:
245
                    if (this.adjacentChestZNeg != chestTe)
246
                    {
247
                        this.adjacentChestChecked = false;
248
                    }
249
250
                    break;
251
252
                case SOUTH:
253
                    if (this.adjacentChestZPos != chestTe)
254
                    {
255
                        this.adjacentChestChecked = false;
256
                    }
257
258
                    break;
259
260
                case EAST:
261
                    if (this.adjacentChestXPos != chestTe)
262
                    {
263
                        this.adjacentChestChecked = false;
264
                    }
265
266
                    break;
267
268
                case WEST:
269
                    if (this.adjacentChestXNeg != chestTe)
270
                    {
271
                        this.adjacentChestChecked = false;
272
                    }
273
            }
274
        }
275
    }
276
277
    /**
278
     * Performs the check for adjacent chests to determine if this chest is double or not.
279
     */
280
    public void checkForAdjacentChests()
281
    {
282
        if (!this.adjacentChestChecked)
283
        {
284
            this.adjacentChestChecked = true;
285
            this.adjacentChestXNeg = this.getAdjacentChest(EnumFacing.WEST);
286
            this.adjacentChestXPos = this.getAdjacentChest(EnumFacing.EAST);
287
            this.adjacentChestZNeg = this.getAdjacentChest(EnumFacing.NORTH);
288
            this.adjacentChestZPos = this.getAdjacentChest(EnumFacing.SOUTH);
289
        }
290
    }
291
292
    protected TileEntityChest getAdjacentChest(EnumFacing side)
293
    {
294
        BlockPos blockpos = this.pos.offset(side);
295
296
        if (this.isChestAt(blockpos))
297
        {
298
            TileEntity tileentity = this.worldObj.getTileEntity(blockpos);
299
300
            if (tileentity instanceof TileEntityChest)
301
            {
302
                TileEntityChest tileentitychest = (TileEntityChest)tileentity;
303
                tileentitychest.func_174910_a(this, side.getOpposite());
304
                return tileentitychest;
305
            }
306
        }
307
308
        return null;
309
    }
310
311
    private boolean isChestAt(BlockPos posIn)
312
    {
313
        if (this.worldObj == null)
314
        {
315
            return false;
316
        }
317
        else
318
        {
319
            Block block = this.worldObj.getBlockState(posIn).getBlock();
320
            return block instanceof BlockChest && ((BlockChest)block).chestType == this.getChestType();
321
        }
322
    }
323
324
    /**
325
     * Like the old updateEntity(), except more generic.
326
     */
327
    public void update()
328
    {
329
        this.checkForAdjacentChests();
330
        int i = this.pos.getX();
331
        int j = this.pos.getY();
332
        int k = this.pos.getZ();
333
        ++this.ticksSinceSync;
334
335
        if (!this.worldObj.isRemote && this.numPlayersUsing != 0 && (this.ticksSinceSync + i + j + k) % 200 == 0)
336
        {
337
            this.numPlayersUsing = 0;
338
            float f = 5.0F;
339
340
            for (EntityPlayer entityplayer : this.worldObj.getEntitiesWithinAABB(EntityPlayer.class, new AxisAlignedBB((double)((float)i - f), (double)((float)j - f), (double)((float)k - f), (double)((float)(i + 1) + f), (double)((float)(j + 1) + f), (double)((float)(k + 1) + f))))
341
            {
342
                if (entityplayer.openContainer instanceof ContainerChest)
343
                {
344
                    IInventory iinventory = ((ContainerChest)entityplayer.openContainer).getLowerChestInventory();
345
346
                    if (iinventory == this || iinventory instanceof InventoryLargeChest && ((InventoryLargeChest)iinventory).isPartOfLargeChest(this))
347
                    {
348
                        ++this.numPlayersUsing;
349
                    }
350
                }
351
            }
352
        }
353
354
        this.prevLidAngle = this.lidAngle;
355
        float f1 = 0.1F;
356
357
        if (this.numPlayersUsing > 0 && this.lidAngle == 0.0F && this.adjacentChestZNeg == null && this.adjacentChestXNeg == null)
358
        {
359
            double d1 = (double)i + 0.5D;
360
            double d2 = (double)k + 0.5D;
361
362
            if (this.adjacentChestZPos != null)
363
            {
364
                d2 += 0.5D;
365
            }
366
367
            if (this.adjacentChestXPos != null)
368
            {
369
                d1 += 0.5D;
370
            }
371
372
            this.worldObj.playSoundEffect(d1, (double)j + 0.5D, d2, "random.chestopen", 0.5F, this.worldObj.rand.nextFloat() * 0.1F + 0.9F);
373
        }
374
375
        if (this.numPlayersUsing == 0 && this.lidAngle > 0.0F || this.numPlayersUsing > 0 && this.lidAngle < 1.0F)
376
        {
377
            float f2 = this.lidAngle;
378
379
            if (this.numPlayersUsing > 0)
380
            {
381
                this.lidAngle += f1;
382
            }
383
            else
384
            {
385
                this.lidAngle -= f1;
386
            }
387
388
            if (this.lidAngle > 1.0F)
389
            {
390
                this.lidAngle = 1.0F;
391
            }
392
393
            float f3 = 0.5F;
394
395
            if (this.lidAngle < f3 && f2 >= f3 && this.adjacentChestZNeg == null && this.adjacentChestXNeg == null)
396
            {
397
                double d3 = (double)i + 0.5D;
398
                double d0 = (double)k + 0.5D;
399
400
                if (this.adjacentChestZPos != null)
401
                {
402
                    d0 += 0.5D;
403
                }
404
405
                if (this.adjacentChestXPos != null)
406
                {
407
                    d3 += 0.5D;
408
                }
409
410
                this.worldObj.playSoundEffect(d3, (double)j + 0.5D, d0, "random.chestclosed", 0.5F, this.worldObj.rand.nextFloat() * 0.1F + 0.9F);
411
            }
412
413
            if (this.lidAngle < 0.0F)
414
            {
415
                this.lidAngle = 0.0F;
416
            }
417
        }
418
    }
419
420
    public boolean receiveClientEvent(int id, int type)
421
    {
422
        if (id == 1)
423
        {
424
            this.numPlayersUsing = type;
425
            return true;
426
        }
427
        else
428
        {
429
            return super.receiveClientEvent(id, type);
430
        }
431
    }
432
433
    public void openInventory(EntityPlayer player)
434
    {
435
        if (!player.isSpectator())
436
        {
437
            if (this.numPlayersUsing < 0)
438
            {
439
                this.numPlayersUsing = 0;
440
            }
441
442
            ++this.numPlayersUsing;
443
            this.worldObj.addBlockEvent(this.pos, this.getBlockType(), 1, this.numPlayersUsing);
444
            this.worldObj.notifyNeighborsOfStateChange(this.pos, this.getBlockType());
445
            this.worldObj.notifyNeighborsOfStateChange(this.pos.down(), this.getBlockType());
446
        }
447
    }
448
449
    public void closeInventory(EntityPlayer player)
450
    {
451
        if (!player.isSpectator() && this.getBlockType() instanceof BlockChest)
452
        {
453
            --this.numPlayersUsing;
454
            this.worldObj.addBlockEvent(this.pos, this.getBlockType(), 1, this.numPlayersUsing);
455
            this.worldObj.notifyNeighborsOfStateChange(this.pos, this.getBlockType());
456
            this.worldObj.notifyNeighborsOfStateChange(this.pos.down(), this.getBlockType());
457
        }
458
    }
459
460
    /**
461
     * Returns true if automation is allowed to insert the given stack (ignoring stack size) into the given slot.
462
     */
463
    public boolean isItemValidForSlot(int index, ItemStack stack)
464
    {
465
        return true;
466
    }
467
468
    /**
469
     * invalidates a tile entity
470
     */
471
    public void invalidate()
472
    {
473
        super.invalidate();
474
        this.updateContainingBlockInfo();
475
        this.checkForAdjacentChests();
476
    }
477
478
    public int getChestType()
479
    {
480
        if (this.cachedChestType == -1)
481
        {
482
            if (this.worldObj == null || !(this.getBlockType() instanceof BlockChest))
483
            {
484
                return 0;
485
            }
486
487
            this.cachedChestType = ((BlockChest)this.getBlockType()).chestType;
488
        }
489
490
        return this.cachedChestType;
491
    }
492
493
    public String getGuiID()
494
    {
495
        return "minecraft:chest";
496
    }
497
498
    public Container createContainer(InventoryPlayer playerInventory, EntityPlayer playerIn)
499
    {
500
        return new ContainerChest(playerInventory, this, playerIn);
501
    }
502
503
    public int getField(int id)
504
    {
505
        return 0;
506
    }
507
508
    public void setField(int id, int value)
509
    {
510
    }
511
512
    public int getFieldCount()
513
    {
514
        return 0;
515
    }
516
517
    public void clear()
518
    {
519
        for (int i = 0; i < this.chestContents.length; ++i)
520
        {
521
            this.chestContents[i] = null;
522
        }
523
    }
524
}
525

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