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 "../BlockInfo.h"
7 #include "../Item.h"
8 #include "../Entities/Player.h"
9 #include "../UI/EnderChestWindow.h"
10 #include "../ClientHandle.h"
11 #include "../Mobs/Ocelot.h"
12 
13 
14 
15 
16 
17 cEnderChestEntity::cEnderChestEntity(BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos, cWorld * a_World):
18  Super(a_BlockType, a_BlockMeta, a_Pos, a_World),
20 {
21  ASSERT(a_BlockType == E_BLOCK_ENDER_CHEST);
22 }
23 
24 
25 
26 
27 
29 {
30  // Send a dummy "number of players with chest open" packet to make the chest visible:
31  a_Client.SendBlockAction(m_Pos, 1, 0, m_BlockType);
32 }
33 
34 
35 
36 
37 
39 {
40  const auto Window = GetWindow();
41  if (Window != nullptr)
42  {
43  Window->OwnerDestroyed();
44  }
45 }
46 
47 
48 
49 
50 
52 {
53  if (
54  (GetPosY() < cChunkDef::Height - 1) &&
55  (
58  )
59  )
60  {
61  // Obstruction, don't open
62  return false;
63  }
64 
66 
67  // If the window is not created, open it anew:
68  cWindow * Window = GetWindow();
69  if (Window == nullptr)
70  {
71  OpenNewWindow();
72  Window = GetWindow();
73  }
74 
75  // Open the window for the player:
76  if (Window != nullptr)
77  {
78  if (a_Player->GetWindow() != Window)
79  {
80  a_Player->OpenWindow(*Window);
81  }
82  }
83  return true;
84 }
85 
86 
87 
88 
89 
91 {
92  OpenWindow(new cEnderChestWindow(this));
93 }
94 
95 
96 
97 
98 
99 void cEnderChestEntity::LoadFromJson(const Json::Value & a_Value, cItemGrid & a_Grid)
100 {
101  int SlotIdx = 0;
102  for (auto & Node : a_Value)
103  {
104  cItem Item;
105  Item.FromJson(Node);
106  a_Grid.SetSlot(SlotIdx, Item);
107  SlotIdx++;
108  }
109 }
110 
111 
112 
113 
114 
115 void cEnderChestEntity::SaveToJson(Json::Value & a_Value, const cItemGrid & a_Grid)
116 {
117  for (int i = 0; i < a_Grid.GetNumSlots(); i++)
118  {
119  Json::Value Slot;
120  a_Grid.GetSlot(i).GetJson(Slot);
121  a_Value.append(Slot);
122  }
123 }
static int GetBlock(lua_State *a_LuaState)
Templated bindings for the GetBlock___() functions.
@ E_BLOCK_ENDER_CHEST
Definition: BlockType.h:145
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 ASSERT(x)
Definition: Globals.h:276
Item
Definition: Items.h:4
Vector3< double > Vector3d
Definition: Vector3.h:485
BLOCKTYPE m_BlockType
The blocktype representing this particular instance in the world.
Definition: BlockEntity.h:120
cWorld * GetWorld() const
Definition: BlockEntity.h:99
Vector3i GetPos() const
Definition: BlockEntity.h:90
int GetPosY() const
Definition: BlockEntity.h:92
Vector3i m_Pos
Position in absolute block coordinates.
Definition: BlockEntity.h:113
cEnderChestEntity(BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos, cWorld *a_World)
virtual bool UsedBy(cPlayer *a_Player) override
Called when a player uses this entity; should open the UI window.
virtual void SendTo(cClientHandle &a_Client) override
Sends the packet defining the block entity to the client specified.
static void LoadFromJson(const Json::Value &a_Value, cItemGrid &a_Grid)
void OpenNewWindow(void)
Opens a new enderchest window for this enderchest.
static void SaveToJson(Json::Value &a_Value, const cItemGrid &a_Grid)
virtual void OnRemoveFromWorld() override
Called when the block entity object is removed from a world.
static bool IsTransparent(BLOCKTYPE Block)
Is a block transparent? (https://minecraft.wiki/w/Opacity)
Definition: BlockInfo.cpp:961
static const int Height
Definition: ChunkDef.h:125
void SendBlockAction(Vector3i a_BlockPos, char a_Byte1, char a_Byte2, BLOCKTYPE a_BlockType)
Definition: Player.h:29
StatisticsManager & GetStatistics()
Return the associated statistic and achievement manager.
Definition: Player.h:237
void OpenWindow(cWindow &a_Window)
Opens the specified window; closes the current one first using CloseWindow()
Definition: Player.cpp:1123
cWindow * GetWindow(void)
Definition: Player.h:262
Definition: Item.h:37
void GetJson(Json::Value &a_OutValue) const
Saves the item data into JSON representation.
Definition: Item.cpp:225
void SetSlot(int a_X, int a_Y, const cItem &a_Item)
Definition: ItemGrid.cpp:121
const cItem & GetSlot(int a_X, int a_Y) const
Definition: ItemGrid.cpp:96
int GetNumSlots(void) const
Definition: ItemGrid.h:40
static bool IsCatSittingOnBlock(cWorld *a_World, Vector3d a_BlockPosition)
Returns true if there's a cat sitting above the given position.
Definition: Ocelot.cpp:196
std::unordered_map< CustomStatistic, StatValue > Custom
Represents a UI window.
Definition: Window.h:54
cWindow * GetWindow(void) const
Definition: WindowOwner.h:40
void OpenWindow(cWindow *a_Window)
Definition: WindowOwner.h:34
Window owner that is associated with a block entity (chest, furnace, ...)
Definition: WindowOwner.h:59
Definition: World.h:53