Cuberite
A lightweight, fast and extensible game server for Minecraft
ChestEntity.h
Go to the documentation of this file.
1 
2 #pragma once
3 
4 #include "BlockEntityWithItems.h"
5 #include "../Simulator/RedstoneSimulator.h"
6 
7 
8 
9 
10 
11 class cClientHandle;
12 
13 
14 
15 
16 
17 // tolua_begin
18 class cChestEntity :
20 {
21  // tolua_end
22 
24 
25  // tolua_begin
26 
27 public:
28 
29  enum
30  {
33  } ;
34 
35  // tolua_end
36 
38 
39 
40  cChestEntity(BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos, cWorld * a_World);
41 
42  virtual ~cChestEntity() override;
43 
44  // cBlockEntity overrides:
45  virtual void CopyFrom(const cBlockEntity & a_Src) override;
46  virtual void SendTo(cClientHandle & a_Client) override;
47  virtual bool UsedBy(cPlayer * a_Player) override;
48 
50  void ScanNeighbours();
51 
53  void OpenNewWindow();
54 
56  void DestroyWindow();
57 
59  bool IsBlocked();
60 
62  int GetNumberOfPlayers(void) const { return m_NumActivePlayers; }
63 
65  void SetNumberOfPlayers(int a_NumActivePlayers) { m_NumActivePlayers = a_NumActivePlayers; }
66 
67 
68 private:
69 
72 
75 
77  virtual void OnSlotChanged(cItemGrid * a_Grid, int a_SlotNum) override
78  {
79  UNUSED(a_SlotNum);
80  ASSERT(a_Grid == &m_Contents);
81  if (m_World != nullptr)
82  {
83  cWindow * Window = GetWindow();
84  if (
85  (Window == nullptr) &&
86  (m_Neighbour != nullptr)
87  )
88  {
89  // Neighbour might own the window
90  Window = m_Neighbour->GetWindow();
91  }
92 
93  if (Window != nullptr)
94  {
95  Window->BroadcastWholeWindow();
96  }
97 
99  m_World->DoWithChunkAt(m_Pos, [&](cChunk & a_Chunk)
100  {
101  m_World->GetRedstoneSimulator()->WakeUp(m_Pos, &a_Chunk);
102  return true;
103  }
104  );
105  }
106  }
107 
108 } ; // tolua_export
109 
110 
111 
112 
int m_NumActivePlayers
Number of players who currently have this chest open.
Definition: ChestEntity.h:71
int GetChunkZ() const
Definition: BlockEntity.h:116
void DestroyWindow()
Forces any players to close the owned window.
cBlockEntityWithItems(BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos, int a_ItemGridWidth, int a_ItemGridHeight, cWorld *a_World)
cChestEntity * m_Neighbour
Neighbouring chest that links to form a double chest.
Definition: ChestEntity.h:74
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
Definition: Player.h:27
void SetNumberOfPlayers(int a_NumActivePlayers)
Sets the number of players who currently have this chest open.
Definition: ChestEntity.h:65
unsigned char NIBBLETYPE
The datatype used by nibbledata (meta, light, skylight)
Definition: ChunkDef.h:45
Definition: Chunk.h:49
virtual void CopyFrom(const cBlockEntity &a_Src) override
Copies all properties of a_Src into this entity, except for its m_World and location.
Definition: ChestEntity.cpp:50
int GetNumberOfPlayers(void) const
Gets the number of players who currently have this chest open.
Definition: ChestEntity.h:62
cWorld * m_World
Definition: BlockEntity.h:155
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
virtual void OnSlotChanged(cItemGrid *a_Grid, int a_SlotNum) override
cItemGrid::cListener overrides:
Definition: ChestEntity.h:77
void OpenNewWindow()
Opens a new chest window where this is the primary chest and any neighbour is the secondary...
Definition: World.h:65
void ScanNeighbours()
Search horizontally adjacent blocks for neighbouring chests of the same type and links them together...
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
#define UNUSED
Definition: Globals.h:152
#define BLOCKENTITY_PROTODEF(classname)
Place this macro in the declaration of each cBlockEntity descendant.
Definition: BlockEntity.h:9
Represents a UI window.
Definition: Window.h:53
virtual bool UsedBy(cPlayer *a_Player) override
Called when a player uses this entity; should open the UI window.
Definition: ChestEntity.cpp:75
cRedstoneSimulator * GetRedstoneSimulator(void)
Definition: World.h:717
bool IsBlocked()
Returns true if the chest should not be accessible by players.
virtual void SendTo(cClientHandle &a_Client) override
Sends the packet defining the block entity to the client specified.
Definition: ChestEntity.cpp:65
void BroadcastWholeWindow(void)
Sends the contents of the whole window to all clients of this window.
Definition: Window.cpp:738