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

98 lines
1.5 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"
#include "../UI/Window.h"
cEnderChestEntity::cEnderChestEntity(int a_BlockX, int a_BlockY, int a_BlockZ, cWorld * a_World) :
2014-09-27 14:19:28 -04:00
super(E_BLOCK_ENDER_CHEST, a_BlockX, a_BlockY, a_BlockZ, a_World),
cBlockEntityWindowOwner(this)
{
}
cEnderChestEntity::~cEnderChestEntity()
{
cWindow * Window = GetWindow();
2014-10-20 16:55:07 -04:00
if (Window != nullptr)
{
Window->OwnerDestroyed();
}
}
2014-06-29 06:36:38 -04:00
void cEnderChestEntity::UsedBy(cPlayer * a_Player)
{
2014-06-29 06:36:38 -04:00
// If the window is not created, open it anew:
cWindow * Window = GetWindow();
2014-10-20 16:55:07 -04:00
if (Window == nullptr)
{
2014-06-29 06:36:38 -04:00
OpenNewWindow();
Window = GetWindow();
}
2014-06-29 06:36:38 -04:00
// Open the window for the player:
2014-10-20 16:55:07 -04:00
if (Window != nullptr)
{
2014-06-29 06:36:38 -04:00
if (a_Player->GetWindow() != Window)
{
a_Player->OpenWindow(Window);
}
}
}
2014-06-29 06:36:38 -04:00
void cEnderChestEntity::OpenNewWindow()
{
2014-06-29 06:36:38 -04:00
OpenWindow(new cEnderChestWindow(this));
}
2014-06-29 06:36:38 -04:00
void cEnderChestEntity::LoadFromJson(const Json::Value & a_Value, cItemGrid & a_Grid)
{
2014-06-29 06:36:38 -04:00
int SlotIdx = 0;
for (Json::Value::iterator itr = a_Value.begin(); itr != a_Value.end(); ++itr)
{
2014-06-29 06:36:38 -04:00
cItem Item;
Item.FromJson(*itr);
a_Grid.SetSlot(SlotIdx, Item);
SlotIdx++;
}
}
2014-06-29 06:36:38 -04:00
void cEnderChestEntity::SaveToJson(Json::Value & a_Value, const cItemGrid & a_Grid)
{
2014-06-29 06:36:38 -04:00
for (int i = 0; i < a_Grid.GetNumSlots(); i++)
{
Json::Value Slot;
a_Grid.GetSlot(i).GetJson(Slot);
a_Value.append(Slot);
}
}