BlockChest .java

net.minecraft.block.BlockChest (class, 610 lines): members: checkForSurroundingChests, getLockableContainer, correctFacing, chestType, canProvidePower.

net.minecraft.blockclass610 linescommonextends BlockContainer19.2 KB

API Surface (23)

boolean isOpaqueCube()
boolean isFullCube()
int getRenderType()
void setBlockBoundsBasedOnState(IBlockAccess worldIn, BlockPos pos)
void onBlockAdded(World worldIn, BlockPos pos, IBlockState state)
IBlockState onBlockPlaced(World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer)
void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack)
IBlockState checkForSurroundingChests(World worldIn, BlockPos pos, IBlockState state)
IBlockState correctFacing(World worldIn, BlockPos pos, IBlockState state)
boolean canPlaceBlockAt(World worldIn, BlockPos pos)
void onNeighborBlockChange(World worldIn, BlockPos pos, IBlockState state, Block neighborBlock)
void breakBlock(World worldIn, BlockPos pos, IBlockState state)
boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumFacing side, float hitX, float hitY, float hitZ)
ILockableContainer getLockableContainer(World worldIn, BlockPos pos)
TileEntity createNewTileEntity(World worldIn, int meta)
boolean canProvidePower()
int getWeakPower(IBlockAccess worldIn, BlockPos pos, IBlockState state, EnumFacing side)
int getStrongPower(IBlockAccess worldIn, BlockPos pos, IBlockState state, EnumFacing side)
boolean hasComparatorInputOverride()
int getComparatorInputOverride(World worldIn, BlockPos pos)
IBlockState getStateFromMeta(int meta)
int getMetaFromState(IBlockState state)
BlockState createBlockState()

Fields & Constants (2)

FACING
chestType

String literals (2)

"facing"
"container.chestDouble"

Full Source

BlockChest.java610 lines · MCP 1.8.9 · verbatimOriginal ↗
1
package net.minecraft.block;
2
3
import net.minecraft.block.material.Material;
4
import net.minecraft.block.properties.IProperty;
5
import net.minecraft.block.properties.PropertyDirection;
6
import net.minecraft.block.state.BlockState;
7
import net.minecraft.block.state.IBlockState;
8
import net.minecraft.creativetab.CreativeTabs;
9
import net.minecraft.entity.Entity;
10
import net.minecraft.entity.EntityLivingBase;
11
import net.minecraft.entity.passive.EntityOcelot;
12
import net.minecraft.entity.player.EntityPlayer;
13
import net.minecraft.inventory.Container;
14
import net.minecraft.inventory.IInventory;
15
import net.minecraft.inventory.InventoryHelper;
16
import net.minecraft.inventory.InventoryLargeChest;
17
import net.minecraft.item.ItemStack;
18
import net.minecraft.stats.StatList;
19
import net.minecraft.tileentity.TileEntity;
20
import net.minecraft.tileentity.TileEntityChest;
21
import net.minecraft.util.AxisAlignedBB;
22
import net.minecraft.util.BlockPos;
23
import net.minecraft.util.EnumFacing;
24
import net.minecraft.util.MathHelper;
25
import net.minecraft.world.IBlockAccess;
26
import net.minecraft.world.ILockableContainer;
27
import net.minecraft.world.World;
28
29
public class BlockChest extends BlockContainer
30
{
31
    public static final PropertyDirection FACING = PropertyDirection.create("facing", EnumFacing.Plane.HORIZONTAL);
32
33
    /** 0 : Normal chest, 1 : Trapped chest */
34
    public final int chestType;
35
36
    protected BlockChest(int type)
37
    {
38
        super(Material.wood);
39
        this.setDefaultState(this.blockState.getBaseState().withProperty(FACING, EnumFacing.NORTH));
40
        this.chestType = type;
41
        this.setCreativeTab(CreativeTabs.tabDecorations);
42
        this.setBlockBounds(0.0625F, 0.0F, 0.0625F, 0.9375F, 0.875F, 0.9375F);
43
    }
44
45
    /**
46
     * Used to determine ambient occlusion and culling when rebuilding chunks for render
47
     */
48
    public boolean isOpaqueCube()
49
    {
50
        return false;
51
    }
52
53
    public boolean isFullCube()
54
    {
55
        return false;
56
    }
57
58
    /**
59
     * The type of render function called. 3 for standard block models, 2 for TESR's, 1 for liquids, -1 is no render
60
     */
61
    public int getRenderType()
62
    {
63
        return 2;
64
    }
65
66
    public void setBlockBoundsBasedOnState(IBlockAccess worldIn, BlockPos pos)
67
    {
68
        if (worldIn.getBlockState(pos.north()).getBlock() == this)
69
        {
70
            this.setBlockBounds(0.0625F, 0.0F, 0.0F, 0.9375F, 0.875F, 0.9375F);
71
        }
72
        else if (worldIn.getBlockState(pos.south()).getBlock() == this)
73
        {
74
            this.setBlockBounds(0.0625F, 0.0F, 0.0625F, 0.9375F, 0.875F, 1.0F);
75
        }
76
        else if (worldIn.getBlockState(pos.west()).getBlock() == this)
77
        {
78
            this.setBlockBounds(0.0F, 0.0F, 0.0625F, 0.9375F, 0.875F, 0.9375F);
79
        }
80
        else if (worldIn.getBlockState(pos.east()).getBlock() == this)
81
        {
82
            this.setBlockBounds(0.0625F, 0.0F, 0.0625F, 1.0F, 0.875F, 0.9375F);
83
        }
84
        else
85
        {
86
            this.setBlockBounds(0.0625F, 0.0F, 0.0625F, 0.9375F, 0.875F, 0.9375F);
87
        }
88
    }
89
90
    public void onBlockAdded(World worldIn, BlockPos pos, IBlockState state)
91
    {
92
        this.checkForSurroundingChests(worldIn, pos, state);
93
94
        for (EnumFacing enumfacing : EnumFacing.Plane.HORIZONTAL)
95
        {
96
            BlockPos blockpos = pos.offset(enumfacing);
97
            IBlockState iblockstate = worldIn.getBlockState(blockpos);
98
99
            if (iblockstate.getBlock() == this)
100
            {
101
                this.checkForSurroundingChests(worldIn, blockpos, iblockstate);
102
            }
103
        }
104
    }
105
106
    /**
107
     * Called by ItemBlocks just before a block is actually set in the world, to allow for adjustments to the
108
     * IBlockstate
109
     */
110
    public IBlockState onBlockPlaced(World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer)
111
    {
112
        return this.getDefaultState().withProperty(FACING, placer.getHorizontalFacing());
113
    }
114
115
    /**
116
     * Called by ItemBlocks after a block is set in the world, to allow post-place logic
117
     */
118
    public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack)
119
    {
120
        EnumFacing enumfacing = EnumFacing.getHorizontal(MathHelper.floor_double((double)(placer.rotationYaw * 4.0F / 360.0F) + 0.5D) & 3).getOpposite();
121
        state = state.withProperty(FACING, enumfacing);
122
        BlockPos blockpos = pos.north();
123
        BlockPos blockpos1 = pos.south();
124
        BlockPos blockpos2 = pos.west();
125
        BlockPos blockpos3 = pos.east();
126
        boolean flag = this == worldIn.getBlockState(blockpos).getBlock();
127
        boolean flag1 = this == worldIn.getBlockState(blockpos1).getBlock();
128
        boolean flag2 = this == worldIn.getBlockState(blockpos2).getBlock();
129
        boolean flag3 = this == worldIn.getBlockState(blockpos3).getBlock();
130
131
        if (!flag && !flag1 && !flag2 && !flag3)
132
        {
133
            worldIn.setBlockState(pos, state, 3);
134
        }
135
        else if (enumfacing.getAxis() != EnumFacing.Axis.X || !flag && !flag1)
136
        {
137
            if (enumfacing.getAxis() == EnumFacing.Axis.Z && (flag2 || flag3))
138
            {
139
                if (flag2)
140
                {
141
                    worldIn.setBlockState(blockpos2, state, 3);
142
                }
143
                else
144
                {
145
                    worldIn.setBlockState(blockpos3, state, 3);
146
                }
147
148
                worldIn.setBlockState(pos, state, 3);
149
            }
150
        }
151
        else
152
        {
153
            if (flag)
154
            {
155
                worldIn.setBlockState(blockpos, state, 3);
156
            }
157
            else
158
            {
159
                worldIn.setBlockState(blockpos1, state, 3);
160
            }
161
162
            worldIn.setBlockState(pos, state, 3);
163
        }
164
165
        if (stack.hasDisplayName())
166
        {
167
            TileEntity tileentity = worldIn.getTileEntity(pos);
168
169
            if (tileentity instanceof TileEntityChest)
170
            {
171
                ((TileEntityChest)tileentity).setCustomName(stack.getDisplayName());
172
            }
173
        }
174
    }
175
176
    public IBlockState checkForSurroundingChests(World worldIn, BlockPos pos, IBlockState state)
177
    {
178
        if (worldIn.isRemote)
179
        {
180
            return state;
181
        }
182
        else
183
        {
184
            IBlockState iblockstate = worldIn.getBlockState(pos.north());
185
            IBlockState iblockstate1 = worldIn.getBlockState(pos.south());
186
            IBlockState iblockstate2 = worldIn.getBlockState(pos.west());
187
            IBlockState iblockstate3 = worldIn.getBlockState(pos.east());
188
            EnumFacing enumfacing = (EnumFacing)state.getValue(FACING);
189
            Block block = iblockstate.getBlock();
190
            Block block1 = iblockstate1.getBlock();
191
            Block block2 = iblockstate2.getBlock();
192
            Block block3 = iblockstate3.getBlock();
193
194
            if (block != this && block1 != this)
195
            {
196
                boolean flag = block.isFullBlock();
197
                boolean flag1 = block1.isFullBlock();
198
199
                if (block2 == this || block3 == this)
200
                {
201
                    BlockPos blockpos1 = block2 == this ? pos.west() : pos.east();
202
                    IBlockState iblockstate6 = worldIn.getBlockState(blockpos1.north());
203
                    IBlockState iblockstate7 = worldIn.getBlockState(blockpos1.south());
204
                    enumfacing = EnumFacing.SOUTH;
205
                    EnumFacing enumfacing2;
206
207
                    if (block2 == this)
208
                    {
209
                        enumfacing2 = (EnumFacing)iblockstate2.getValue(FACING);
210
                    }
211
                    else
212
                    {
213
                        enumfacing2 = (EnumFacing)iblockstate3.getValue(FACING);
214
                    }
215
216
                    if (enumfacing2 == EnumFacing.NORTH)
217
                    {
218
                        enumfacing = EnumFacing.NORTH;
219
                    }
220
221
                    Block block6 = iblockstate6.getBlock();
222
                    Block block7 = iblockstate7.getBlock();
223
224
                    if ((flag || block6.isFullBlock()) && !flag1 && !block7.isFullBlock())
225
                    {
226
                        enumfacing = EnumFacing.SOUTH;
227
                    }
228
229
                    if ((flag1 || block7.isFullBlock()) && !flag && !block6.isFullBlock())
230
                    {
231
                        enumfacing = EnumFacing.NORTH;
232
                    }
233
                }
234
            }
235
            else
236
            {
237
                BlockPos blockpos = block == this ? pos.north() : pos.south();
238
                IBlockState iblockstate4 = worldIn.getBlockState(blockpos.west());
239
                IBlockState iblockstate5 = worldIn.getBlockState(blockpos.east());
240
                enumfacing = EnumFacing.EAST;
241
                EnumFacing enumfacing1;
242
243
                if (block == this)
244
                {
245
                    enumfacing1 = (EnumFacing)iblockstate.getValue(FACING);
246
                }
247
                else
248
                {
249
                    enumfacing1 = (EnumFacing)iblockstate1.getValue(FACING);
250
                }
251
252
                if (enumfacing1 == EnumFacing.WEST)
253
                {
254
                    enumfacing = EnumFacing.WEST;
255
                }
256
257
                Block block4 = iblockstate4.getBlock();
258
                Block block5 = iblockstate5.getBlock();
259
260
                if ((block2.isFullBlock() || block4.isFullBlock()) && !block3.isFullBlock() && !block5.isFullBlock())
261
                {
262
                    enumfacing = EnumFacing.EAST;
263
                }
264
265
                if ((block3.isFullBlock() || block5.isFullBlock()) && !block2.isFullBlock() && !block4.isFullBlock())
266
                {
267
                    enumfacing = EnumFacing.WEST;
268
                }
269
            }
270
271
            state = state.withProperty(FACING, enumfacing);
272
            worldIn.setBlockState(pos, state, 3);
273
            return state;
274
        }
275
    }
276
277
    public IBlockState correctFacing(World worldIn, BlockPos pos, IBlockState state)
278
    {
279
        EnumFacing enumfacing = null;
280
281
        for (EnumFacing enumfacing1 : EnumFacing.Plane.HORIZONTAL)
282
        {
283
            IBlockState iblockstate = worldIn.getBlockState(pos.offset(enumfacing1));
284
285
            if (iblockstate.getBlock() == this)
286
            {
287
                return state;
288
            }
289
290
            if (iblockstate.getBlock().isFullBlock())
291
            {
292
                if (enumfacing != null)
293
                {
294
                    enumfacing = null;
295
                    break;
296
                }
297
298
                enumfacing = enumfacing1;
299
            }
300
        }
301
302
        if (enumfacing != null)
303
        {
304
            return state.withProperty(FACING, enumfacing.getOpposite());
305
        }
306
        else
307
        {
308
            EnumFacing enumfacing2 = (EnumFacing)state.getValue(FACING);
309
310
            if (worldIn.getBlockState(pos.offset(enumfacing2)).getBlock().isFullBlock())
311
            {
312
                enumfacing2 = enumfacing2.getOpposite();
313
            }
314
315
            if (worldIn.getBlockState(pos.offset(enumfacing2)).getBlock().isFullBlock())
316
            {
317
                enumfacing2 = enumfacing2.rotateY();
318
            }
319
320
            if (worldIn.getBlockState(pos.offset(enumfacing2)).getBlock().isFullBlock())
321
            {
322
                enumfacing2 = enumfacing2.getOpposite();
323
            }
324
325
            return state.withProperty(FACING, enumfacing2);
326
        }
327
    }
328
329
    public boolean canPlaceBlockAt(World worldIn, BlockPos pos)
330
    {
331
        int i = 0;
332
        BlockPos blockpos = pos.west();
333
        BlockPos blockpos1 = pos.east();
334
        BlockPos blockpos2 = pos.north();
335
        BlockPos blockpos3 = pos.south();
336
337
        if (worldIn.getBlockState(blockpos).getBlock() == this)
338
        {
339
            if (this.isDoubleChest(worldIn, blockpos))
340
            {
341
                return false;
342
            }
343
344
            ++i;
345
        }
346
347
        if (worldIn.getBlockState(blockpos1).getBlock() == this)
348
        {
349
            if (this.isDoubleChest(worldIn, blockpos1))
350
            {
351
                return false;
352
            }
353
354
            ++i;
355
        }
356
357
        if (worldIn.getBlockState(blockpos2).getBlock() == this)
358
        {
359
            if (this.isDoubleChest(worldIn, blockpos2))
360
            {
361
                return false;
362
            }
363
364
            ++i;
365
        }
366
367
        if (worldIn.getBlockState(blockpos3).getBlock() == this)
368
        {
369
            if (this.isDoubleChest(worldIn, blockpos3))
370
            {
371
                return false;
372
            }
373
374
            ++i;
375
        }
376
377
        return i <= 1;
378
    }
379
380
    private boolean isDoubleChest(World worldIn, BlockPos pos)
381
    {
382
        if (worldIn.getBlockState(pos).getBlock() != this)
383
        {
384
            return false;
385
        }
386
        else
387
        {
388
            for (EnumFacing enumfacing : EnumFacing.Plane.HORIZONTAL)
389
            {
390
                if (worldIn.getBlockState(pos.offset(enumfacing)).getBlock() == this)
391
                {
392
                    return true;
393
                }
394
            }
395
396
            return false;
397
        }
398
    }
399
400
    /**
401
     * Called when a neighboring block changes.
402
     */
403
    public void onNeighborBlockChange(World worldIn, BlockPos pos, IBlockState state, Block neighborBlock)
404
    {
405
        super.onNeighborBlockChange(worldIn, pos, state, neighborBlock);
406
        TileEntity tileentity = worldIn.getTileEntity(pos);
407
408
        if (tileentity instanceof TileEntityChest)
409
        {
410
            tileentity.updateContainingBlockInfo();
411
        }
412
    }
413
414
    public void breakBlock(World worldIn, BlockPos pos, IBlockState state)
415
    {
416
        TileEntity tileentity = worldIn.getTileEntity(pos);
417
418
        if (tileentity instanceof IInventory)
419
        {
420
            InventoryHelper.dropInventoryItems(worldIn, pos, (IInventory)tileentity);
421
            worldIn.updateComparatorOutputLevel(pos, this);
422
        }
423
424
        super.breakBlock(worldIn, pos, state);
425
    }
426
427
    public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumFacing side, float hitX, float hitY, float hitZ)
428
    {
429
        if (worldIn.isRemote)
430
        {
431
            return true;
432
        }
433
        else
434
        {
435
            ILockableContainer ilockablecontainer = this.getLockableContainer(worldIn, pos);
436
437
            if (ilockablecontainer != null)
438
            {
439
                playerIn.displayGUIChest(ilockablecontainer);
440
441
                if (this.chestType == 0)
442
                {
443
                    playerIn.triggerAchievement(StatList.field_181723_aa);
444
                }
445
                else if (this.chestType == 1)
446
                {
447
                    playerIn.triggerAchievement(StatList.field_181737_U);
448
                }
449
            }
450
451
            return true;
452
        }
453
    }
454
455
    public ILockableContainer getLockableContainer(World worldIn, BlockPos pos)
456
    {
457
        TileEntity tileentity = worldIn.getTileEntity(pos);
458
459
        if (!(tileentity instanceof TileEntityChest))
460
        {
461
            return null;
462
        }
463
        else
464
        {
465
            ILockableContainer ilockablecontainer = (TileEntityChest)tileentity;
466
467
            if (this.isBlocked(worldIn, pos))
468
            {
469
                return null;
470
            }
471
            else
472
            {
473
                for (EnumFacing enumfacing : EnumFacing.Plane.HORIZONTAL)
474
                {
475
                    BlockPos blockpos = pos.offset(enumfacing);
476
                    Block block = worldIn.getBlockState(blockpos).getBlock();
477
478
                    if (block == this)
479
                    {
480
                        if (this.isBlocked(worldIn, blockpos))
481
                        {
482
                            return null;
483
                        }
484
485
                        TileEntity tileentity1 = worldIn.getTileEntity(blockpos);
486
487
                        if (tileentity1 instanceof TileEntityChest)
488
                        {
489
                            if (enumfacing != EnumFacing.WEST && enumfacing != EnumFacing.NORTH)
490
                            {
491
                                ilockablecontainer = new InventoryLargeChest("container.chestDouble", ilockablecontainer, (TileEntityChest)tileentity1);
492
                            }
493
                            else
494
                            {
495
                                ilockablecontainer = new InventoryLargeChest("container.chestDouble", (TileEntityChest)tileentity1, ilockablecontainer);
496
                            }
497
                        }
498
                    }
499
                }
500
501
                return ilockablecontainer;
502
            }
503
        }
504
    }
505
506
    /**
507
     * Returns a new instance of a block's tile entity class. Called on placing the block.
508
     */
509
    public TileEntity createNewTileEntity(World worldIn, int meta)
510
    {
511
        return new TileEntityChest();
512
    }
513
514
    /**
515
     * Can this block provide power. Only wire currently seems to have this change based on its state.
516
     */
517
    public boolean canProvidePower()
518
    {
519
        return this.chestType == 1;
520
    }
521
522
    public int getWeakPower(IBlockAccess worldIn, BlockPos pos, IBlockState state, EnumFacing side)
523
    {
524
        if (!this.canProvidePower())
525
        {
526
            return 0;
527
        }
528
        else
529
        {
530
            int i = 0;
531
            TileEntity tileentity = worldIn.getTileEntity(pos);
532
533
            if (tileentity instanceof TileEntityChest)
534
            {
535
                i = ((TileEntityChest)tileentity).numPlayersUsing;
536
            }
537
538
            return MathHelper.clamp_int(i, 0, 15);
539
        }
540
    }
541
542
    public int getStrongPower(IBlockAccess worldIn, BlockPos pos, IBlockState state, EnumFacing side)
543
    {
544
        return side == EnumFacing.UP ? this.getWeakPower(worldIn, pos, state, side) : 0;
545
    }
546
547
    private boolean isBlocked(World worldIn, BlockPos pos)
548
    {
549
        return this.isBelowSolidBlock(worldIn, pos) || this.isOcelotSittingOnChest(worldIn, pos);
550
    }
551
552
    private boolean isBelowSolidBlock(World worldIn, BlockPos pos)
553
    {
554
        return worldIn.getBlockState(pos.up()).getBlock().isNormalCube();
555
    }
556
557
    private boolean isOcelotSittingOnChest(World worldIn, BlockPos pos)
558
    {
559
        for (Entity entity : worldIn.getEntitiesWithinAABB(EntityOcelot.class, new AxisAlignedBB((double)pos.getX(), (double)(pos.getY() + 1), (double)pos.getZ(), (double)(pos.getX() + 1), (double)(pos.getY() + 2), (double)(pos.getZ() + 1))))
560
        {
561
            EntityOcelot entityocelot = (EntityOcelot)entity;
562
563
            if (entityocelot.isSitting())
564
            {
565
                return true;
566
            }
567
        }
568
569
        return false;
570
    }
571
572
    public boolean hasComparatorInputOverride()
573
    {
574
        return true;
575
    }
576
577
    public int getComparatorInputOverride(World worldIn, BlockPos pos)
578
    {
579
        return Container.calcRedstoneFromInventory(this.getLockableContainer(worldIn, pos));
580
    }
581
582
    /**
583
     * Convert the given metadata into a BlockState for this Block
584
     */
585
    public IBlockState getStateFromMeta(int meta)
586
    {
587
        EnumFacing enumfacing = EnumFacing.getFront(meta);
588
589
        if (enumfacing.getAxis() == EnumFacing.Axis.Y)
590
        {
591
            enumfacing = EnumFacing.NORTH;
592
        }
593
594
        return this.getDefaultState().withProperty(FACING, enumfacing);
595
    }
596
597
    /**
598
     * Convert the BlockState into the correct metadata value
599
     */
600
    public int getMetaFromState(IBlockState state)
601
    {
602
        return ((EnumFacing)state.getValue(FACING)).getIndex();
603
    }
604
605
    protected BlockState createBlockState()
606
    {
607
        return new BlockState(this, new IProperty[] {FACING});
608
    }
609
}
610

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