BlockBed .java

net.minecraft.block.BlockBed (class, 337 lines): members: getSafeExitLocation, hasRoomForPlayer, EnumPartType, OCCUPIED, PART, getMobilityFlag, getItem.

net.minecraft.blockclass337 linescommonextends BlockDirectional10.5 KB

API Surface (19)

boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumFacing side, float hitX, float hitY, float hitZ)
boolean isFullCube()
boolean isOpaqueCube()
void setBlockBoundsBasedOnState(IBlockAccess worldIn, BlockPos pos)
void onNeighborBlockChange(World worldIn, BlockPos pos, IBlockState state, Block neighborBlock)
Item getItemDropped(IBlockState state, Random rand, int fortune)
BlockPos getSafeExitLocation(World worldIn, BlockPos pos, int tries)
boolean hasRoomForPlayer(World worldIn, BlockPos pos)
void dropBlockAsItemWithChance(World worldIn, BlockPos pos, IBlockState state, float chance, int fortune)
int getMobilityFlag()
EnumWorldBlockLayer getBlockLayer()
Item getItem(World worldIn, BlockPos pos)
void onBlockHarvested(World worldIn, BlockPos pos, IBlockState state, EntityPlayer player)
IBlockState getStateFromMeta(int meta)
IBlockState getActualState(IBlockState state, IBlockAccess worldIn, BlockPos pos)
int getMetaFromState(IBlockState state)
BlockState createBlockState()
String toString()
String getName()

Fields & Constants (2)

PART
OCCUPIED

String literals (7)

"part"
"occupied"
"tile.bed.occupied"
"tile.bed.noSleep"
"tile.bed.notSafe"
"head"
"foot"

Full Source

BlockBed.java337 lines · MCP 1.8.9 · verbatimOriginal ↗
1
package net.minecraft.block;
2
3
import java.util.Random;
4
import net.minecraft.block.material.Material;
5
import net.minecraft.block.properties.IProperty;
6
import net.minecraft.block.properties.PropertyBool;
7
import net.minecraft.block.properties.PropertyEnum;
8
import net.minecraft.block.state.BlockState;
9
import net.minecraft.block.state.IBlockState;
10
import net.minecraft.entity.Entity;
11
import net.minecraft.entity.player.EntityPlayer;
12
import net.minecraft.init.Items;
13
import net.minecraft.item.Item;
14
import net.minecraft.util.BlockPos;
15
import net.minecraft.util.ChatComponentTranslation;
16
import net.minecraft.util.EnumFacing;
17
import net.minecraft.util.EnumWorldBlockLayer;
18
import net.minecraft.util.IStringSerializable;
19
import net.minecraft.world.IBlockAccess;
20
import net.minecraft.world.World;
21
import net.minecraft.world.biome.BiomeGenBase;
22
23
public class BlockBed extends BlockDirectional
24
{
25
    public static final PropertyEnum<BlockBed.EnumPartType> PART = PropertyEnum.<BlockBed.EnumPartType>create("part", BlockBed.EnumPartType.class);
26
    public static final PropertyBool OCCUPIED = PropertyBool.create("occupied");
27
28
    public BlockBed()
29
    {
30
        super(Material.cloth);
31
        this.setDefaultState(this.blockState.getBaseState().withProperty(PART, BlockBed.EnumPartType.FOOT).withProperty(OCCUPIED, Boolean.valueOf(false)));
32
        this.setBedBounds();
33
    }
34
35
    public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumFacing side, float hitX, float hitY, float hitZ)
36
    {
37
        if (worldIn.isRemote)
38
        {
39
            return true;
40
        }
41
        else
42
        {
43
            if (state.getValue(PART) != BlockBed.EnumPartType.HEAD)
44
            {
45
                pos = pos.offset((EnumFacing)state.getValue(FACING));
46
                state = worldIn.getBlockState(pos);
47
48
                if (state.getBlock() != this)
49
                {
50
                    return true;
51
                }
52
            }
53
54
            if (worldIn.provider.canRespawnHere() && worldIn.getBiomeGenForCoords(pos) != BiomeGenBase.hell)
55
            {
56
                if (((Boolean)state.getValue(OCCUPIED)).booleanValue())
57
                {
58
                    EntityPlayer entityplayer = this.getPlayerInBed(worldIn, pos);
59
60
                    if (entityplayer != null)
61
                    {
62
                        playerIn.addChatComponentMessage(new ChatComponentTranslation("tile.bed.occupied", new Object[0]));
63
                        return true;
64
                    }
65
66
                    state = state.withProperty(OCCUPIED, Boolean.valueOf(false));
67
                    worldIn.setBlockState(pos, state, 4);
68
                }
69
70
                EntityPlayer.EnumStatus entityplayer$enumstatus = playerIn.trySleep(pos);
71
72
                if (entityplayer$enumstatus == EntityPlayer.EnumStatus.OK)
73
                {
74
                    state = state.withProperty(OCCUPIED, Boolean.valueOf(true));
75
                    worldIn.setBlockState(pos, state, 4);
76
                    return true;
77
                }
78
                else
79
                {
80
                    if (entityplayer$enumstatus == EntityPlayer.EnumStatus.NOT_POSSIBLE_NOW)
81
                    {
82
                        playerIn.addChatComponentMessage(new ChatComponentTranslation("tile.bed.noSleep", new Object[0]));
83
                    }
84
                    else if (entityplayer$enumstatus == EntityPlayer.EnumStatus.NOT_SAFE)
85
                    {
86
                        playerIn.addChatComponentMessage(new ChatComponentTranslation("tile.bed.notSafe", new Object[0]));
87
                    }
88
89
                    return true;
90
                }
91
            }
92
            else
93
            {
94
                worldIn.setBlockToAir(pos);
95
                BlockPos blockpos = pos.offset(((EnumFacing)state.getValue(FACING)).getOpposite());
96
97
                if (worldIn.getBlockState(blockpos).getBlock() == this)
98
                {
99
                    worldIn.setBlockToAir(blockpos);
100
                }
101
102
                worldIn.newExplosion((Entity)null, (double)pos.getX() + 0.5D, (double)pos.getY() + 0.5D, (double)pos.getZ() + 0.5D, 5.0F, true, true);
103
                return true;
104
            }
105
        }
106
    }
107
108
    private EntityPlayer getPlayerInBed(World worldIn, BlockPos pos)
109
    {
110
        for (EntityPlayer entityplayer : worldIn.playerEntities)
111
        {
112
            if (entityplayer.isPlayerSleeping() && entityplayer.playerLocation.equals(pos))
113
            {
114
                return entityplayer;
115
            }
116
        }
117
118
        return null;
119
    }
120
121
    public boolean isFullCube()
122
    {
123
        return false;
124
    }
125
126
    /**
127
     * Used to determine ambient occlusion and culling when rebuilding chunks for render
128
     */
129
    public boolean isOpaqueCube()
130
    {
131
        return false;
132
    }
133
134
    public void setBlockBoundsBasedOnState(IBlockAccess worldIn, BlockPos pos)
135
    {
136
        this.setBedBounds();
137
    }
138
139
    /**
140
     * Called when a neighboring block changes.
141
     */
142
    public void onNeighborBlockChange(World worldIn, BlockPos pos, IBlockState state, Block neighborBlock)
143
    {
144
        EnumFacing enumfacing = (EnumFacing)state.getValue(FACING);
145
146
        if (state.getValue(PART) == BlockBed.EnumPartType.HEAD)
147
        {
148
            if (worldIn.getBlockState(pos.offset(enumfacing.getOpposite())).getBlock() != this)
149
            {
150
                worldIn.setBlockToAir(pos);
151
            }
152
        }
153
        else if (worldIn.getBlockState(pos.offset(enumfacing)).getBlock() != this)
154
        {
155
            worldIn.setBlockToAir(pos);
156
157
            if (!worldIn.isRemote)
158
            {
159
                this.dropBlockAsItem(worldIn, pos, state, 0);
160
            }
161
        }
162
    }
163
164
    /**
165
     * Get the Item that this Block should drop when harvested.
166
     */
167
    public Item getItemDropped(IBlockState state, Random rand, int fortune)
168
    {
169
        return state.getValue(PART) == BlockBed.EnumPartType.HEAD ? null : Items.bed;
170
    }
171
172
    private void setBedBounds()
173
    {
174
        this.setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, 0.5625F, 1.0F);
175
    }
176
177
    /**
178
     * Returns a safe BlockPos to disembark the bed
179
     */
180
    public static BlockPos getSafeExitLocation(World worldIn, BlockPos pos, int tries)
181
    {
182
        EnumFacing enumfacing = (EnumFacing)worldIn.getBlockState(pos).getValue(FACING);
183
        int i = pos.getX();
184
        int j = pos.getY();
185
        int k = pos.getZ();
186
187
        for (int l = 0; l <= 1; ++l)
188
        {
189
            int i1 = i - enumfacing.getFrontOffsetX() * l - 1;
190
            int j1 = k - enumfacing.getFrontOffsetZ() * l - 1;
191
            int k1 = i1 + 2;
192
            int l1 = j1 + 2;
193
194
            for (int i2 = i1; i2 <= k1; ++i2)
195
            {
196
                for (int j2 = j1; j2 <= l1; ++j2)
197
                {
198
                    BlockPos blockpos = new BlockPos(i2, j, j2);
199
200
                    if (hasRoomForPlayer(worldIn, blockpos))
201
                    {
202
                        if (tries <= 0)
203
                        {
204
                            return blockpos;
205
                        }
206
207
                        --tries;
208
                    }
209
                }
210
            }
211
        }
212
213
        return null;
214
    }
215
216
    protected static boolean hasRoomForPlayer(World worldIn, BlockPos pos)
217
    {
218
        return World.doesBlockHaveSolidTopSurface(worldIn, pos.down()) && !worldIn.getBlockState(pos).getBlock().getMaterial().isSolid() && !worldIn.getBlockState(pos.up()).getBlock().getMaterial().isSolid();
219
    }
220
221
    /**
222
     * Spawns this Block's drops into the World as EntityItems.
223
     */
224
    public void dropBlockAsItemWithChance(World worldIn, BlockPos pos, IBlockState state, float chance, int fortune)
225
    {
226
        if (state.getValue(PART) == BlockBed.EnumPartType.FOOT)
227
        {
228
            super.dropBlockAsItemWithChance(worldIn, pos, state, chance, 0);
229
        }
230
    }
231
232
    public int getMobilityFlag()
233
    {
234
        return 1;
235
    }
236
237
    public EnumWorldBlockLayer getBlockLayer()
238
    {
239
        return EnumWorldBlockLayer.CUTOUT;
240
    }
241
242
    public Item getItem(World worldIn, BlockPos pos)
243
    {
244
        return Items.bed;
245
    }
246
247
    public void onBlockHarvested(World worldIn, BlockPos pos, IBlockState state, EntityPlayer player)
248
    {
249
        if (player.capabilities.isCreativeMode && state.getValue(PART) == BlockBed.EnumPartType.HEAD)
250
        {
251
            BlockPos blockpos = pos.offset(((EnumFacing)state.getValue(FACING)).getOpposite());
252
253
            if (worldIn.getBlockState(blockpos).getBlock() == this)
254
            {
255
                worldIn.setBlockToAir(blockpos);
256
            }
257
        }
258
    }
259
260
    /**
261
     * Convert the given metadata into a BlockState for this Block
262
     */
263
    public IBlockState getStateFromMeta(int meta)
264
    {
265
        EnumFacing enumfacing = EnumFacing.getHorizontal(meta);
266
        return (meta & 8) > 0 ? this.getDefaultState().withProperty(PART, BlockBed.EnumPartType.HEAD).withProperty(FACING, enumfacing).withProperty(OCCUPIED, Boolean.valueOf((meta & 4) > 0)) : this.getDefaultState().withProperty(PART, BlockBed.EnumPartType.FOOT).withProperty(FACING, enumfacing);
267
    }
268
269
    /**
270
     * Get the actual Block state of this Block at the given position. This applies properties not visible in the
271
     * metadata, such as fence connections.
272
     */
273
    public IBlockState getActualState(IBlockState state, IBlockAccess worldIn, BlockPos pos)
274
    {
275
        if (state.getValue(PART) == BlockBed.EnumPartType.FOOT)
276
        {
277
            IBlockState iblockstate = worldIn.getBlockState(pos.offset((EnumFacing)state.getValue(FACING)));
278
279
            if (iblockstate.getBlock() == this)
280
            {
281
                state = state.withProperty(OCCUPIED, iblockstate.getValue(OCCUPIED));
282
            }
283
        }
284
285
        return state;
286
    }
287
288
    /**
289
     * Convert the BlockState into the correct metadata value
290
     */
291
    public int getMetaFromState(IBlockState state)
292
    {
293
        int i = 0;
294
        i = i | ((EnumFacing)state.getValue(FACING)).getHorizontalIndex();
295
296
        if (state.getValue(PART) == BlockBed.EnumPartType.HEAD)
297
        {
298
            i |= 8;
299
300
            if (((Boolean)state.getValue(OCCUPIED)).booleanValue())
301
            {
302
                i |= 4;
303
            }
304
        }
305
306
        return i;
307
    }
308
309
    protected BlockState createBlockState()
310
    {
311
        return new BlockState(this, new IProperty[] {FACING, PART, OCCUPIED});
312
    }
313
314
    public static enum EnumPartType implements IStringSerializable
315
    {
316
        HEAD("head"),
317
        FOOT("foot");
318
319
        private final String name;
320
321
        private EnumPartType(String name)
322
        {
323
            this.name = name;
324
        }
325
326
        public String toString()
327
        {
328
            return this.name;
329
        }
330
331
        public String getName()
332
        {
333
            return this.name;
334
        }
335
    }
336
}
337

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