2013-12-06 19:18:58 -05:00
|
|
|
|
|
|
|
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
|
|
|
|
|
|
|
|
#include "EnderChestEntity.h"
|
2016-11-18 14:00:04 -05:00
|
|
|
#include "json/json.h"
|
2013-12-06 19:18:58 -05:00
|
|
|
#include "../Item.h"
|
|
|
|
#include "../Entities/Player.h"
|
2014-12-13 09:06:55 -05:00
|
|
|
#include "../UI/EnderChestWindow.h"
|
2017-05-21 05:48:33 -04:00
|
|
|
#include "../ClientHandle.h"
|
2017-08-17 10:29:43 -04:00
|
|
|
#include "../Mobs/Ocelot.h"
|
2013-12-06 19:18:58 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-06-15 09:32:33 -04:00
|
|
|
cEnderChestEntity::cEnderChestEntity(BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, int a_BlockX, int a_BlockY, int a_BlockZ, cWorld * a_World):
|
|
|
|
Super(a_BlockType, a_BlockMeta, a_BlockX, a_BlockY, a_BlockZ, a_World),
|
2014-09-27 14:19:28 -04:00
|
|
|
cBlockEntityWindowOwner(this)
|
2013-12-06 19:18:58 -05:00
|
|
|
{
|
2017-06-15 09:32:33 -04:00
|
|
|
ASSERT(a_BlockType == E_BLOCK_ENDER_CHEST);
|
2013-12-06 19:18:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cEnderChestEntity::~cEnderChestEntity()
|
|
|
|
{
|
|
|
|
cWindow * Window = GetWindow();
|
2014-10-20 16:55:07 -04:00
|
|
|
if (Window != nullptr)
|
2013-12-06 19:18:58 -05:00
|
|
|
{
|
|
|
|
Window->OwnerDestroyed();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-05-21 05:48:33 -04:00
|
|
|
void cEnderChestEntity::SendTo(cClientHandle & a_Client)
|
|
|
|
{
|
|
|
|
// Send a dummy "number of players with chest open" packet to make the chest visible:
|
|
|
|
a_Client.SendBlockAction(m_PosX, m_PosY, m_PosZ, 1, 0, m_BlockType);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-12-01 17:12:44 -05:00
|
|
|
bool cEnderChestEntity::UsedBy(cPlayer * a_Player)
|
2013-12-06 19:18:58 -05:00
|
|
|
{
|
2017-08-17 10:29:43 -04:00
|
|
|
if (
|
|
|
|
(GetPosY() < cChunkDef::Height - 1) &&
|
|
|
|
(
|
|
|
|
!cBlockInfo::IsTransparent(GetWorld()->GetBlock(GetPosX(), GetPosY() + 1, GetPosZ())) ||
|
|
|
|
!cOcelot::IsCatSittingOnBlock(GetWorld(), Vector3d(GetPos()))
|
|
|
|
)
|
|
|
|
)
|
2015-07-06 12:39:02 -04:00
|
|
|
{
|
|
|
|
// Obstruction, don't open
|
2015-12-01 17:12:44 -05:00
|
|
|
return false;
|
2015-07-06 12:39:02 -04:00
|
|
|
}
|
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)
|
2013-12-06 19:18:58 -05:00
|
|
|
{
|
2014-06-29 06:36:38 -04:00
|
|
|
OpenNewWindow();
|
|
|
|
Window = GetWindow();
|
2013-12-06 19:18:58 -05:00
|
|
|
}
|
2015-07-06 12:39:02 -04:00
|
|
|
|
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)
|
2013-12-06 19:18:58 -05:00
|
|
|
{
|
2014-06-29 06:36:38 -04:00
|
|
|
if (a_Player->GetWindow() != Window)
|
|
|
|
{
|
2017-05-29 15:33:30 -04:00
|
|
|
a_Player->OpenWindow(*Window);
|
2014-06-29 06:36:38 -04:00
|
|
|
}
|
2013-12-06 19:18:58 -05:00
|
|
|
}
|
2015-12-01 17:12:44 -05:00
|
|
|
return true;
|
2013-12-06 19:18:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-06-29 06:36:38 -04:00
|
|
|
void cEnderChestEntity::OpenNewWindow()
|
2013-12-06 19:18:58 -05:00
|
|
|
{
|
2014-06-29 06:36:38 -04:00
|
|
|
OpenWindow(new cEnderChestWindow(this));
|
2013-12-06 19:18:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-06-29 06:36:38 -04:00
|
|
|
void cEnderChestEntity::LoadFromJson(const Json::Value & a_Value, cItemGrid & a_Grid)
|
2013-12-06 19:18:58 -05:00
|
|
|
{
|
2014-06-29 06:36:38 -04:00
|
|
|
int SlotIdx = 0;
|
2015-10-19 10:03:55 -04:00
|
|
|
for (auto & Node : a_Value)
|
2013-12-06 19:18:58 -05:00
|
|
|
{
|
2014-06-29 06:36:38 -04:00
|
|
|
cItem Item;
|
2015-10-19 10:03:55 -04:00
|
|
|
Item.FromJson(Node);
|
2014-06-29 06:36:38 -04:00
|
|
|
a_Grid.SetSlot(SlotIdx, Item);
|
|
|
|
SlotIdx++;
|
2013-12-06 19:18:58 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-06-29 06:36:38 -04:00
|
|
|
void cEnderChestEntity::SaveToJson(Json::Value & a_Value, const cItemGrid & a_Grid)
|
2013-12-06 19:18:58 -05:00
|
|
|
{
|
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);
|
|
|
|
}
|
2013-12-06 19:18:58 -05:00
|
|
|
}
|