Cuberite
A lightweight, fast and extensible game server for Minecraft
EnderChestEntity.cpp
Go to the documentation of this file.
1 
2 #include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
3 
4 #include "EnderChestEntity.h"
5 #include "json/json.h"
6 #include "../Item.h"
7 #include "../Entities/Player.h"
8 #include "../UI/EnderChestWindow.h"
9 #include "../ClientHandle.h"
10 #include "../Mobs/Ocelot.h"
11 
12 
13 
14 
15 
16 cEnderChestEntity::cEnderChestEntity(BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos, cWorld * a_World):
17  super(a_BlockType, a_BlockMeta, a_Pos, a_World),
19 {
20  ASSERT(a_BlockType == E_BLOCK_ENDER_CHEST);
21 }
22 
23 
24 
25 
26 
28 {
29  cWindow * Window = GetWindow();
30  if (Window != nullptr)
31  {
32  Window->OwnerDestroyed();
33  }
34 }
35 
36 
37 
38 
39 
41 {
42  // Send a dummy "number of players with chest open" packet to make the chest visible:
43  a_Client.SendBlockAction(m_Pos.x, m_Pos.y, m_Pos.z, 1, 0, m_BlockType);
44 }
45 
46 
47 
48 
49 
51 {
52  if (
53  (GetPosY() < cChunkDef::Height - 1) &&
54  (
57  )
58  )
59  {
60  // Obstruction, don't open
61  return false;
62  }
63  // If the window is not created, open it anew:
64  cWindow * Window = GetWindow();
65  if (Window == nullptr)
66  {
67  OpenNewWindow();
68  Window = GetWindow();
69  }
70 
71  // Open the window for the player:
72  if (Window != nullptr)
73  {
74  if (a_Player->GetWindow() != Window)
75  {
76  a_Player->OpenWindow(*Window);
77  }
78  }
79  return true;
80 }
81 
82 
83 
84 
85 
87 {
88  OpenWindow(new cEnderChestWindow(this));
89 }
90 
91 
92 
93 
94 
95 void cEnderChestEntity::LoadFromJson(const Json::Value & a_Value, cItemGrid & a_Grid)
96 {
97  int SlotIdx = 0;
98  for (auto & Node : a_Value)
99  {
100  cItem Item;
101  Item.FromJson(Node);
102  a_Grid.SetSlot(SlotIdx, Item);
103  SlotIdx++;
104  }
105 }
106 
107 
108 
109 
110 
111 void cEnderChestEntity::SaveToJson(Json::Value & a_Value, const cItemGrid & a_Grid)
112 {
113  for (int i = 0; i < a_Grid.GetNumSlots(); i++)
114  {
115  Json::Value Slot;
116  a_Grid.GetSlot(i).GetJson(Slot);
117  a_Value.append(Slot);
118  }
119 }
T x
Definition: Vector3.h:17
static void LoadFromJson(const Json::Value &a_Value, cItemGrid &a_Grid)
cWorld * GetWorld() const
Definition: BlockEntity.h:113
int GetPosY() const
Definition: BlockEntity.h:106
unsigned char BLOCKTYPE
The datatype used by blockdata.
Definition: ChunkDef.h:42
cWindow * GetWindow(void)
Definition: Player.h:239
static bool IsCatSittingOnBlock(cWorld *a_World, Vector3d a_BlockPosition)
Returns true if there&#39;s a cat sitting above the given position.
Definition: Ocelot.cpp:197
Definition: Player.h:27
virtual void SendTo(cClientHandle &a_Client) override
Sends the packet defining the block entity to the client specified.
void OpenWindow(cWindow *a_Window)
Definition: WindowOwner.h:34
void OwnerDestroyed(void)
Definition: Window.cpp:352
int GetPosX() const
Definition: BlockEntity.h:105
BLOCKTYPE m_BlockType
The blocktype representing this particular instance in the world.
Definition: BlockEntity.h:149
int GetPosZ() const
Definition: BlockEntity.h:107
cEnderChestEntity(BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos, cWorld *a_World)
unsigned char NIBBLETYPE
The datatype used by nibbledata (meta, light, skylight)
Definition: ChunkDef.h:45
static void SaveToJson(Json::Value &a_Value, const cItemGrid &a_Grid)
void SendBlockAction(int a_BlockX, int a_BlockY, int a_BlockZ, char a_Byte1, char a_Byte2, BLOCKTYPE a_BlockType)
T y
Definition: Vector3.h:17
T z
Definition: Vector3.h:17
static const int Height
Definition: ChunkDef.h:135
void OpenNewWindow(void)
Opens a new enderchest window for this enderchest.
virtual ~cEnderChestEntity() override
cWindow * GetWindow(void) const
Definition: WindowOwner.h:40
Vector3i GetPos() const
Definition: BlockEntity.h:104
Definition: World.h:65
Vector3< double > Vector3d
Definition: Vector3.h:445
#define ASSERT(x)
Definition: Globals.h:335
Vector3i m_Pos
Position in absolute block coordinates.
Definition: BlockEntity.h:142
Window owner that is associated with a block entity (chest, furnace, ...)
Definition: WindowOwner.h:57
static bool IsTransparent(BLOCKTYPE a_Type)
Definition: BlockInfo.h:32
virtual bool UsedBy(cPlayer *a_Player) override
Called when a player uses this entity; should open the UI window.
static int GetBlock(lua_State *a_LuaState)
Templated bindings for the GetBlock___() functions.
void GetJson(Json::Value &a_OutValue) const
Saves the item data into JSON representation.
Definition: Item.cpp:146
Represents a UI window.
Definition: Window.h:53
const cItem & GetSlot(int a_X, int a_Y) const
Definition: ItemGrid.cpp:96
int GetNumSlots(void) const
Definition: ItemGrid.h:40
void FromJson(const Json::Value &a_Value)
Loads the item data from JSON representation.
Definition: Item.cpp:197
Definition: Item.h:36
void OpenWindow(cWindow &a_Window)
Opens the specified window; closes the current one first using CloseWindow()
Definition: Player.cpp:1349
void SetSlot(int a_X, int a_Y, const cItem &a_Item)
Definition: ItemGrid.cpp:121