aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.Client/Common/UI/IUIScene_TradingMenu.cpp
blob: 0b1e0df2447c64e9cf0f1062037506f74803e760 (plain)
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#include "stdafx.h"
#include "..\..\..\Minecraft.World\net.minecraft.world.item.trading.h"
#include "..\..\..\Minecraft.World\net.minecraft.world.inventory.h"
#include "..\..\..\Minecraft.World\net.minecraft.world.item.h"
#include "..\..\..\Minecraft.World\net.minecraft.network.packet.h"
#include "..\..\Minecraft.h"
#include "..\..\MultiPlayerLocalPlayer.h"
#include "..\..\ClientConnection.h"
#include "IUIScene_TradingMenu.h"

#include "UI.h"

IUIScene_TradingMenu::IUIScene_TradingMenu()
{
	m_validOffersCount = 0;
	m_selectedSlot = 0;
	m_offersStartIndex = 0;
	m_menu = nullptr;
	m_bHasUpdatedOnce = false;
}

shared_ptr<Merchant> IUIScene_TradingMenu::getMerchant()
{
	return m_merchant;
}

bool IUIScene_TradingMenu::handleKeyDown(int iPad, int iAction, bool bRepeat)
{
	bool handled = false;
	//MerchantRecipeList *offers = m_merchant->getOffers(Minecraft::GetInstance()->localplayers[getPad()]);

	bool changed = false;

	Minecraft *pMinecraft = Minecraft::GetInstance();

	if( pMinecraft->localgameModes[getPad()] != nullptr )
	{
		Tutorial *tutorial = pMinecraft->localgameModes[getPad()]->getTutorial();
		if(tutorial != nullptr)
		{
			tutorial->handleUIInput(iAction);
			if(ui.IsTutorialVisible(getPad()) && !tutorial->isInputAllowed(iAction))
			{
				return S_OK;
			}
		}
	}


	switch(iAction)
	{
	case ACTION_MENU_B:
		ui.ShowTooltip( iPad, eToolTipButtonX, false );
		ui.ShowTooltip( iPad, eToolTipButtonB, false );
		ui.ShowTooltip( iPad, eToolTipButtonA, false );
		ui.ShowTooltip( iPad, eToolTipButtonRB, false );
		// kill the crafting xui
		//ui.PlayUISFX(eSFX_Back);
		ui.CloseUIScenes(iPad);

		handled = true;
		break;
	case ACTION_MENU_A:
#ifdef __ORBIS__
	case ACTION_MENU_TOUCHPAD_PRESS:
#endif
		if(!m_activeOffers.empty())
		{
			int selectedShopItem = (m_selectedSlot + m_offersStartIndex);
			if( selectedShopItem < m_activeOffers.size() )
			{
				MerchantRecipe *activeRecipe = m_activeOffers.at(selectedShopItem).first;
				if(!activeRecipe->isDeprecated())
				{
					// Do we have the ingredients?
					shared_ptr<ItemInstance> buyAItem = activeRecipe->getBuyAItem();
					shared_ptr<ItemInstance> buyBItem = activeRecipe->getBuyBItem();
					shared_ptr<MultiplayerLocalPlayer> player = Minecraft::GetInstance()->localplayers[getPad()];
					int buyAMatches = player->inventory->countMatches(buyAItem);
					int buyBMatches = player->inventory->countMatches(buyBItem);
					if( (buyAItem != nullptr && buyAMatches >= buyAItem->count) && (buyBItem == nullptr || buyBMatches >= buyBItem->count) )
					{
						// 4J-JEV: Fix for PS4 #7111: [PATCH 1.12] Trading Librarian villagers for multiple �Enchanted Books� will cause the title to crash.
						int actualShopItem = m_activeOffers.at(selectedShopItem).second;

						m_merchant->notifyTrade(activeRecipe);

						// Remove the items we are purchasing with
						player->inventory->removeResources(buyAItem);
						player->inventory->removeResources(buyBItem);

						// Add the item we have purchased
						shared_ptr<ItemInstance> result = activeRecipe->getSellItem()->copy();
						if(!player->inventory->add( result ) )
						{
							player->drop(result);
						}

						// Send a packet to the server
						player->connection->send(std::make_shared<TradeItemPacket>(m_menu->containerId, actualShopItem));

						updateDisplay();
					}
				}
			}
		}
		handled = true;
		break;
	case ACTION_MENU_LEFT:
		handled = true;
		if(m_selectedSlot == 0)
		{
			if(m_offersStartIndex > 0)
			{
				--m_offersStartIndex;
				changed = true;
			}
		}
		else
		{
			--m_selectedSlot;
			changed = true;
			moveSelector(false);
		}
		break;
	case ACTION_MENU_RIGHT:
		handled = true;
		if(m_selectedSlot == (DISPLAY_TRADES_COUNT - 1))
		{
			if((m_offersStartIndex + DISPLAY_TRADES_COUNT) < m_activeOffers.size())
			{
				++m_offersStartIndex;
				changed = true;
			}
		}
		else
		{
			++m_selectedSlot;
			changed = true;
			moveSelector(true);
		}
		break;
	}
	if (changed)
	{
		updateDisplay();

		int selectedShopItem = (m_selectedSlot + m_offersStartIndex);
		if( selectedShopItem < m_activeOffers.size() )
		{
			int actualShopItem = m_activeOffers.at(selectedShopItem).second;
			m_menu->setSelectionHint(actualShopItem);

			ByteArrayOutputStream rawOutput;
			DataOutputStream output(&rawOutput);
			output.writeInt(actualShopItem);
			Minecraft::GetInstance()->getConnection(getPad())->send(std::make_shared<CustomPayloadPacket>(CustomPayloadPacket::TRADER_SELECTION_PACKET, rawOutput.toByteArray()));
		}
	}
	return handled;
}

void IUIScene_TradingMenu::handleTick()
{
	int offerCount = 0;
	MerchantRecipeList *offers = m_merchant->getOffers(Minecraft::GetInstance()->localplayers[getPad()]);
	if (offers != nullptr)
	{
		offerCount = offers->size();

		if(!m_bHasUpdatedOnce)
		{
			updateDisplay();
		}
	}

	showScrollRightArrow( (m_offersStartIndex + DISPLAY_TRADES_COUNT) < m_activeOffers.size());
	showScrollLeftArrow(m_offersStartIndex > 0);
}

void IUIScene_TradingMenu::updateDisplay()
{
	int iA = -1;

	MerchantRecipeList *unfilteredOffers = m_merchant->getOffers(Minecraft::GetInstance()->localplayers[getPad()]);
	if (unfilteredOffers != nullptr)
	{
		m_activeOffers.clear();
		int unfilteredIndex = 0;
		int firstValidTrade = INT_MAX;
		for(auto& recipe : *unfilteredOffers)
		{
			if(!recipe->isDeprecated())
			{
				m_activeOffers.emplace_back(recipe,unfilteredIndex);
				firstValidTrade = std::min<int>(firstValidTrade, unfilteredIndex);
			}
			++unfilteredIndex;
		}

		if(!m_bHasUpdatedOnce)
		{
			if(firstValidTrade != 0 && firstValidTrade < unfilteredOffers->size())
			{
				m_menu->setSelectionHint(firstValidTrade);

				ByteArrayOutputStream rawOutput;
				DataOutputStream output(&rawOutput);
				output.writeInt(firstValidTrade);
				Minecraft::GetInstance()->getConnection(getPad())->send(std::make_shared<CustomPayloadPacket>(CustomPayloadPacket::TRADER_SELECTION_PACKET, rawOutput.toByteArray()));
			}
		}

		if( (m_offersStartIndex + DISPLAY_TRADES_COUNT) > m_activeOffers.size())
		{
			m_offersStartIndex = m_activeOffers.size() - DISPLAY_TRADES_COUNT;
			if(m_offersStartIndex < 0) m_offersStartIndex = 0;
		}

		for(unsigned int i = 0; i < DISPLAY_TRADES_COUNT; ++i)
		{
			int offerIndex = i + m_offersStartIndex;
			bool showRedBox = false;
			if(offerIndex < m_activeOffers.size())
			{
				showRedBox = !canMake(m_activeOffers.at(offerIndex).first);
				setTradeItem(i, m_activeOffers.at(offerIndex).first->getSellItem() );
			}
			else
			{
				setTradeItem(i, nullptr);
			}
			setTradeRedBox( i, showRedBox);
		}

		int selectedShopItem = (m_selectedSlot + m_offersStartIndex);
		if( selectedShopItem < m_activeOffers.size() )
		{
			MerchantRecipe *activeRecipe = m_activeOffers.at(selectedShopItem).first;

			wstring wsTemp;

			// 4J-PB - need to get the villager type here
			wsTemp = app.GetString(IDS_VILLAGER_OFFERS_ITEM);
			wsTemp = replaceAll(wsTemp,L"{*VILLAGER_TYPE*}",m_merchant->getDisplayName());
			size_t iPos=wsTemp.find(L"%s");
			wsTemp.replace(iPos,2,activeRecipe->getSellItem()->getHoverName());

			setTitle(wsTemp.c_str());

			vector<HtmlString> *offerDescription = GetItemDescription(activeRecipe->getSellItem());
			setOfferDescription(offerDescription);

			shared_ptr<ItemInstance> buyAItem = activeRecipe->getBuyAItem();
			shared_ptr<ItemInstance> buyBItem = activeRecipe->getBuyBItem();

			setRequest1Item(buyAItem);
			setRequest2Item(buyBItem);

			if(buyAItem != nullptr) setRequest1Name(buyAItem->getHoverName());
			else setRequest1Name(L"");

			if(buyBItem != nullptr) setRequest2Name(buyBItem->getHoverName());
			else setRequest2Name(L"");

			bool canMake = true;

			shared_ptr<MultiplayerLocalPlayer> player = Minecraft::GetInstance()->localplayers[getPad()];
			int buyAMatches = player->inventory->countMatches(buyAItem);
			if(buyAMatches > 0)
			{
				setRequest1RedBox(buyAMatches < buyAItem->count);
				canMake = buyAMatches > buyAItem->count;
			}
			else
			{
				setRequest1RedBox(true);
				canMake = false;
			}

			int buyBMatches = player->inventory->countMatches(buyBItem);
			if(buyBMatches > 0)
			{
				setRequest2RedBox(buyBMatches < buyBItem->count);
				canMake = canMake && buyBMatches > buyBItem->count;
			}
			else
			{
				if(buyBItem!=nullptr)
				{
					setRequest2RedBox(true);
					canMake = false;
				}
				else
				{
					setRequest2RedBox(buyBItem != nullptr);
					canMake = canMake && buyBItem == nullptr;
				}
			}

			if(canMake) iA = IDS_TOOLTIPS_TRADE;
		}
		else
		{
			setTitle(m_merchant->getDisplayName());
			setRequest1Name(L"");
			setRequest2Name(L"");
			setRequest1RedBox(false);
			setRequest2RedBox(false);
			setRequest1Item(nullptr);
			setRequest2Item(nullptr);
			vector<HtmlString> offerDescription;
			setOfferDescription(&offerDescription);
		}

		m_bHasUpdatedOnce = true;
	}

	ui.SetTooltips(getPad(), iA, IDS_TOOLTIPS_EXIT);
}

bool IUIScene_TradingMenu::canMake(MerchantRecipe *recipe)
{
	bool canMake = false;
	if (recipe != nullptr)
	{
		if(recipe->isDeprecated()) return false;

		shared_ptr<ItemInstance> buyAItem = recipe->getBuyAItem();
		shared_ptr<ItemInstance> buyBItem = recipe->getBuyBItem();

		shared_ptr<MultiplayerLocalPlayer> player = Minecraft::GetInstance()->localplayers[getPad()];
		int buyAMatches = player->inventory->countMatches(buyAItem);
		if(buyAMatches > 0)
		{
			canMake = buyAMatches >= buyAItem->count;
		}
		else
		{
			canMake = buyAItem == nullptr;
		}

		int buyBMatches = player->inventory->countMatches(buyBItem);
		if(buyBMatches > 0)
		{
			canMake = canMake && buyBMatches >= buyBItem->count;
		}
		else
		{
			canMake = canMake && buyBItem == nullptr;
		}
	}
	return canMake;
}


void IUIScene_TradingMenu::setRequest1Item(shared_ptr<ItemInstance> item)
{
}

void IUIScene_TradingMenu::setRequest2Item(shared_ptr<ItemInstance> item)
{
}

void IUIScene_TradingMenu::setTradeItem(int index, shared_ptr<ItemInstance> item)
{
}

vector<HtmlString> *IUIScene_TradingMenu::GetItemDescription(shared_ptr<ItemInstance> item)
{
	vector<HtmlString> *lines = item->getHoverText(nullptr, false);

	// Add rarity to first line
	if (lines->size() > 0)
	{
		lines->at(0).color = item->getRarity()->color;
	}

	return lines;
}

void IUIScene_TradingMenu::HandleInventoryUpdated()
{
	updateDisplay();
}