Cuberite
A lightweight, fast and extensible game server for Minecraft
BlockEntity.cpp
Go to the documentation of this file.
1 
2 // BlockEntity.cpp
3 
4 // Implements the cBlockEntity class that is the common ancestor for all block entities
5 
6 #include "Globals.h"
7 #include "BeaconEntity.h"
8 #include "BedEntity.h"
9 #include "BlockEntity.h"
10 #include "BrewingstandEntity.h"
11 #include "ChestEntity.h"
12 #include "CommandBlockEntity.h"
13 #include "DispenserEntity.h"
14 #include "DropperEntity.h"
15 #include "EnderChestEntity.h"
16 #include "FlowerPotEntity.h"
17 #include "FurnaceEntity.h"
18 #include "HopperEntity.h"
19 #include "MobHeadEntity.h"
20 #include "MobSpawnerEntity.h"
21 #include "JukeboxEntity.h"
22 #include "NoteEntity.h"
23 #include "SignEntity.h"
24 
25 
26 
27 
28 
30 {
31  ASSERT(m_World == nullptr); // Cannot move block entities that represent world blocks (only use this for cBlockArea's BEs)
32  m_Pos = a_NewPos;
33 }
34 
35 
36 
37 
38 
40 {
41  switch (a_BlockType)
42  {
43  case E_BLOCK_BEACON:
44  case E_BLOCK_BED:
46  case E_BLOCK_CHEST:
48  case E_BLOCK_DISPENSER:
49  case E_BLOCK_DROPPER:
51  case E_BLOCK_FLOWER_POT:
52  case E_BLOCK_FURNACE:
53  case E_BLOCK_HEAD:
54  case E_BLOCK_HOPPER:
55  case E_BLOCK_JUKEBOX:
58  case E_BLOCK_NOTE_BLOCK:
59  case E_BLOCK_SIGN_POST:
61  case E_BLOCK_WALLSIGN:
62  {
63  return true;
64  }
65  default:
66  {
67  return false;
68  }
69  }
70 }
71 
72 
73 
74 
75 
76 cBlockEntity * cBlockEntity::CreateByBlockType(BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos, cWorld * a_World)
77 {
78  switch (a_BlockType)
79  {
80  case E_BLOCK_BEACON: return new cBeaconEntity (a_BlockType, a_BlockMeta, a_Pos, a_World);
81  case E_BLOCK_BED: return new cBedEntity (a_BlockType, a_BlockMeta, a_Pos, a_World);
82  case E_BLOCK_BREWING_STAND: return new cBrewingstandEntity(a_BlockType, a_BlockMeta, a_Pos, a_World);
83  case E_BLOCK_CHEST: return new cChestEntity (a_BlockType, a_BlockMeta, a_Pos, a_World);
84  case E_BLOCK_COMMAND_BLOCK: return new cCommandBlockEntity(a_BlockType, a_BlockMeta, a_Pos, a_World);
85  case E_BLOCK_DISPENSER: return new cDispenserEntity (a_BlockType, a_BlockMeta, a_Pos, a_World);
86  case E_BLOCK_DROPPER: return new cDropperEntity (a_BlockType, a_BlockMeta, a_Pos, a_World);
87  case E_BLOCK_ENDER_CHEST: return new cEnderChestEntity (a_BlockType, a_BlockMeta, a_Pos, a_World);
88  case E_BLOCK_FLOWER_POT: return new cFlowerPotEntity (a_BlockType, a_BlockMeta, a_Pos, a_World);
89  case E_BLOCK_FURNACE: return new cFurnaceEntity (a_BlockType, a_BlockMeta, a_Pos, a_World);
90  case E_BLOCK_HEAD: return new cMobHeadEntity (a_BlockType, a_BlockMeta, a_Pos, a_World);
91  case E_BLOCK_HOPPER: return new cHopperEntity (a_BlockType, a_BlockMeta, a_Pos, a_World);
92  case E_BLOCK_JUKEBOX: return new cJukeboxEntity (a_BlockType, a_BlockMeta, a_Pos, a_World);
93  case E_BLOCK_LIT_FURNACE: return new cFurnaceEntity (a_BlockType, a_BlockMeta, a_Pos, a_World);
94  case E_BLOCK_MOB_SPAWNER: return new cMobSpawnerEntity (a_BlockType, a_BlockMeta, a_Pos, a_World);
95  case E_BLOCK_NOTE_BLOCK: return new cNoteEntity (a_BlockType, a_BlockMeta, a_Pos, a_World);
96  case E_BLOCK_SIGN_POST: return new cSignEntity (a_BlockType, a_BlockMeta, a_Pos, a_World);
97  case E_BLOCK_TRAPPED_CHEST: return new cChestEntity (a_BlockType, a_BlockMeta, a_Pos, a_World);
98  case E_BLOCK_WALLSIGN: return new cSignEntity (a_BlockType, a_BlockMeta, a_Pos, a_World);
99  default:
100  {
101  LOGD("%s: Requesting creation of an unknown block entity - block type %d (%s)",
102  __FUNCTION__, a_BlockType, ItemTypeToString(a_BlockType).c_str()
103  );
104  ASSERT(!"Requesting creation of an unknown block entity");
105  return nullptr;
106  }
107  }
108 }
109 
110 
111 
112 
113 
115 {
116  auto res = std::unique_ptr<cBlockEntity>(CreateByBlockType(m_BlockType, m_BlockMeta, a_Pos, nullptr));
117  res->CopyFrom(*this);
118  return res.release();
119 }
120 
121 
122 
123 
124 
126 {
127  // Nothing to copy, but check that we're copying the right entity:
128  ASSERT(m_BlockType == a_Src.m_BlockType);
129  ASSERT(m_BlockMeta == a_Src.m_BlockMeta);
130 }
131 
132 
133 
134 
NIBBLETYPE m_BlockMeta
The block meta representing this particular instance in the world Mainly used for directional entitie...
Definition: BlockEntity.h:153
unsigned char BLOCKTYPE
The datatype used by blockdata.
Definition: ChunkDef.h:42
BLOCKTYPE m_BlockType
The blocktype representing this particular instance in the world.
Definition: BlockEntity.h:149
unsigned char NIBBLETYPE
The datatype used by nibbledata (meta, light, skylight)
Definition: ChunkDef.h:45
cWorld * m_World
Definition: BlockEntity.h:155
cBlockEntity * Clone(Vector3i a_Pos)
Makes an exact copy of this block entity, except for its m_World (set to nullptr), and at a new position.
Definition: World.h:65
#define ASSERT(x)
Definition: Globals.h:335
#define LOGD(...)
Definition: LoggerSimple.h:40
Vector3i m_Pos
Position in absolute block coordinates.
Definition: BlockEntity.h:142
virtual void CopyFrom(const cBlockEntity &a_Src)
Copies all properties of a_Src into this entity, except for its m_World and location.
void SetPos(Vector3i a_NewPos)
Updates the internally stored position.
Definition: BlockEntity.cpp:29
AString ItemTypeToString(short a_ItemType)
Translates itemtype into a string.
Definition: BlockID.cpp:270
static bool IsBlockEntityBlockType(BLOCKTYPE a_BlockType)
Returns true if the specified blocktype is supposed to have an associated block entity.
Definition: BlockEntity.cpp:39
static cBlockEntity * CreateByBlockType(BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos, cWorld *a_World=nullptr)
Creates a new block entity for the specified block type at the specified absolute pos...
Definition: BlockEntity.cpp:76