summaryrefslogtreecommitdiffstats
path: root/src/BlockEntities/ChestEntity.cpp
blob: c2c31b30ac4444211dcd14e860d7b8e13e68191d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259

#include "Globals.h"  // NOTE: MSVC stupidness requires this to be the same across all modules

#include "ChestEntity.h"
#include "../Chunk.h"
#include "../BlockInfo.h"
#include "../Item.h"
#include "../Entities/Player.h"
#include "../UI/ChestWindow.h"
#include "../ClientHandle.h"
#include "../Mobs/Ocelot.h"





cChestEntity::cChestEntity(BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos, cWorld * a_World):
	Super(a_BlockType, a_BlockMeta, a_Pos, ContentsWidth, ContentsHeight, a_World),
	m_NumActivePlayers(0),
	m_Neighbour(nullptr)
{
}





cChestEntity & cChestEntity::GetPrimaryChest()
{
	if (m_Neighbour == nullptr)
	{
		return *this;
	}

	// 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;
}





cChestEntity * cChestEntity::GetSecondaryChest()
{
	// If we're the primary, then our neighbour is the secondary, and vice versa:
	return (&GetPrimaryChest() == this) ? m_Neighbour : this;
}





bool cChestEntity::ScanNeighbour(cChunk & a_Chunk, Vector3i a_Position)
{
	const auto Chunk = a_Chunk.GetRelNeighborChunkAdjustCoords(a_Position);

	if ((Chunk == nullptr) || !Chunk->IsValid())
	{
		// If a chest was in fact there, they'll find us when their chunk loads.
		return false;
	}

	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;
}





void cChestEntity::DestroyWindow()
{
	const auto Window = GetWindow();
	if (Window != nullptr)
	{
		Window->OwnerDestroyed();
	}
}





bool cChestEntity::IsBlocked()
{
	return (
		(GetPosY() < cChunkDef::Height - 1) &&
		(
			!cBlockInfo::IsTransparent(GetWorld()->GetBlock(GetPos().addedY(1))) ||
			!cOcelot::IsCatSittingOnBlock(GetWorld(), Vector3d(GetPos()))
		)
	);
}





void cChestEntity::OpenNewWindow(void)
{
	if (m_Neighbour != nullptr)
	{
		ASSERT(&GetPrimaryChest() == this);  // Should only open windows for the primary chest.

		OpenWindow(new cChestWindow(this, m_Neighbour));
	}
	else
	{
		// There is no chest neighbour, open a single-chest window:
		OpenWindow(new cChestWindow(this));
	}
}





void cChestEntity::CopyFrom(const cBlockEntity & a_Src)
{
	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;
}





void cChestEntity::OnAddToWorld(cWorld & a_World, cChunk & a_Chunk)
{
	Super::OnAddToWorld(a_World, a_Chunk);

	// Scan horizontally adjacent blocks for any neighbouring chest of the same type:
	if (
		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))
	)
	{
		m_Neighbour->m_Neighbour = this;
		m_Neighbour->DestroyWindow();  // Force neighbour's window shut. Does Mojang server do this or should a double window open?
	}
}





void cChestEntity::OnRemoveFromWorld()
{
	if (m_Neighbour != nullptr)
	{
		// Neighbour may share a window with us, force the window shut:
		m_Neighbour->DestroyWindow();
		m_Neighbour->m_Neighbour = nullptr;
	}

	DestroyWindow();
}





void cChestEntity::SendTo(cClientHandle & a_Client)
{
	a_Client.SendUpdateBlockEntity(*this);
}





bool cChestEntity::UsedBy(cPlayer * a_Player)
{
	if (IsBlocked())
	{
		// Obstruction, don't open
		return true;
	}

	if ((m_Neighbour != nullptr) && m_Neighbour->IsBlocked())
	{
		return true;
	}

	if (m_BlockType == E_BLOCK_CHEST)
	{
		a_Player->GetStatManager().AddValue(Statistic::OpenChest);
	}
	else  // E_BLOCK_TRAPPED_CHEST
	{
		a_Player->GetStatManager().AddValue(Statistic::TriggerTrappedChest);
	}

	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;
}





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();
	}
}