1
0
Fork 0
cuberite-2a/src/BlockEntities/EnderChestEntity.cpp

100 lines
1.6 KiB
C++
Raw Normal View History

#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
#include "EnderChestEntity.h"
#include "../Item.h"
#include "../Entities/Player.h"
2014-12-13 14:06:55 +00:00
#include "../UI/EnderChestWindow.h"
cEnderChestEntity::cEnderChestEntity(int a_BlockX, int a_BlockY, int a_BlockZ, cWorld * a_World) :
2014-09-27 18:19:28 +00:00
super(E_BLOCK_ENDER_CHEST, a_BlockX, a_BlockY, a_BlockZ, a_World),
cBlockEntityWindowOwner(this)
{
}
cEnderChestEntity::~cEnderChestEntity()
{
cWindow * Window = GetWindow();
2014-10-20 20:55:07 +00:00
if (Window != nullptr)
{
Window->OwnerDestroyed();
}
}
2014-06-29 10:36:38 +00:00
void cEnderChestEntity::UsedBy(cPlayer * a_Player)
{
// TODO: cats are an obstruction
if ((GetPosY() < cChunkDef::Height - 1) && !cBlockInfo::IsTransparent(GetWorld()->GetBlock(GetPosX(), GetPosY() + 1, GetPosZ())))
{
// Obstruction, don't open
return;
}
2014-06-29 10:36:38 +00:00
// If the window is not created, open it anew:
cWindow * Window = GetWindow();
2014-10-20 20:55:07 +00:00
if (Window == nullptr)
{
2014-06-29 10:36:38 +00:00
OpenNewWindow();
Window = GetWindow();
}
2014-06-29 10:36:38 +00:00
// Open the window for the player:
2014-10-20 20:55:07 +00:00
if (Window != nullptr)
{
2014-06-29 10:36:38 +00:00
if (a_Player->GetWindow() != Window)
{
a_Player->OpenWindow(Window);
}
}
}
2014-06-29 10:36:38 +00:00
void cEnderChestEntity::OpenNewWindow()
{
2014-06-29 10:36:38 +00:00
OpenWindow(new cEnderChestWindow(this));
}
2014-06-29 10:36:38 +00:00
void cEnderChestEntity::LoadFromJson(const Json::Value & a_Value, cItemGrid & a_Grid)
{
2014-06-29 10:36:38 +00:00
int SlotIdx = 0;
2015-10-19 14:03:55 +00:00
for (auto & Node : a_Value)
{
2014-06-29 10:36:38 +00:00
cItem Item;
2015-10-19 14:03:55 +00:00
Item.FromJson(Node);
2014-06-29 10:36:38 +00:00
a_Grid.SetSlot(SlotIdx, Item);
SlotIdx++;
}
}
2014-06-29 10:36:38 +00:00
void cEnderChestEntity::SaveToJson(Json::Value & a_Value, const cItemGrid & a_Grid)
{
2014-06-29 10:36:38 +00:00
for (int i = 0; i < a_Grid.GetNumSlots(); i++)
{
Json::Value Slot;
a_Grid.GetSlot(i).GetJson(Slot);
a_Value.append(Slot);
}
}