the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
1#include "stdafx.h"
2#include "net.minecraft.world.item.trading.h"
3#include "MerchantMenu.h"
4#include "MerchantContainer.h"
5
6MerchantContainer::MerchantContainer(shared_ptr<Player> player, shared_ptr<Merchant> villager)
7{
8 this->player = player;
9 merchant = villager;
10 items = ItemInstanceArray(3);
11 items[0] = nullptr;
12 items[1] = nullptr;
13 items[2] = nullptr;
14 activeRecipe = NULL;
15 selectionHint = 0;
16}
17
18MerchantContainer::~MerchantContainer()
19{
20 delete [] items.data;
21}
22
23unsigned int MerchantContainer::getContainerSize()
24{
25 return items.length;
26}
27
28shared_ptr<ItemInstance> MerchantContainer::getItem(unsigned int slot)
29{
30 return items[slot];
31}
32
33shared_ptr<ItemInstance> MerchantContainer::removeItem(unsigned int slot, int count)
34{
35 if (items[slot] != NULL)
36 {
37 if (slot == MerchantMenu::RESULT_SLOT)
38 {
39 shared_ptr<ItemInstance> item = items[slot];
40 items[slot] = nullptr;
41 return item;
42 }
43 if (items[slot]->count <= count)
44 {
45 shared_ptr<ItemInstance> item = items[slot];
46 items[slot] = nullptr;
47 if (isPaymentSlot(slot))
48 {
49 updateSellItem();
50 }
51 return item;
52 }
53 else
54 {
55 shared_ptr<ItemInstance> i = items[slot]->remove(count);
56 if (items[slot]->count == 0) items[slot] = nullptr;
57 if (isPaymentSlot(slot))
58 {
59 updateSellItem();
60 }
61 return i;
62 }
63 }
64 return nullptr;
65}
66
67bool MerchantContainer::isPaymentSlot(int slot)
68{
69 return slot == MerchantMenu::PAYMENT1_SLOT || slot == MerchantMenu::PAYMENT2_SLOT;
70}
71
72shared_ptr<ItemInstance> MerchantContainer::removeItemNoUpdate(int slot)
73{
74 if (items[slot] != NULL)
75 {
76 shared_ptr<ItemInstance> item = items[slot];
77 items[slot] = nullptr;
78 return item;
79 }
80 return nullptr;
81}
82
83void MerchantContainer::setItem(unsigned int slot, shared_ptr<ItemInstance> item)
84{
85 items[slot] = item;
86 if (item != NULL && item->count > getMaxStackSize()) item->count = getMaxStackSize();
87 if (isPaymentSlot(slot))
88 {
89 updateSellItem();
90 }
91}
92
93wstring MerchantContainer::getName()
94{
95 return merchant->getDisplayName();
96}
97
98wstring MerchantContainer::getCustomName()
99{
100 return L"";
101}
102
103bool MerchantContainer::hasCustomName()
104{
105 return false;
106}
107
108int MerchantContainer::getMaxStackSize() const
109{
110 return Container::LARGE_MAX_STACK_SIZE;
111}
112
113bool MerchantContainer::stillValid(shared_ptr<Player> player)
114{
115 return merchant->getTradingPlayer() == player;
116}
117
118void MerchantContainer::startOpen()
119{
120}
121
122void MerchantContainer::stopOpen()
123{
124}
125
126bool MerchantContainer::canPlaceItem(int slot, shared_ptr<ItemInstance> item)
127{
128 return true;
129}
130
131void MerchantContainer::setChanged()
132{
133 updateSellItem();
134}
135
136void MerchantContainer::updateSellItem()
137{
138 activeRecipe = NULL;
139
140 shared_ptr<ItemInstance> buyItem1 = items[MerchantMenu::PAYMENT1_SLOT];
141 shared_ptr<ItemInstance> buyItem2 = items[MerchantMenu::PAYMENT2_SLOT];
142
143 if (buyItem1 == NULL)
144 {
145 buyItem1 = buyItem2;
146 buyItem2 = nullptr;
147 }
148
149 if (buyItem1 == NULL)
150 {
151 setItem(MerchantMenu::RESULT_SLOT, nullptr);
152 }
153 else
154 {
155 MerchantRecipeList *offers = merchant->getOffers(player);
156 if (offers != NULL)
157 {
158 MerchantRecipe *recipeFor = offers->getRecipeFor(buyItem1, buyItem2, selectionHint);
159 if (recipeFor != NULL && !recipeFor->isDeprecated())
160 {
161 activeRecipe = recipeFor;
162 setItem(MerchantMenu::RESULT_SLOT, recipeFor->getSellItem()->copy());
163 }
164 else if (buyItem2 != NULL)
165 {
166 // try to switch
167 recipeFor = offers->getRecipeFor(buyItem2, buyItem1, selectionHint);
168 if (recipeFor != NULL && !recipeFor->isDeprecated())
169 {
170 activeRecipe = recipeFor;
171 setItem(MerchantMenu::RESULT_SLOT, recipeFor->getSellItem()->copy());
172 }
173 else
174 {
175 setItem(MerchantMenu::RESULT_SLOT, nullptr);
176 }
177
178 }
179 else
180 {
181 setItem(MerchantMenu::RESULT_SLOT, nullptr);
182 }
183 }
184 }
185
186 merchant->notifyTradeUpdated(getItem(MerchantMenu::RESULT_SLOT));
187}
188
189MerchantRecipe *MerchantContainer::getActiveRecipe()
190{
191 return activeRecipe;
192}
193
194void MerchantContainer::setSelectionHint(int selectionHint)
195{
196 this->selectionHint = selectionHint;
197 updateSellItem();
198}