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 "BannerEntity.h"
8 #include "BeaconEntity.h"
9 #include "BedEntity.h"
10 #include "BlockEntity.h"
11 #include "EnchantingTableEntity.h"
12 #include "BrewingstandEntity.h"
13 #include "ChestEntity.h"
14 #include "CommandBlockEntity.h"
15 #include "DispenserEntity.h"
16 #include "DropperEntity.h"
17 #include "EnderChestEntity.h"
18 #include "EndPortalEntity.h"
19 #include "FlowerPotEntity.h"
20 #include "FurnaceEntity.h"
21 #include "HopperEntity.h"
22 #include "MobHeadEntity.h"
23 #include "MobSpawnerEntity.h"
24 #include "JukeboxEntity.h"
25 #include "NoteEntity.h"
26 #include "SignEntity.h"
27 
28 
29 
30 
31 
32 cBlockEntity::cBlockEntity(const BLOCKTYPE a_BlockType, const NIBBLETYPE a_BlockMeta, const Vector3i a_Pos, cWorld * const a_World) :
33  m_Pos(a_Pos),
34  m_RelX(a_Pos.x - cChunkDef::Width * FAST_FLOOR_DIV(a_Pos.x, cChunkDef::Width)),
35  m_RelZ(a_Pos.z - cChunkDef::Width * FAST_FLOOR_DIV(a_Pos.z, cChunkDef::Width)),
36  m_BlockType(a_BlockType),
37  m_BlockMeta(a_BlockMeta),
38  m_World(a_World)
39 {
40 }
41 
42 
43 
44 
45 
47 {
48  auto res = CreateByBlockType(m_BlockType, m_BlockMeta, a_Pos, nullptr);
49  res->CopyFrom(*this);
50  return res;
51 }
52 
53 
54 
55 
56 
58 {
59  return {};
60 }
61 
62 
63 
64 
65 
67 {
68  // Nothing to copy, but check that we're copying the right entity:
69  ASSERT(m_BlockType == a_Src.m_BlockType);
70  ASSERT(m_BlockMeta == a_Src.m_BlockMeta);
71 }
72 
73 
74 
75 
76 
77 OwnedBlockEntity cBlockEntity::CreateByBlockType(const BLOCKTYPE a_BlockType, const NIBBLETYPE a_BlockMeta, const Vector3i a_Pos, cWorld * const a_World)
78 {
79  switch (a_BlockType)
80  {
81  // Banners:
83  case E_BLOCK_WALL_BANNER: return std::make_unique<cBannerEntity >(a_BlockType, a_BlockMeta, a_Pos, a_World);
84 
85  // Others:
86  case E_BLOCK_BEACON: return std::make_unique<cBeaconEntity >(a_BlockType, a_BlockMeta, a_Pos, a_World);
87  case E_BLOCK_BED: return std::make_unique<cBedEntity >(a_BlockType, a_BlockMeta, a_Pos, a_World);
88  case E_BLOCK_BREWING_STAND: return std::make_unique<cBrewingstandEntity >(a_BlockType, a_BlockMeta, a_Pos, a_World);
89  case E_BLOCK_CHEST: return std::make_unique<cChestEntity >(a_BlockType, a_BlockMeta, a_Pos, a_World);
90  case E_BLOCK_COMMAND_BLOCK: return std::make_unique<cCommandBlockEntity >(a_BlockType, a_BlockMeta, a_Pos, a_World);
91  case E_BLOCK_DISPENSER: return std::make_unique<cDispenserEntity >(a_BlockType, a_BlockMeta, a_Pos, a_World);
92  case E_BLOCK_DROPPER: return std::make_unique<cDropperEntity >(a_BlockType, a_BlockMeta, a_Pos, a_World);
93  case E_BLOCK_ENCHANTMENT_TABLE: return std::make_unique<cEnchantingTableEntity>(a_BlockType, a_BlockMeta, a_Pos, a_World);
94  case E_BLOCK_ENDER_CHEST: return std::make_unique<cEnderChestEntity >(a_BlockType, a_BlockMeta, a_Pos, a_World);
95  case E_BLOCK_END_PORTAL: return std::make_unique<cEndPortalEntity >(a_BlockType, a_BlockMeta, a_Pos, a_World);
96  case E_BLOCK_FLOWER_POT: return std::make_unique<cFlowerPotEntity >(a_BlockType, a_BlockMeta, a_Pos, a_World);
97  case E_BLOCK_FURNACE: return std::make_unique<cFurnaceEntity >(a_BlockType, a_BlockMeta, a_Pos, a_World);
98  case E_BLOCK_HEAD: return std::make_unique<cMobHeadEntity >(a_BlockType, a_BlockMeta, a_Pos, a_World);
99  case E_BLOCK_HOPPER: return std::make_unique<cHopperEntity >(a_BlockType, a_BlockMeta, a_Pos, a_World);
100  case E_BLOCK_JUKEBOX: return std::make_unique<cJukeboxEntity >(a_BlockType, a_BlockMeta, a_Pos, a_World);
101  case E_BLOCK_LIT_FURNACE: return std::make_unique<cFurnaceEntity >(a_BlockType, a_BlockMeta, a_Pos, a_World);
102  case E_BLOCK_MOB_SPAWNER: return std::make_unique<cMobSpawnerEntity >(a_BlockType, a_BlockMeta, a_Pos, a_World);
103  case E_BLOCK_NOTE_BLOCK: return std::make_unique<cNoteEntity >(a_BlockType, a_BlockMeta, a_Pos, a_World);
104  case E_BLOCK_SIGN_POST: return std::make_unique<cSignEntity >(a_BlockType, a_BlockMeta, a_Pos, a_World);
105  case E_BLOCK_TRAPPED_CHEST: return std::make_unique<cChestEntity >(a_BlockType, a_BlockMeta, a_Pos, a_World);
106  case E_BLOCK_WALLSIGN: return std::make_unique<cSignEntity >(a_BlockType, a_BlockMeta, a_Pos, a_World);
107  default:
108  {
109  LOGD("%s: Requesting creation of an unknown block entity - block type %d (%s)",
110  __FUNCTION__, a_BlockType, ItemTypeToString(a_BlockType).c_str()
111  );
112  ASSERT(!"Requesting creation of an unknown block entity");
113  return nullptr;
114  }
115  }
116 }
117 
118 
119 
120 
121 
123 {
124 }
125 
126 
127 
128 
129 
131 {
132  switch (a_BlockType)
133  {
134  case E_BLOCK_BEACON:
135  case E_BLOCK_BED:
137  case E_BLOCK_CHEST:
139  case E_BLOCK_DISPENSER:
140  case E_BLOCK_DROPPER:
142  case E_BLOCK_ENDER_CHEST:
143  case E_BLOCK_END_PORTAL:
144  case E_BLOCK_FLOWER_POT:
145  case E_BLOCK_FURNACE:
146  case E_BLOCK_HEAD:
147  case E_BLOCK_HOPPER:
148  case E_BLOCK_JUKEBOX:
149  case E_BLOCK_LIT_FURNACE:
150  case E_BLOCK_MOB_SPAWNER:
151  case E_BLOCK_NOTE_BLOCK:
152  case E_BLOCK_SIGN_POST:
155  case E_BLOCK_WALL_BANNER:
156  case E_BLOCK_WALLSIGN:
157  {
158  return true;
159  }
160  default:
161  {
162  return false;
163  }
164  }
165 }
166 
167 
168 
169 
170 
171 void cBlockEntity::OnAddToWorld(cWorld & a_World, cChunk & a_Chunk)
172 {
173  m_World = &a_World;
174 }
175 
176 
177 
178 
179 
181 {
182 }
183 
184 
185 
186 
187 
188 void cBlockEntity::SetPos(const Vector3i a_NewPos)
189 {
190  ASSERT(m_World == nullptr); // Cannot move block entities that represent world blocks (only use this for cBlockArea's BEs)
191  m_Pos = a_NewPos;
192 }
193 
194 
195 
196 
197 
198 void cBlockEntity::SetWorld(cWorld * const a_World)
199 {
200  m_World = a_World;
201 }
202 
203 
204 
205 
206 
207 bool cBlockEntity::Tick(const std::chrono::milliseconds a_Dt, cChunk & a_Chunk)
208 {
209  UNUSED(a_Dt);
210  return false;
211 }
BrewingstandEntity.h
E_BLOCK_NOTE_BLOCK
@ E_BLOCK_NOTE_BLOCK
Definition: BlockType.h:35
MobSpawnerEntity.h
cChunkDef
Constants used throughout the code, useful typedefs and utility functions.
Definition: ChunkDef.h:102
JukeboxEntity.h
E_BLOCK_ENDER_CHEST
@ E_BLOCK_ENDER_CHEST
Definition: BlockType.h:145
cBlockEntity::m_BlockMeta
NIBBLETYPE m_BlockMeta
The block meta representing this particular instance in the world Mainly used for directional entitie...
Definition: BlockEntity.h:124
HopperEntity.h
E_BLOCK_FURNACE
@ E_BLOCK_FURNACE
Definition: BlockType.h:73
E_BLOCK_WALL_BANNER
@ E_BLOCK_WALL_BANNER
Definition: BlockType.h:196
cBlockEntity::cBlockEntity
cBlockEntity(BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos, cWorld *a_World)
Definition: BlockEntity.cpp:32
SignEntity.h
E_BLOCK_LIT_FURNACE
@ E_BLOCK_LIT_FURNACE
Definition: BlockType.h:74
cBlockEntity::CreateByBlockType
static OwnedBlockEntity 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:77
E_BLOCK_COMMAND_BLOCK
@ E_BLOCK_COMMAND_BLOCK
Definition: BlockType.h:152
cBlockEntity::OnAddToWorld
virtual void OnAddToWorld(cWorld &a_World, cChunk &a_Chunk)
Called when the block entity object is added to a world.
Definition: BlockEntity.cpp:171
Globals.h
E_BLOCK_WALLSIGN
@ E_BLOCK_WALLSIGN
Definition: BlockType.h:82
cBlockEntity::IsBlockEntityBlockType
static bool IsBlockEntityBlockType(BLOCKTYPE a_BlockType)
Returns true if the specified blocktype is supposed to have an associated block entity.
Definition: BlockEntity.cpp:130
ASSERT
#define ASSERT(x)
Definition: Globals.h:273
E_BLOCK_BREWING_STAND
@ E_BLOCK_BREWING_STAND
Definition: BlockType.h:132
E_BLOCK_SIGN_POST
@ E_BLOCK_SIGN_POST
Definition: BlockType.h:76
NIBBLETYPE
unsigned char NIBBLETYPE
The datatype used by nibbledata (meta, light, skylight)
Definition: ChunkDef.h:44
E_BLOCK_BED
@ E_BLOCK_BED
Definition: BlockType.h:36
LOGD
#define LOGD
Definition: LoggerSimple.h:83
cBlockEntity::Tick
virtual bool Tick(std::chrono::milliseconds a_Dt, cChunk &a_Chunk)
Ticks the entity; returns true if the chunk should be marked as dirty as a result of this ticking.
Definition: BlockEntity.cpp:207
E_BLOCK_TRAPPED_CHEST
@ E_BLOCK_TRAPPED_CHEST
Definition: BlockType.h:161
E_BLOCK_FLOWER_POT
@ E_BLOCK_FLOWER_POT
Definition: BlockType.h:155
E_BLOCK_JUKEBOX
@ E_BLOCK_JUKEBOX
Definition: BlockType.h:99
cBlockEntity::OnRemoveFromWorld
virtual void OnRemoveFromWorld()
Called when the block entity object is removed from a world.
Definition: BlockEntity.cpp:180
cBlockEntity::SetPos
void SetPos(Vector3i a_NewPos)
Updates the internally stored position.
Definition: BlockEntity.cpp:188
cBlockEntity::Clone
OwnedBlockEntity Clone(Vector3i a_Pos)
Makes an exact copy of this block entity, except for its m_World (set to nullptr),...
Definition: BlockEntity.cpp:46
cWorld
Definition: World.h:47
E_BLOCK_STANDING_BANNER
@ E_BLOCK_STANDING_BANNER
Definition: BlockType.h:195
FurnaceEntity.h
BeaconEntity.h
DispenserEntity.h
E_BLOCK_HOPPER
@ E_BLOCK_HOPPER
Definition: BlockType.h:171
E_BLOCK_MOB_SPAWNER
@ E_BLOCK_MOB_SPAWNER
Definition: BlockType.h:62
cBlockEntity::m_BlockType
BLOCKTYPE m_BlockType
The blocktype representing this particular instance in the world.
Definition: BlockEntity.h:120
ItemTypeToString
AString ItemTypeToString(short a_ItemType)
Translates itemtype into a string.
Definition: BlockType.cpp:252
OwnedBlockEntity
std::unique_ptr< cBlockEntity > OwnedBlockEntity
Definition: BlockEntity.h:16
FlowerPotEntity.h
cBlockEntity::ConvertToPickups
virtual cItems ConvertToPickups() const
Returns the contents of this block entity that it would drop if broken.
Definition: BlockEntity.cpp:57
BedEntity.h
DropperEntity.h
cItems
This class bridges a vector of cItem for safe access via Lua.
Definition: Item.h:213
CommandBlockEntity.h
E_BLOCK_END_PORTAL
@ E_BLOCK_END_PORTAL
Definition: BlockType.h:134
EndPortalEntity.h
BlockEntity.h
cBlockEntity::Destroy
virtual void Destroy()
Called when this block entity's associated block is destroyed.
Definition: BlockEntity.cpp:122
E_BLOCK_DISPENSER
@ E_BLOCK_DISPENSER
Definition: BlockType.h:33
BLOCKTYPE
unsigned char BLOCKTYPE
The datatype used by blockdata.
Definition: ChunkDef.h:41
ChestEntity.h
E_BLOCK_HEAD
@ E_BLOCK_HEAD
Definition: BlockType.h:159
cChunk
Definition: Chunk.h:35
EnderChestEntity.h
FAST_FLOOR_DIV
#define FAST_FLOOR_DIV(x, div)
Faster than (int)floorf((float)x / (float)div)
Definition: Globals.h:235
cBlockEntity::m_Pos
Vector3i m_Pos
Position in absolute block coordinates.
Definition: BlockEntity.h:113
UNUSED
#define UNUSED
Definition: Globals.h:72
cBlockEntity::CopyFrom
virtual void CopyFrom(const cBlockEntity &a_Src)
Copies all properties of a_Src into this entity, except for its m_World and location.
Definition: BlockEntity.cpp:66
E_BLOCK_BEACON
@ E_BLOCK_BEACON
Definition: BlockType.h:153
E_BLOCK_CHEST
@ E_BLOCK_CHEST
Definition: BlockType.h:64
NoteEntity.h
Vector3< int >
E_BLOCK_ENCHANTMENT_TABLE
@ E_BLOCK_ENCHANTMENT_TABLE
Definition: BlockType.h:131
MobHeadEntity.h
cBlockEntity::m_World
cWorld * m_World
Definition: BlockEntity.h:126
E_BLOCK_DROPPER
@ E_BLOCK_DROPPER
Definition: BlockType.h:176
cBlockEntity
Definition: BlockEntity.h:24
EnchantingTableEntity.h
cBlockEntity::SetWorld
void SetWorld(cWorld *a_World)
Definition: BlockEntity.cpp:198
BannerEntity.h