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"
|
2021-04-30 09:23:46 -04:00
|
|
|
#include "../Chunk.h"
|
2020-04-03 02:57:01 -04:00
|
|
|
#include "../BlockInfo.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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-09-29 08:59:24 -04:00
|
|
|
cChestEntity::cChestEntity(BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos, cWorld * a_World):
|
2020-04-13 12:38:06 -04:00
|
|
|
Super(a_BlockType, a_BlockMeta, a_Pos, 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
|
|
|
{
|
2021-04-30 09:23:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cChestEntity & cChestEntity::GetPrimaryChest()
|
|
|
|
{
|
|
|
|
if (m_Neighbour == nullptr)
|
2017-05-28 14:07:38 -04:00
|
|
|
{
|
2021-04-30 09:23:46 -04:00
|
|
|
return *this;
|
2017-05-28 14:07:38 -04:00
|
|
|
}
|
2021-04-30 09:23:46 -04:00
|
|
|
|
|
|
|
// The primary chest should be the one with lesser X or Z coord:
|
|
|
|
return (
|
|
|
|
(m_Neighbour->GetPosX() < GetPosX()) ||
|
|
|
|
(m_Neighbour->GetPosZ() < GetPosZ())
|
|
|
|
) ? *m_Neighbour : *this;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-07-26 17:24:36 -04:00
|
|
|
|
2021-04-30 09:23:46 -04:00
|
|
|
cChestEntity * cChestEntity::GetSecondaryChest()
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2021-04-30 09:23:46 -04:00
|
|
|
// If we're the primary, then our neighbour is the secondary, and vice versa:
|
|
|
|
return (&GetPrimaryChest() == this) ? m_Neighbour : this;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-04-30 09:23:46 -04:00
|
|
|
bool cChestEntity::ScanNeighbour(cChunk & a_Chunk, Vector3i a_Position)
|
2017-06-15 09:32:33 -04:00
|
|
|
{
|
2021-04-30 09:23:46 -04:00
|
|
|
const auto Chunk = a_Chunk.GetRelNeighborChunkAdjustCoords(a_Position);
|
|
|
|
|
|
|
|
if ((Chunk == nullptr) || !Chunk->IsValid())
|
2021-01-02 08:50:34 -05:00
|
|
|
{
|
2021-04-30 09:23:46 -04:00
|
|
|
// If a chest was in fact there, they'll find us when their chunk loads.
|
|
|
|
return false;
|
2021-01-02 08:50:34 -05:00
|
|
|
}
|
2017-06-15 09:32:33 -04:00
|
|
|
|
2021-04-30 09:23:46 -04:00
|
|
|
const auto BlockEntity = Chunk->GetBlockEntityRel(a_Position);
|
|
|
|
|
|
|
|
if ((BlockEntity == nullptr) || (BlockEntity->GetBlockType() != m_BlockType))
|
|
|
|
{
|
|
|
|
// Neighbouring block is not the same type of chest:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_Neighbour = static_cast<cChestEntity *>(BlockEntity);
|
|
|
|
return true;
|
2017-06-15 09:32:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-04-30 09:23:46 -04:00
|
|
|
void cChestEntity::DestroyWindow()
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2021-04-30 09:23:46 -04:00
|
|
|
const auto Window = GetWindow();
|
|
|
|
if (Window != nullptr)
|
|
|
|
{
|
|
|
|
Window->OwnerDestroyed();
|
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-04-30 09:23:46 -04:00
|
|
|
bool cChestEntity::IsBlocked()
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2021-04-30 09:23:46 -04:00
|
|
|
return (
|
|
|
|
(GetPosY() < cChunkDef::Height - 1) &&
|
|
|
|
(
|
|
|
|
!cBlockInfo::IsTransparent(GetWorld()->GetBlock(GetPos().addedY(1))) ||
|
|
|
|
!cOcelot::IsCatSittingOnBlock(GetWorld(), Vector3d(GetPos()))
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2017-05-28 14:07:38 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-09-05 11:13:44 -04:00
|
|
|
|
2021-04-30 09:23:46 -04:00
|
|
|
void cChestEntity::OpenNewWindow(void)
|
|
|
|
{
|
|
|
|
if (m_Neighbour != nullptr)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2021-04-30 09:23:46 -04:00
|
|
|
ASSERT(&GetPrimaryChest() == this); // Should only open windows for the primary chest.
|
2015-07-06 12:39:02 -04:00
|
|
|
|
2021-04-30 09:23:46 -04:00
|
|
|
OpenWindow(new cChestWindow(this, m_Neighbour));
|
|
|
|
}
|
|
|
|
else
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2021-04-30 09:23:46 -04:00
|
|
|
// There is no chest neighbour, open a single-chest window:
|
|
|
|
OpenWindow(new cChestWindow(this));
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-04-30 09:23:46 -04:00
|
|
|
void cChestEntity::CopyFrom(const cBlockEntity & a_Src)
|
2021-04-23 08:51:59 -04:00
|
|
|
{
|
2021-04-30 09:23:46 -04:00
|
|
|
Super::CopyFrom(a_Src);
|
|
|
|
auto & src = static_cast<const cChestEntity &>(a_Src);
|
|
|
|
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;
|
2021-04-23 08:51:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-04-30 09:23:46 -04:00
|
|
|
void cChestEntity::OnAddToWorld(cWorld & a_World, cChunk & a_Chunk)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2021-04-30 09:23:46 -04:00
|
|
|
Super::OnAddToWorld(a_World, a_Chunk);
|
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 (
|
2021-04-30 09:23:46 -04:00
|
|
|
const auto Position = GetRelPos();
|
|
|
|
|
|
|
|
ScanNeighbour(a_Chunk, Position.addedX(-1)) ||
|
|
|
|
ScanNeighbour(a_Chunk, Position.addedX(+1)) ||
|
|
|
|
ScanNeighbour(a_Chunk, Position.addedZ(-1)) ||
|
|
|
|
ScanNeighbour(a_Chunk, Position.addedZ(+1))
|
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;
|
2021-04-30 09:23:46 -04:00
|
|
|
m_Neighbour->DestroyWindow(); // Force neighbour's window shut. Does Mojang server do this or should a double window open?
|
2017-05-28 14:07:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-04-30 09:23:46 -04:00
|
|
|
void cChestEntity::OnRemoveFromWorld()
|
2017-05-28 14:07:38 -04:00
|
|
|
{
|
|
|
|
if (m_Neighbour != nullptr)
|
|
|
|
{
|
2021-04-30 09:23:46 -04:00
|
|
|
// Neighbour may share a window with us, force the window shut:
|
|
|
|
m_Neighbour->DestroyWindow();
|
|
|
|
m_Neighbour->m_Neighbour = nullptr;
|
2017-05-28 14:07:38 -04:00
|
|
|
}
|
2021-04-30 09:23:46 -04:00
|
|
|
|
|
|
|
DestroyWindow();
|
2017-05-28 14:07:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-04-30 09:23:46 -04:00
|
|
|
void cChestEntity::SendTo(cClientHandle & a_Client)
|
2017-05-28 14:07:38 -04:00
|
|
|
{
|
2021-04-30 09:23:46 -04:00
|
|
|
a_Client.SendUpdateBlockEntity(*this);
|
2017-05-28 14:07:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-20 17:53:09 -04:00
|
|
|
|
2017-05-28 14:07:38 -04:00
|
|
|
|
|
|
|
|
2021-04-30 09:23:46 -04:00
|
|
|
bool cChestEntity::UsedBy(cPlayer * a_Player)
|
2017-05-28 14:07:38 -04:00
|
|
|
{
|
2021-04-30 09:23:46 -04:00
|
|
|
if (IsBlocked())
|
|
|
|
{
|
|
|
|
// Obstruction, don't open
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((m_Neighbour != nullptr) && m_Neighbour->IsBlocked())
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_BlockType == E_BLOCK_CHEST)
|
|
|
|
{
|
2021-05-03 16:07:09 -04:00
|
|
|
a_Player->GetStatistics().Custom[CustomStatistic::OpenChest]++;
|
2021-04-30 09:23:46 -04:00
|
|
|
}
|
|
|
|
else // E_BLOCK_TRAPPED_CHEST
|
|
|
|
{
|
2021-05-03 16:07:09 -04:00
|
|
|
a_Player->GetStatistics().Custom[CustomStatistic::TriggerTrappedChest]++;
|
2021-04-30 09:23:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
auto & PrimaryChest = GetPrimaryChest();
|
|
|
|
cWindow * Window = PrimaryChest.GetWindow();
|
|
|
|
|
|
|
|
// If the window is not created, open it anew:
|
|
|
|
if (Window == nullptr)
|
|
|
|
{
|
|
|
|
PrimaryChest.OpenNewWindow();
|
|
|
|
Window = PrimaryChest.GetWindow();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open the window for the player:
|
|
|
|
if (Window != nullptr)
|
|
|
|
{
|
|
|
|
if (a_Player->GetWindow() != Window)
|
|
|
|
{
|
|
|
|
a_Player->OpenWindow(*Window);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
2017-07-12 06:42:02 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-07-28 20:18:59 -04:00
|
|
|
|
|
|
|
void cChestEntity::OnSlotChanged(cItemGrid * a_Grid, int a_SlotNum)
|
|
|
|
{
|
|
|
|
ASSERT(a_Grid == &m_Contents);
|
|
|
|
|
|
|
|
// Have cBlockEntityWithItems update redstone and try to broadcast our window:
|
|
|
|
Super::OnSlotChanged(a_Grid, a_SlotNum);
|
|
|
|
|
|
|
|
cWindow * Window = GetWindow();
|
|
|
|
if ((Window == nullptr) && (m_Neighbour != nullptr))
|
|
|
|
{
|
|
|
|
// Window was null, Super will have failed.
|
|
|
|
// Neighbour might own the window:
|
|
|
|
Window = m_Neighbour->GetWindow();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Window != nullptr)
|
|
|
|
{
|
|
|
|
Window->BroadcastWholeWindow();
|
|
|
|
}
|
|
|
|
}
|