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 
56  virtual void Destroy();
57 
59  static bool IsBlockEntityBlockType(BLOCKTYPE a_BlockType);
60 
62  virtual void OnAddToWorld(cWorld & a_World, cChunk & a_Chunk);
63 
67  virtual void OnRemoveFromWorld();
68 
71  virtual void SendTo(cClientHandle & a_Client) = 0;
72 
76  void SetPos(Vector3i a_NewPos);
77 
78  void SetWorld(cWorld * a_World);
79 
81  virtual bool Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk);
82 
85  virtual bool UsedBy(cPlayer * a_Player) = 0;
86 
87  // tolua_begin
88 
89  // Position, in absolute block coordinates:
90  Vector3i GetPos() const { return m_Pos; }
91  int GetPosX() const { return m_Pos.x; }
92  int GetPosY() const { return m_Pos.y; }
93  int GetPosZ() const { return m_Pos.z; }
94 
95  Vector3i GetRelPos() const { return Vector3i(m_RelX, m_Pos.y, m_RelZ); }
96 
97  BLOCKTYPE GetBlockType() const { return m_BlockType; }
98 
99  cWorld * GetWorld() const { return m_World; }
100 
101  int GetChunkX() const { return FAST_FLOOR_DIV(m_Pos.x, cChunkDef::Width); }
102  int GetChunkZ() const { return FAST_FLOOR_DIV(m_Pos.z, cChunkDef::Width); }
103 
104  int GetRelX() const { return m_RelX; }
105  int GetRelZ() const { return m_RelZ; }
106 
107  // tolua_end
108 
109 
110 protected:
111 
114 
117 
121 
125 
127 } ; // tolua_export
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
Vector3< int > Vector3i
Definition: Vector3.h:487
virtual void SendTo(cClientHandle &a_Client)=0
Sends the packet defining the block entity to the client specified.
int m_RelX
Position relative to the chunk, used to speed up ticking.
Definition: BlockEntity.h:116
int GetChunkX() const
Definition: BlockEntity.h:101
int GetChunkZ() const
Definition: BlockEntity.h:102
BLOCKTYPE m_BlockType
The blocktype representing this particular instance in the world.
Definition: BlockEntity.h:120
int GetRelZ() const
Definition: BlockEntity.h:105
cWorld * GetWorld() const
Definition: BlockEntity.h:99
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
int GetPosZ() const
Definition: BlockEntity.h:93
void SetWorld(cWorld *a_World)
Vector3i GetPos() const
Definition: BlockEntity.h:90
int GetPosY() const
Definition: BlockEntity.h:92
virtual cItems ConvertToPickups() const
Returns the contents of this block entity that it would drop if broken.
Definition: BlockEntity.cpp:57
int GetRelX() const
Definition: BlockEntity.h:104
virtual ~cBlockEntity()=default
void SetPos(Vector3i a_NewPos)
Updates the internally stored position.
virtual void OnRemoveFromWorld()
Called when the block entity object is removed from a world.
int GetPosX() const
Definition: BlockEntity.h:91
cBlockEntity(BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos, cWorld *a_World)
Definition: BlockEntity.cpp:32
Vector3i GetRelPos() const
Definition: BlockEntity.h:95
Vector3i m_Pos
Position in absolute block coordinates.
Definition: BlockEntity.h:113
virtual bool UsedBy(cPlayer *a_Player)=0
Called when a player uses this entity; should open the UI window.
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
BLOCKTYPE GetBlockType() const
Definition: BlockEntity.h:97
cWorld * m_World
Definition: BlockEntity.h:126
Definition: Chunk.h:36
static const int Width
Definition: ChunkDef.h:124
Definition: Player.h:29
This class bridges a vector of cItem for safe access via Lua.
Definition: Item.h:215
T x
Definition: Vector3.h:17
T y
Definition: Vector3.h:17
T z
Definition: Vector3.h:17
Definition: World.h:53
std::unordered_map< size_t, OwnedBlockEntity > cBlockEntities
Definition: BlockEntity.h:17
std::unique_ptr< cBlockEntity > OwnedBlockEntity
Definition: BlockEntity.h:16