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 }
AString ItemTypeToString(short a_ItemType)
Translates itemtype into a string.
Definition: BlockType.cpp:252
@ E_BLOCK_HEAD
Definition: BlockType.h:159
@ E_BLOCK_STANDING_BANNER
Definition: BlockType.h:195
@ E_BLOCK_BREWING_STAND
Definition: BlockType.h:132
@ E_BLOCK_JUKEBOX
Definition: BlockType.h:99
@ E_BLOCK_ENCHANTMENT_TABLE
Definition: BlockType.h:131
@ E_BLOCK_FURNACE
Definition: BlockType.h:73
@ E_BLOCK_SIGN_POST
Definition: BlockType.h:76
@ E_BLOCK_BEACON
Definition: BlockType.h:153
@ E_BLOCK_CHEST
Definition: BlockType.h:64
@ E_BLOCK_LIT_FURNACE
Definition: BlockType.h:74
@ E_BLOCK_TRAPPED_CHEST
Definition: BlockType.h:161
@ E_BLOCK_COMMAND_BLOCK
Definition: BlockType.h:152
@ E_BLOCK_NOTE_BLOCK
Definition: BlockType.h:35
@ E_BLOCK_BED
Definition: BlockType.h:36
@ E_BLOCK_HOPPER
Definition: BlockType.h:171
@ E_BLOCK_DROPPER
Definition: BlockType.h:176
@ E_BLOCK_ENDER_CHEST
Definition: BlockType.h:145
@ E_BLOCK_END_PORTAL
Definition: BlockType.h:134
@ E_BLOCK_DISPENSER
Definition: BlockType.h:33
@ E_BLOCK_FLOWER_POT
Definition: BlockType.h:155
@ E_BLOCK_MOB_SPAWNER
Definition: BlockType.h:62
@ E_BLOCK_WALLSIGN
Definition: BlockType.h:82
@ E_BLOCK_WALL_BANNER
Definition: BlockType.h:196
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
#define FAST_FLOOR_DIV(x, div)
Faster than (int)floorf((float)x / (float)div)
Definition: Globals.h:238
#define ASSERT(x)
Definition: Globals.h:276
#define UNUSED
Definition: Globals.h:72
#define LOGD
Definition: LoggerSimple.h:83
BLOCKTYPE m_BlockType
The blocktype representing this particular instance in the world.
Definition: BlockEntity.h:120
NIBBLETYPE m_BlockMeta
The block meta representing this particular instance in the world Mainly used for directional entitie...
Definition: BlockEntity.h:124
virtual void Destroy()
Called when this block entity's associated block is destroyed.
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.
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
void SetWorld(cWorld *a_World)
virtual cItems ConvertToPickups() const
Returns the contents of this block entity that it would drop if broken.
Definition: BlockEntity.cpp:57
void SetPos(Vector3i a_NewPos)
Updates the internally stored position.
virtual void OnRemoveFromWorld()
Called when the block entity object is removed from a world.
cBlockEntity(BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos, cWorld *a_World)
Definition: BlockEntity.cpp:32
Vector3i m_Pos
Position in absolute block coordinates.
Definition: BlockEntity.h:113
static bool IsBlockEntityBlockType(BLOCKTYPE a_BlockType)
Returns true if the specified blocktype is supposed to have an associated block entity.
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
virtual void OnAddToWorld(cWorld &a_World, cChunk &a_Chunk)
Called when the block entity object is added to a world.
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 * m_World
Definition: BlockEntity.h:126
Definition: Chunk.h:36
Constants used throughout the code, useful typedefs and utility functions.
Definition: ChunkDef.h:120
This class bridges a vector of cItem for safe access via Lua.
Definition: Item.h:215
Definition: World.h:53
std::unique_ptr< cBlockEntity > OwnedBlockEntity
Definition: BlockEntity.h:16