2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
|
|
|
|
|
2012-09-23 18:09:57 -04:00
|
|
|
#include "ChestEntity.h"
|
2013-05-28 15:12:47 -04:00
|
|
|
#include "../Item.h"
|
2013-08-19 05:39:13 -04:00
|
|
|
#include "../Entities/Player.h"
|
2014-12-13 09:06:55 -05:00
|
|
|
#include "../UI/ChestWindow.h"
|
2017-05-21 05:48:33 -04:00
|
|
|
#include "../ClientHandle.h"
|
2017-07-12 06:42:02 -04:00
|
|
|
#include "../Mobs/Ocelot.h"
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-06-15 09:32:33 -04:00
|
|
|
cChestEntity::cChestEntity(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, ContentsWidth, ContentsHeight, a_World),
|
2017-05-28 14:07:38 -04:00
|
|
|
m_NumActivePlayers(0),
|
|
|
|
m_Neighbour(nullptr)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2017-05-28 14:07:38 -04:00
|
|
|
int ChunkX = 0, ChunkZ = 0;
|
|
|
|
cChunkDef::BlockToChunk(m_PosX, m_PosZ, ChunkX, ChunkZ);
|
|
|
|
if (
|
|
|
|
(m_World != nullptr) &&
|
|
|
|
m_World->IsChunkValid(ChunkX, ChunkZ)
|
|
|
|
)
|
|
|
|
{
|
|
|
|
ScanNeighbours();
|
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cChestEntity::~cChestEntity()
|
|
|
|
{
|
2017-05-28 14:07:38 -04:00
|
|
|
if (m_Neighbour != nullptr)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2017-05-28 14:07:38 -04:00
|
|
|
// Neighbour may share a window with us, force the window shut
|
|
|
|
m_Neighbour->DestroyWindow();
|
|
|
|
m_Neighbour->m_Neighbour = nullptr;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
2017-05-28 14:07:38 -04:00
|
|
|
|
|
|
|
DestroyWindow();
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-06-15 09:32:33 -04:00
|
|
|
void cChestEntity::CopyFrom(const cBlockEntity & a_Src)
|
|
|
|
{
|
|
|
|
Super::CopyFrom(a_Src);
|
2018-05-02 03:50:36 -04:00
|
|
|
auto & src = static_cast<const cChestEntity &>(a_Src);
|
2017-06-15 09:32:33 -04:00
|
|
|
m_Contents.CopyFrom(src.m_Contents);
|
|
|
|
|
|
|
|
// Reset the neighbor and player count, there's no sense in copying these:
|
|
|
|
m_Neighbour = nullptr;
|
|
|
|
m_NumActivePlayers = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-08-24 03:58:26 -04:00
|
|
|
void cChestEntity::SendTo(cClientHandle & a_Client)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2017-05-21 05:48:33 -04:00
|
|
|
// 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);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-12-01 17:12:44 -05:00
|
|
|
bool cChestEntity::UsedBy(cPlayer * a_Player)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2017-05-28 14:07:38 -04:00
|
|
|
if (IsBlocked())
|
|
|
|
{
|
|
|
|
// Obstruction, don't open
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_Neighbour == nullptr)
|
|
|
|
{
|
|
|
|
ScanNeighbours();
|
|
|
|
}
|
|
|
|
|
|
|
|
// The primary chest should be the one with lesser X or Z coord:
|
|
|
|
cChestEntity * PrimaryChest = this;
|
|
|
|
if (m_Neighbour != nullptr)
|
|
|
|
{
|
|
|
|
if (m_Neighbour->IsBlocked())
|
|
|
|
{
|
|
|
|
// Obstruction, don't open
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
(m_Neighbour->GetPosX() > GetPosX()) ||
|
|
|
|
(m_Neighbour->GetPosZ() > GetPosZ())
|
|
|
|
)
|
|
|
|
{
|
|
|
|
PrimaryChest = m_Neighbour;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-13 02:13:56 -04:00
|
|
|
// If the window is not created, open it anew:
|
2017-05-28 14:07:38 -04:00
|
|
|
cWindow * Window = PrimaryChest->GetWindow();
|
2014-10-20 16:55:07 -04:00
|
|
|
if (Window == nullptr)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2017-05-28 14:07:38 -04:00
|
|
|
PrimaryChest->OpenNewWindow();
|
|
|
|
Window = PrimaryChest->GetWindow();
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
2015-07-06 12:39:02 -04:00
|
|
|
|
2013-06-13 02:13:56 -04:00
|
|
|
// Open the window for the player:
|
2014-10-20 16:55:07 -04:00
|
|
|
if (Window != nullptr)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-06-13 02:13:56 -04:00
|
|
|
if (a_Player->GetWindow() != Window)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2017-05-29 15:33:30 -04:00
|
|
|
a_Player->OpenWindow(*Window);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is rather a hack
|
|
|
|
// Instead of marking the chunk as dirty upon chest contents change, we mark it dirty now
|
|
|
|
// We cannot properly detect contents change, but such a change doesn't happen without a player opening the chest first.
|
|
|
|
// The few false positives aren't much to worry about
|
2013-04-13 17:02:10 -04:00
|
|
|
int ChunkX, ChunkZ;
|
2013-08-03 14:05:07 -04:00
|
|
|
cChunkDef::BlockToChunk(m_PosX, m_PosZ, ChunkX, ChunkZ);
|
2016-04-18 06:30:23 -04:00
|
|
|
m_World->MarkChunkDirty(ChunkX, ChunkZ);
|
2015-12-01 17:12:44 -05:00
|
|
|
return true;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-05-28 14:07:38 -04:00
|
|
|
void cChestEntity::ScanNeighbours()
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2017-05-28 14:07:38 -04:00
|
|
|
// Callback for finding neighbouring chest:
|
2017-09-11 17:20:49 -04:00
|
|
|
auto FindNeighbour = [this](cChestEntity & a_Chest)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2017-09-11 17:20:49 -04:00
|
|
|
if (a_Chest.GetBlockType() != m_BlockType)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2017-09-11 17:20:49 -04:00
|
|
|
// Neighboring block is not the same type of chest
|
|
|
|
return true;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
2017-09-11 17:20:49 -04:00
|
|
|
m_Neighbour = &a_Chest;
|
|
|
|
return false;
|
2017-05-28 14:07:38 -04:00
|
|
|
};
|
2015-07-06 12:39:02 -04:00
|
|
|
|
2017-06-03 15:17:53 -04:00
|
|
|
// Scan horizontally adjacent blocks for any neighbouring chest of the same type:
|
2012-10-20 17:53:09 -04:00
|
|
|
if (
|
2017-05-28 14:07:38 -04:00
|
|
|
m_World->DoWithChestAt(m_PosX - 1, m_PosY, m_PosZ, FindNeighbour) ||
|
|
|
|
m_World->DoWithChestAt(m_PosX + 1, m_PosY, m_PosZ, FindNeighbour) ||
|
|
|
|
m_World->DoWithChestAt(m_PosX, m_PosY, m_PosZ - 1, FindNeighbour) ||
|
|
|
|
m_World->DoWithChestAt(m_PosX, m_PosY, m_PosZ + 1, FindNeighbour)
|
2012-10-20 17:53:09 -04:00
|
|
|
)
|
2012-08-19 15:42:32 -04:00
|
|
|
{
|
2017-05-28 14:07:38 -04:00
|
|
|
m_Neighbour->m_Neighbour = this;
|
|
|
|
// Force neighbour's window shut. Does Mojang server do this or should a double window open?
|
|
|
|
m_Neighbour->DestroyWindow();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cChestEntity::OpenNewWindow(void)
|
|
|
|
{
|
|
|
|
if (m_Neighbour != nullptr)
|
|
|
|
{
|
|
|
|
ASSERT( // This should be the primary chest
|
|
|
|
(m_Neighbour->GetPosX() < GetPosX()) ||
|
|
|
|
(m_Neighbour->GetPosZ() < GetPosZ())
|
|
|
|
);
|
|
|
|
OpenWindow(new cChestWindow(this, m_Neighbour));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// There is no chest neighbour, open a single-chest window:
|
|
|
|
OpenWindow(new cChestWindow(this));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cChestEntity::DestroyWindow()
|
|
|
|
{
|
|
|
|
cWindow * Window = GetWindow();
|
|
|
|
if (Window != nullptr)
|
|
|
|
{
|
|
|
|
Window->OwnerDestroyed();
|
|
|
|
CloseWindow();
|
2012-08-19 15:42:32 -04:00
|
|
|
}
|
2017-05-28 14:07:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-20 17:53:09 -04:00
|
|
|
|
2017-05-28 14:07:38 -04:00
|
|
|
|
|
|
|
|
|
|
|
bool cChestEntity::IsBlocked()
|
|
|
|
{
|
|
|
|
return (
|
2017-08-17 10:29:43 -04:00
|
|
|
(GetPosY() < cChunkDef::Height - 1) &&
|
2017-07-12 06:42:02 -04:00
|
|
|
(
|
2017-08-17 10:29:43 -04:00
|
|
|
!cBlockInfo::IsTransparent(GetWorld()->GetBlock(GetPosX(), GetPosY() + 1, GetPosZ())) ||
|
|
|
|
!cOcelot::IsCatSittingOnBlock(GetWorld(), Vector3d(GetPos()))
|
2017-07-12 06:42:02 -04:00
|
|
|
)
|
2017-05-28 14:07:38 -04:00
|
|
|
);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
2017-07-12 06:42:02 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|