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 "Mixins.h"
8 #include "ChunkInterface.h"
9 
10 
11 class cEntity;
12 class cPlayer;
13 class cWorldInterface;
14 
15 
16 
17 
19  public cMetaRotator<cBlockEntityHandler, 0x3, 0x02, 0x03, 0x00, 0x01, true>
20 {
22 
23 public:
24 
26  super(a_BlockType)
27  {
28  }
29 
30 
31 
32 
33 
34  // Overrides:
35  virtual void OnBroken(cChunkInterface & a_ChunkInterface, cWorldInterface & a_WorldInterface, Vector3i a_BlockPos, BLOCKTYPE a_OldBlockType, NIBBLETYPE a_OldBlockMeta) override;
36  virtual bool OnUse(cChunkInterface & a_ChunkInterface, cWorldInterface & a_WorldInterface, cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ) override;
37  virtual cItems ConvertToPickups(NIBBLETYPE a_BlockMeta, cBlockEntity * a_BlockEntity, const cEntity * a_Digger, const cItem * a_Tool) override;
38  virtual void OnPlacedByPlayer(cChunkInterface & a_ChunkInterface, cWorldInterface & a_WorldInterface, cPlayer & a_Player, const sSetBlock & a_BlockChange) override;
39 
40 
41 
42 
43 
44  // Bed specific helper functions
45  static NIBBLETYPE RotationToMetaData(double a_Rotation)
46  {
47  a_Rotation += 180 + (180 / 4); // So its not aligned with axis
48  if (a_Rotation > 360)
49  {
50  a_Rotation -= 360;
51  }
52 
53  a_Rotation = (a_Rotation / 360) * 4;
54 
55  return (static_cast<NIBBLETYPE>(a_Rotation + 2)) % 4;
56  }
57 
58 
59 
60 
61 
63  {
64  switch (a_MetaData)
65  {
66  case 0: return Vector3i( 0, 0, 1);
67  case 1: return Vector3i(-1, 0, 0);
68  case 2: return Vector3i( 0, 0, -1);
69  case 3: return Vector3i( 1, 0, 0);
70  }
71  return Vector3i();
72  }
73 
74 
75 
76 
77 
78  static void SetBedOccupationState(cChunkInterface & a_ChunkInterface, Vector3i a_BedPosition, bool a_IsOccupied)
79  {
80  auto Meta = a_ChunkInterface.GetBlockMeta(a_BedPosition);
81  if (a_IsOccupied)
82  {
83  Meta |= 0x04; // Where 0x4 = occupied bit
84  }
85  else
86  {
87  Meta &= 0x0b; // Clear the "occupied" bit of the bed's block
88  }
89 
90  a_ChunkInterface.SetBlockMeta(a_BedPosition, Meta);
91  }
92 
93 
94 
95 
96 
97  virtual ColourID GetMapBaseColourID(NIBBLETYPE a_Meta) override
98  {
99  UNUSED(a_Meta);
100  return 28;
101  }
102 } ;
103 
104 
105 
106 
virtual void OnPlacedByPlayer(cChunkInterface &a_ChunkInterface, cWorldInterface &a_WorldInterface, cPlayer &a_Player, const sSetBlock &a_BlockChange) override
Called by cPlayer::PlaceBlocks() for each block after it has been set to the world.
Definition: BlockBed.cpp:142
cBlockBedHandler(BLOCKTYPE a_BlockType)
Definition: BlockBed.h:25
unsigned char BLOCKTYPE
The datatype used by blockdata.
Definition: ChunkDef.h:42
virtual void OnBroken(cChunkInterface &a_ChunkInterface, cWorldInterface &a_WorldInterface, Vector3i a_BlockPos, BLOCKTYPE a_OldBlockType, NIBBLETYPE a_OldBlockMeta) override
Called after a block gets broken (replaced with air), either by player or by natural means...
Definition: BlockBed.cpp:18
Definition: Player.h:27
static Vector3i MetaDataToDirection(NIBBLETYPE a_MetaData)
Definition: BlockBed.h:62
virtual ColourID GetMapBaseColourID(NIBBLETYPE a_Meta) override
Returns the base colour ID of the block, as will be represented on a map, as per documentation: https...
Definition: BlockBed.h:97
static void SetBedOccupationState(cChunkInterface &a_ChunkInterface, Vector3i a_BedPosition, bool a_IsOccupied)
Definition: BlockBed.h:78
unsigned char NIBBLETYPE
The datatype used by nibbledata (meta, light, skylight)
Definition: ChunkDef.h:45
virtual bool OnUse(cChunkInterface &a_ChunkInterface, cWorldInterface &a_WorldInterface, cPlayer &a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ) override
Called if the user right clicks the block and the block is useable returns true if the use was succes...
Definition: BlockBed.cpp:53
Mixin for rotations and reflections following the standard pattern of "apply mask, then use a switch".
Definition: Mixins.h:84
#define UNUSED
Definition: Globals.h:152
eBlockFace
Block face constants, used in PlayerDigging and PlayerBlockPlacement packets and bbox collision calc...
Definition: Defines.h:29
static NIBBLETYPE RotationToMetaData(double a_Rotation)
Definition: BlockBed.h:45
NIBBLETYPE GetBlockMeta(Vector3i a_Pos)
Byte ColourID
Definition: Globals.h:118
Definition: Entity.h:73
void SetBlockMeta(Vector3i a_BlockPos, NIBBLETYPE a_MetaData, bool a_ShouldMarkDirty=true, bool a_ShouldInformClient=true)
Sets the meta for the specified block, while keeping the blocktype.
Vector3< int > Vector3i
Definition: Vector3.h:447
Definition: Item.h:36
virtual cItems ConvertToPickups(NIBBLETYPE a_BlockMeta, cBlockEntity *a_BlockEntity, const cEntity *a_Digger, const cItem *a_Tool) override
Returns the pickups that would result if the block was mined by a_Digger using a_Tool.
Definition: BlockBed.cpp:156
This class bridges a vector of cItem for safe access via Lua.
Definition: Item.h:234