Cuberite
A lightweight, fast and extensible game server for Minecraft
BlockEntityWithItems.cpp
Go to the documentation of this file.
1 // BlockEntityWithItems.cpp
2 
3 #include "Globals.h"
4 #include "BlockEntityWithItems.h"
5 #include "../Simulator/RedstoneSimulator.h"
6 
7 
8 
9 
10 
12  BLOCKTYPE a_BlockType,
13  NIBBLETYPE a_BlockMeta,
14  Vector3i a_Pos,
15  int a_ItemGridWidth, int a_ItemGridHeight,
16  cWorld * a_World
17 ):
18  super(a_BlockType, a_BlockMeta, a_Pos, a_World),
20  m_Contents(a_ItemGridWidth, a_ItemGridHeight)
21 {
22  m_Contents.AddListener(*this);
23 }
24 
25 
26 
27 
28 
30 {
31  // Drop the contents as pickups:
32  ASSERT(m_World != nullptr);
33  cItems Pickups;
34  m_Contents.CopyToItems(Pickups);
35  m_Contents.Clear();
36  m_World->SpawnItemPickups(Pickups, m_Pos.x + 0.5, m_Pos.y + 0.5, m_Pos.z + 0.5); // Spawn in centre of block
37 }
38 
39 
40 
41 
42 
44 {
45  super::CopyFrom(a_Src);
46  auto & src = static_cast<const cBlockEntityWithItems &>(a_Src);
47  m_Contents.CopyFrom(src.m_Contents);
48 }
49 
50 
51 
52 
53 
54 void cBlockEntityWithItems::OnSlotChanged(cItemGrid * a_Grid, int a_SlotNum)
55 {
56  UNUSED(a_SlotNum);
57  ASSERT(a_Grid == &m_Contents);
58  if (m_World != nullptr)
59  {
60  if (GetWindow() != nullptr)
61  {
63  }
64 
66  m_World->DoWithChunkAt(m_Pos, [&](cChunk & a_Chunk)
67  {
69  return true;
70  }
71  );
72  }
73 }
T x
Definition: Vector3.h:17
int GetChunkZ() const
Definition: BlockEntity.h:116
cBlockEntityWithItems(BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos, int a_ItemGridWidth, int a_ItemGridHeight, cWorld *a_World)
void CopyFrom(const cItemGrid &a_Src)
Copies all items from a_Src to this grid.
Definition: ItemGrid.cpp:83
unsigned char BLOCKTYPE
The datatype used by blockdata.
Definition: ChunkDef.h:42
bool DoWithChunkAt(Vector3i a_BlockPos, cChunkCallback a_Callback)
Calls the callback for the chunk at the block position specified, with ChunkMapCS locked...
Definition: World.cpp:1567
virtual void CopyFrom(const cBlockEntity &a_Src) override
Copies all properties of a_Src into this entity, except for its m_World and location.
void CopyToItems(cItems &a_Items) const
Copies the contents into a cItems object; preserves the original a_Items contents.
Definition: ItemGrid.cpp:679
unsigned char NIBBLETYPE
The datatype used by nibbledata (meta, light, skylight)
Definition: ChunkDef.h:45
virtual void Destroy(void) override
void Clear(void)
Sets all items as empty.
Definition: ItemGrid.cpp:228
Definition: Chunk.h:49
void SpawnItemPickups(const cItems &a_Pickups, Vector3i a_BlockPos, double a_FlyAwaySpeed=1.0, bool a_IsPlayerCreated=false)
Spawns item pickups for each item in the list.
Definition: World.cpp:1936
T y
Definition: Vector3.h:17
cWorld * m_World
Definition: BlockEntity.h:155
void AddListener(cListener &a_Listener)
Adds a callback that gets called whenever a slot changes.
Definition: ItemGrid.cpp:785
T z
Definition: Vector3.h:17
int GetChunkX() const
Definition: BlockEntity.h:115
void MarkChunkDirty(int a_ChunkX, int a_ChunkZ)
Definition: World.cpp:2260
cWindow * GetWindow(void) const
Definition: WindowOwner.h:40
Definition: World.h:65
void WakeUp(Vector3i a_Block, cChunk *a_Chunk)
Called when a block changes.
Definition: Simulator.cpp:21
#define ASSERT(x)
Definition: Globals.h:335
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.
#define UNUSED
Definition: Globals.h:152
Window owner that is associated with a block entity (chest, furnace, ...)
Definition: WindowOwner.h:57
virtual void OnSlotChanged(cItemGrid *a_Grid, int a_SlotNum) override
Called whenever a slot changes.
cRedstoneSimulator * GetRedstoneSimulator(void)
Definition: World.h:717
This class bridges a vector of cItem for safe access via Lua.
Definition: Item.h:234
void BroadcastWholeWindow(void)
Sends the contents of the whole window to all clients of this window.
Definition: Window.cpp:738