Cuberite
A lightweight, fast and extensible game server for Minecraft
BlockEntity.h
Go to the documentation of this file.
1 
2 #pragma once
3 
4 #include "ChunkDef.h"
5 
6 
7 
8 
9 
10 class cChunk;
11 class cItems;
12 class cPlayer;
13 class cWorld;
14 class cBlockEntity;
15 
16 using OwnedBlockEntity = std::unique_ptr<cBlockEntity>;
17 using cBlockEntities = std::unordered_map<size_t, OwnedBlockEntity>;
18 
19 
20 
21 
22 
23 // tolua_begin
25 {
26 protected:
27 
28  cBlockEntity(BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos, cWorld * a_World);
29 
30 public:
31 
32  // tolua_end
33 
34  virtual ~cBlockEntity() = default; // force a virtual destructor in all descendants
35 
39 
42  virtual cItems ConvertToPickups() const;
43 
47  virtual void CopyFrom(const cBlockEntity & a_Src);
48 
52  static OwnedBlockEntity CreateByBlockType(BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos, cWorld * a_World = nullptr);
53 
54  virtual void Destroy();
55 
57  static bool IsBlockEntityBlockType(BLOCKTYPE a_BlockType);
58 
60  virtual void OnRemoveFromWorld();
61 
64  virtual void SendTo(cClientHandle & a_Client) = 0;
65 
69  void SetPos(Vector3i a_NewPos);
70 
71  void SetWorld(cWorld * a_World);
72 
74  virtual bool Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk);
75 
78  virtual bool UsedBy(cPlayer * a_Player) = 0;
79 
80  // tolua_begin
81 
82  // Position, in absolute block coordinates:
83  Vector3i GetPos() const { return m_Pos; }
84  int GetPosX() const { return m_Pos.x; }
85  int GetPosY() const { return m_Pos.y; }
86  int GetPosZ() const { return m_Pos.z; }
87 
88  Vector3i GetRelPos() const { return Vector3i(m_RelX, m_Pos.y, m_RelZ); }
89 
90  BLOCKTYPE GetBlockType() const { return m_BlockType; }
91 
92  cWorld * GetWorld() const { return m_World; }
93 
94  int GetChunkX() const { return FAST_FLOOR_DIV(m_Pos.x, cChunkDef::Width); }
95  int GetChunkZ() const { return FAST_FLOOR_DIV(m_Pos.z, cChunkDef::Width); }
96 
97  int GetRelX() const { return m_RelX; }
98  int GetRelZ() const { return m_RelZ; }
99 
100  // tolua_end
101 
102 
103 protected:
104 
107 
110 
114 
118 
120 } ; // tolua_export
std::unordered_map< size_t, OwnedBlockEntity > cBlockEntities
Definition: BlockEntity.h:17
virtual void OnRemoveFromWorld()
Called when the block entity is removed from a world.
int GetPosX() const
Definition: BlockEntity.h:84
virtual void SendTo(cClientHandle &a_Client)=0
Sends the packet defining the block entity to the client specified.
Definition: Chunk.h:48
int GetRelZ() const
Definition: BlockEntity.h:98
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:76
BLOCKTYPE m_BlockType
The blocktype representing this particular instance in the world.
Definition: BlockEntity.h:113
Vector3< int > Vector3i
Definition: Vector3.h:487
T z
Definition: Vector3.h:17
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...
virtual void Destroy()
void SetPos(Vector3i a_NewPos)
Updates the internally stored position.
void SetWorld(cWorld *a_World)
Definition: Player.h:27
unsigned char NIBBLETYPE
The datatype used by nibbledata (meta, light, skylight)
Definition: ChunkDef.h:44
Vector3i GetPos() const
Definition: BlockEntity.h:83
int GetPosY() const
Definition: BlockEntity.h:85
virtual bool UsedBy(cPlayer *a_Player)=0
Called when a player uses this entity; should open the UI window.
Definition: World.h:65
T x
Definition: Vector3.h:17
This class bridges a vector of cItem for safe access via Lua.
Definition: Item.h:213
std::unique_ptr< cBlockEntity > OwnedBlockEntity
Definition: BlockEntity.h:16
int GetChunkX() const
Definition: BlockEntity.h:94
unsigned char BLOCKTYPE
The datatype used by blockdata.
Definition: ChunkDef.h:41
Vector3i m_Pos
Position in absolute block coordinates.
Definition: BlockEntity.h:106
T y
Definition: Vector3.h:17
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:65
int m_RelX
Position relative to the chunk, used to speed up ticking.
Definition: BlockEntity.h:109
int GetPosZ() const
Definition: BlockEntity.h:86
int GetChunkZ() const
Definition: BlockEntity.h:95
cWorld * m_World
Definition: BlockEntity.h:119
Vector3i GetRelPos() const
Definition: BlockEntity.h:88
NIBBLETYPE m_BlockMeta
The block meta representing this particular instance in the world Mainly used for directional entitie...
Definition: BlockEntity.h:117
virtual ~cBlockEntity()=default
#define FAST_FLOOR_DIV(x, div)
Faster than (int)floorf((float)x / (float)div)
Definition: Globals.h:256
virtual cItems ConvertToPickups() const
Returns the contents of this block entity that it would drop if broken.
Definition: BlockEntity.cpp:56
BLOCKTYPE GetBlockType() const
Definition: BlockEntity.h:90
static const int Width
Definition: ChunkDef.h:133
OwnedBlockEntity 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: BlockEntity.cpp:45
cWorld * GetWorld() const
Definition: BlockEntity.h:92
cBlockEntity(BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos, cWorld *a_World)
Definition: BlockEntity.cpp:31
static bool IsBlockEntityBlockType(BLOCKTYPE a_BlockType)
Returns true if the specified blocktype is supposed to have an associated block entity.
int GetRelX() const
Definition: BlockEntity.h:97