Cuberite
A lightweight, fast and extensible game server for Minecraft
BlockBed.h
Go to the documentation of this file.
1 
2 // BlockBed.h
3 
4 #pragma once
5 
6 #include "BlockEntity.h"
7 #include "ChunkInterface.h"
8 #include "Entities/Player.h"
9 #include "Mixins.h"
10 
11 
12 
13 
14 
15 class cEntity;
16 class cWorldInterface;
17 
18 
19 
20 
21 
22 class cBlockBedHandler final :
23  public cYawRotator<cBlockEntityHandler, 0x03, 0x02, 0x03, 0x00, 0x01>
24 {
26 
27 public:
28 
29  using Super::Super;
30 
32  {
33  switch (a_MetaData)
34  {
35  case 0: return Vector3i(0, 0, 1);
36  case 1: return Vector3i(-1, 0, 0);
37  case 2: return Vector3i(0, 0, -1);
38  case 3: return Vector3i(1, 0, 0);
39  }
40  return Vector3i();
41  }
42 
43  static void VacateBed(cChunkInterface & a_ChunkInterface, cPlayer & a_Player)
44  {
45  auto BedPosition = a_Player.GetLastBedPos();
46 
48  NIBBLETYPE Meta;
49  a_ChunkInterface.GetBlockTypeMeta(BedPosition, Type, Meta);
50 
51  if (Type != E_BLOCK_BED)
52  {
53  // Bed was incomplete, just wake:
54  a_Player.SetIsInBed(false);
55  return;
56  }
57 
58  if ((Meta & 0x8) == 0)
59  {
60  // BedPosition is the foot of the bed, adjust to the head:
61  BedPosition += MetaDataToDirection(Meta & 0x03);
62 
63  a_ChunkInterface.GetBlockTypeMeta(BedPosition, Type, Meta);
64  if (Type != E_BLOCK_BED)
65  {
66  // Bed was incomplete, just wake:
67  a_Player.SetIsInBed(false);
68  return;
69  }
70  }
71 
72  // Clear the "occupied" bit of the bed's pillow block:
73  a_ChunkInterface.SetBlockMeta(BedPosition, Meta & 0x0b);
74 
75  // Wake the player:
76  a_Player.SetIsInBed(false);
77  }
78 
79 private:
80 
81  // Overrides:
82  virtual void OnBroken(
83  cChunkInterface & a_ChunkInterface, cWorldInterface & a_WorldInterface,
84  const Vector3i a_BlockPos,
85  BLOCKTYPE a_OldBlockType, NIBBLETYPE a_OldBlockMeta,
86  const cEntity * a_Digger
87  ) const override;
88 
89  virtual bool OnUse(
90  cChunkInterface & a_ChunkInterface, cWorldInterface & a_WorldInterface, cPlayer & a_Player,
91  const Vector3i a_ClickedBlockPos,
92  eBlockFace a_ClickedBlockFace,
93  const Vector3i a_CursorPos
94  ) const override;
95 
96  virtual cItems ConvertToPickups(NIBBLETYPE a_BlockMeta, const cItem * a_Tool) const override;
97 
98 
99 
100 
101 
102  virtual ColourID GetMapBaseColourID(NIBBLETYPE a_Meta) const override
103  {
104  UNUSED(a_Meta);
105  return 28;
106  }
107 } ;
@ E_BLOCK_BED
Definition: BlockType.h:36
unsigned char NIBBLETYPE
The datatype used by nibbledata (meta, light, skylight)
Definition: ChunkDef.h:44
unsigned char BLOCKTYPE
The datatype used by blockdata.
Definition: ChunkDef.h:41
eBlockFace
Block face constants, used in PlayerDigging and PlayerBlockPlacement packets and bbox collision calc.
Definition: Defines.h:38
Byte ColourID
Definition: Globals.h:162
#define UNUSED
Definition: Globals.h:72
Vector3< int > Vector3i
Definition: Vector3.h:487
static Vector3i MetaDataToDirection(NIBBLETYPE a_MetaData)
Definition: BlockBed.h:31
virtual void OnBroken(cChunkInterface &a_ChunkInterface, cWorldInterface &a_WorldInterface, const Vector3i a_BlockPos, BLOCKTYPE a_OldBlockType, NIBBLETYPE a_OldBlockMeta, const cEntity *a_Digger) const override
Definition: BlockBed.cpp:17
virtual ColourID GetMapBaseColourID(NIBBLETYPE a_Meta) const override
Definition: BlockBed.h:102
virtual bool OnUse(cChunkInterface &a_ChunkInterface, cWorldInterface &a_WorldInterface, cPlayer &a_Player, const Vector3i a_ClickedBlockPos, eBlockFace a_ClickedBlockFace, const Vector3i a_CursorPos) const override
Definition: BlockBed.cpp:58
static void VacateBed(cChunkInterface &a_ChunkInterface, cPlayer &a_Player)
Definition: BlockBed.h:43
virtual cItems ConvertToPickups(NIBBLETYPE a_BlockMeta, const cItem *a_Tool) const override
Definition: BlockBed.cpp:160
bool GetBlockTypeMeta(Vector3i a_Pos, BLOCKTYPE &a_BlockType, NIBBLETYPE &a_BlockMeta)
void SetBlockMeta(Vector3i a_BlockPos, NIBBLETYPE a_MetaData)
Sets the meta for the specified block, while keeping the blocktype.
Mixin for blocks whose meta on placement depends on the yaw of the player placing the block.
Definition: Mixins.h:162
cMetaRotator< cBlockEntityHandler, BitMask, North, East, South, West, false > Super
Definition: Mixins.h:163
Definition: Entity.h:76
Definition: Player.h:29
Vector3i GetLastBedPos(void) const
Gets the player's potential respawn position (named LastBedPos for compatibility reasons).
Definition: Player.h:515
void SetIsInBed(bool a_IsInBed)
Sets a player's in-bed state.
Definition: Player.cpp:496
Definition: Item.h:37
This class bridges a vector of cItem for safe access via Lua.
Definition: Item.h:215