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
|
#include "stdafx.h"
#include "net.minecraft.world.item.trading.h"
#include "MerchantMenu.h"
#include "MerchantContainer.h"
MerchantContainer::MerchantContainer(shared_ptr<Player> player, shared_ptr<Merchant> villager)
{
this->player = player;
merchant = villager;
items = ItemInstanceArray(3);
items[0] = nullptr;
items[1] = nullptr;
items[2] = nullptr;
activeRecipe = nullptr;
selectionHint = 0;
}
MerchantContainer::~MerchantContainer()
{
delete [] items.data;
}
unsigned int MerchantContainer::getContainerSize()
{
return items.length;
}
shared_ptr<ItemInstance> MerchantContainer::getItem(unsigned int slot)
{
return items[slot];
}
shared_ptr<ItemInstance> MerchantContainer::removeItem(unsigned int slot, int count)
{
if (items[slot] != nullptr)
{
if (slot == MerchantMenu::RESULT_SLOT)
{
shared_ptr<ItemInstance> item = items[slot];
items[slot] = nullptr;
return item;
}
if (items[slot]->count <= count)
{
shared_ptr<ItemInstance> item = items[slot];
items[slot] = nullptr;
if (isPaymentSlot(slot))
{
updateSellItem();
}
return item;
}
else
{
shared_ptr<ItemInstance> i = items[slot]->remove(count);
if (items[slot]->count == 0) items[slot] = nullptr;
if (isPaymentSlot(slot))
{
updateSellItem();
}
return i;
}
}
return nullptr;
}
bool MerchantContainer::isPaymentSlot(int slot)
{
return slot == MerchantMenu::PAYMENT1_SLOT || slot == MerchantMenu::PAYMENT2_SLOT;
}
shared_ptr<ItemInstance> MerchantContainer::removeItemNoUpdate(int slot)
{
if (items[slot] != nullptr)
{
shared_ptr<ItemInstance> item = items[slot];
items[slot] = nullptr;
return item;
}
return nullptr;
}
void MerchantContainer::setItem(unsigned int slot, shared_ptr<ItemInstance> item)
{
items[slot] = item;
if (item != nullptr && item->count > getMaxStackSize()) item->count = getMaxStackSize();
if (isPaymentSlot(slot))
{
updateSellItem();
}
}
wstring MerchantContainer::getName()
{
return merchant->getDisplayName();
}
wstring MerchantContainer::getCustomName()
{
return L"";
}
bool MerchantContainer::hasCustomName()
{
return false;
}
int MerchantContainer::getMaxStackSize() const
{
return Container::LARGE_MAX_STACK_SIZE;
}
bool MerchantContainer::stillValid(shared_ptr<Player> player)
{
return merchant->getTradingPlayer() == player;
}
void MerchantContainer::startOpen()
{
}
void MerchantContainer::stopOpen()
{
}
bool MerchantContainer::canPlaceItem(int slot, shared_ptr<ItemInstance> item)
{
return true;
}
void MerchantContainer::setChanged()
{
updateSellItem();
}
void MerchantContainer::updateSellItem()
{
activeRecipe = nullptr;
shared_ptr<ItemInstance> buyItem1 = items[MerchantMenu::PAYMENT1_SLOT];
shared_ptr<ItemInstance> buyItem2 = items[MerchantMenu::PAYMENT2_SLOT];
if (buyItem1 == nullptr)
{
buyItem1 = buyItem2;
buyItem2 = nullptr;
}
if (buyItem1 == nullptr)
{
setItem(MerchantMenu::RESULT_SLOT, nullptr);
}
else
{
MerchantRecipeList *offers = merchant->getOffers(player);
if (offers != nullptr)
{
MerchantRecipe *recipeFor = offers->getRecipeFor(buyItem1, buyItem2, selectionHint);
if (recipeFor != nullptr && !recipeFor->isDeprecated())
{
activeRecipe = recipeFor;
setItem(MerchantMenu::RESULT_SLOT, recipeFor->getSellItem()->copy());
}
else if (buyItem2 != nullptr)
{
// try to switch
recipeFor = offers->getRecipeFor(buyItem2, buyItem1, selectionHint);
if (recipeFor != nullptr && !recipeFor->isDeprecated())
{
activeRecipe = recipeFor;
setItem(MerchantMenu::RESULT_SLOT, recipeFor->getSellItem()->copy());
}
else
{
setItem(MerchantMenu::RESULT_SLOT, nullptr);
}
}
else
{
setItem(MerchantMenu::RESULT_SLOT, nullptr);
}
}
}
merchant->notifyTradeUpdated(getItem(MerchantMenu::RESULT_SLOT));
}
MerchantRecipe *MerchantContainer::getActiveRecipe()
{
return activeRecipe;
}
void MerchantContainer::setSelectionHint(int selectionHint)
{
this->selectionHint = selectionHint;
updateSellItem();
}
|