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
|
#include "stdafx.h"
#include "net.minecraft.world.item.trading.h"
#include "MerchantRecipeList.h"
MerchantRecipeList::MerchantRecipeList()
{
}
MerchantRecipeList::MerchantRecipeList(CompoundTag *tag)
{
load(tag);
}
MerchantRecipeList::~MerchantRecipeList()
{
for(auto& it : m_recipes)
{
delete it;
}
}
MerchantRecipe *MerchantRecipeList::getRecipeFor(shared_ptr<ItemInstance> buyA, shared_ptr<ItemInstance> buyB, int selectionHint)
{
if (buyA == nullptr)
{
return nullptr;
}
if (selectionHint > 0 && selectionHint < static_cast<int>(m_recipes.size()))
{
// attempt to match vs the hint
MerchantRecipe *r = m_recipes.at(selectionHint);
if (buyA->id == r->getBuyAItem()->id && ((buyB == nullptr && !r->hasSecondaryBuyItem()) || (r->hasSecondaryBuyItem() && buyB != nullptr && r->getBuyBItem()->id == buyB->id)))
{
if (buyA->count >= r->getBuyAItem()->count && (!r->hasSecondaryBuyItem() || buyB->count >= r->getBuyBItem()->count))
{
return r;
}
}
return nullptr;
}
for (size_t i = 0; i < m_recipes.size(); i++)
{
MerchantRecipe *r = m_recipes.at(i);
if (buyA->id == r->getBuyAItem()->id && buyA->count >= r->getBuyAItem()->count
&& ((!r->hasSecondaryBuyItem() && buyB == nullptr) || (r->hasSecondaryBuyItem() && buyB != nullptr && r->getBuyBItem()->id == buyB->id && buyB->count >= r->getBuyBItem()->count)))
{
return r;
}
}
return nullptr;
}
bool MerchantRecipeList::addIfNewOrBetter(MerchantRecipe *recipe)
{
bool added = false;
for (size_t i = 0; i < m_recipes.size(); i++)
{
MerchantRecipe *r = m_recipes.at(i);
if (recipe->isSame(r))
{
if (recipe->isSameSameButBetter(r))
{
delete m_recipes[i];
m_recipes[i] = recipe;
added = true;
}
return added;
}
}
m_recipes.push_back(recipe);
return true;
}
MerchantRecipe *MerchantRecipeList::getMatchingRecipeFor(shared_ptr<ItemInstance> buy, shared_ptr<ItemInstance> buyB, shared_ptr<ItemInstance> sell)
{
for (size_t i = 0; i < m_recipes.size(); i++)
{
MerchantRecipe *r = m_recipes.at(i);
if (buy->id == r->getBuyAItem()->id && buy->count >= r->getBuyAItem()->count && sell->id == r->getSellItem()->id)
{
if (!r->hasSecondaryBuyItem() || (buyB != nullptr && buyB->id == r->getBuyBItem()->id && buyB->count >= r->getBuyBItem()->count))
{
return r;
}
}
}
return nullptr;
}
void MerchantRecipeList::writeToStream(DataOutputStream *stream)
{
stream->writeByte(static_cast<byte>(m_recipes.size() & 0xff));
for (size_t i = 0; i < m_recipes.size(); i++)
{
MerchantRecipe *r = m_recipes.at(i);
Packet::writeItem(r->getBuyAItem(), stream);
Packet::writeItem(r->getSellItem(), stream);
shared_ptr<ItemInstance> buyBItem = r->getBuyBItem();
stream->writeBoolean(buyBItem != nullptr);
if (buyBItem != nullptr)
{
Packet::writeItem(buyBItem, stream);
}
stream->writeBoolean(r->isDeprecated());
stream->writeInt(r->getUses());
stream->writeInt(r->getMaxUses());
}
}
MerchantRecipeList *MerchantRecipeList::createFromStream(DataInputStream *stream)
{
MerchantRecipeList *list = new MerchantRecipeList();
int count = (int) (stream->readByte() & 0xff);
for (int i = 0; i < count; i++)
{
shared_ptr<ItemInstance> buy = Packet::readItem(stream);
shared_ptr<ItemInstance> sell = Packet::readItem(stream);
shared_ptr<ItemInstance> buyB = nullptr;
if (stream->readBoolean())
{
buyB = Packet::readItem(stream);
}
bool isDeprecated = stream->readBoolean();
int uses = stream->readInt();
int maxUses = stream->readInt();
MerchantRecipe *recipe = new MerchantRecipe(buy, buyB, sell, uses, maxUses);
if (isDeprecated)
{
recipe->enforceDeprecated();
}
list->push_back(recipe);
}
return list;
}
void MerchantRecipeList::load(CompoundTag *tag)
{
ListTag<CompoundTag> *list = (ListTag<CompoundTag> *) tag->getList(L"Recipes");
for (int i = 0; i < list->size(); i++)
{
CompoundTag *recipeTag = list->get(i);
m_recipes.push_back(new MerchantRecipe(recipeTag));
}
}
CompoundTag *MerchantRecipeList::createTag()
{
CompoundTag *tag = new CompoundTag();
ListTag<CompoundTag> *list = new ListTag<CompoundTag>(L"Recipes");
for (size_t i = 0; i < m_recipes.size(); i++)
{
MerchantRecipe *merchantRecipe = m_recipes.at(i);
list->add(merchantRecipe->createTag());
}
tag->put(L"Recipes", list);
return tag;
}
void MerchantRecipeList::push_back(MerchantRecipe *recipe)
{
m_recipes.push_back(recipe);
}
MerchantRecipe *MerchantRecipeList::at(size_t index)
{
if (index >= m_recipes.size())
{
return nullptr;
}
return m_recipes.at(index);
}
std::vector<MerchantRecipe *>::iterator MerchantRecipeList::begin()
{
return m_recipes.begin();
}
std::vector<MerchantRecipe *>::iterator MerchantRecipeList::end()
{
return m_recipes.end();
}
std::vector<MerchantRecipe *>::iterator MerchantRecipeList::erase(std::vector<MerchantRecipe *>::iterator it)
{
return m_recipes.erase(it);
}
size_t MerchantRecipeList::size()
{
return m_recipes.size();
}
bool MerchantRecipeList::empty()
{
return m_recipes.empty();
}
|