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.h"
3#include "net.minecraft.world.item.crafting.h"
4#include "net.minecraft.world.entity.player.h"
5#include "ResultSlot.h"
6#include "ArmorSlot.h"
7#include "CraftingContainer.h"
8#include "ResultContainer.h"
9#include "InventoryMenu.h"
10#include "Tile.h"
11#include "GenericStats.h"
12
13const int InventoryMenu::RESULT_SLOT = 0;
14const int InventoryMenu::CRAFT_SLOT_START = 1;
15const int InventoryMenu::CRAFT_SLOT_END = InventoryMenu::CRAFT_SLOT_START + 4;
16const int InventoryMenu::ARMOR_SLOT_START = InventoryMenu::CRAFT_SLOT_END;
17const int InventoryMenu::ARMOR_SLOT_END = InventoryMenu::ARMOR_SLOT_START + 4;
18const int InventoryMenu::INV_SLOT_START = InventoryMenu::ARMOR_SLOT_END;
19const int InventoryMenu::INV_SLOT_END = InventoryMenu::INV_SLOT_START + 9 * 3;
20const int InventoryMenu::USE_ROW_SLOT_START = InventoryMenu::INV_SLOT_END;
21const int InventoryMenu::USE_ROW_SLOT_END = InventoryMenu::USE_ROW_SLOT_START + 9;
22
23InventoryMenu::InventoryMenu(shared_ptr<Inventory> inventory, bool active, Player *player) : AbstractContainerMenu()
24{
25 owner = player;
26 _init( inventory, active );
27}
28
29void InventoryMenu::_init(shared_ptr<Inventory> inventory, bool active)
30{
31 craftSlots = shared_ptr<CraftingContainer>( new CraftingContainer(this, 2, 2) );
32 resultSlots = shared_ptr<ResultContainer>( new ResultContainer() );
33
34 this->active = active;
35 addSlot(new ResultSlot( inventory->player, craftSlots, resultSlots, 0, 144, 36));
36
37 for (int y = 0; y < 2; y++)
38 {
39 for (int x = 0; x < 2; x++)
40 {
41 addSlot(new Slot(craftSlots, x + y * 2, 88 + x * 18, 26 + y * 18));
42 }
43 }
44
45 for (int i = 0; i < 4; i++)
46 {
47 // 4J Stu I removed an anonymous class that was here whose only purpose seemed to be a way of using the
48 // loop counter i within the functions, rather than making it a member of the object. I have moved all that
49 // out to the ArmorSlot class
50 addSlot(new ArmorSlot(i, inventory, inventory->getContainerSize() - 1 - i, 8, 8 + i * 18) );
51 }
52 for (int y = 0; y < 3; y++)
53 {
54 for (int x = 0; x < 9; x++)
55 {
56 addSlot(new Slot(inventory, x + (y + 1) * 9, 8 + x * 18, 84 + y * 18));
57 }
58 }
59 for (int x = 0; x < 9; x++)
60 {
61 addSlot(new Slot(inventory, x, 8 + x * 18, 142));
62 }
63
64 slotsChanged(); // 4J removed craftSlots parameter, see comment below
65}
66
67void InventoryMenu::slotsChanged() // 4J used to take a shared_ptr<Container> but wasn't using it, so removed to simplify things
68{
69 MemSect(23);
70 resultSlots->setItem(0, Recipes::getInstance()->getItemFor(craftSlots, owner->level) );
71 MemSect(0);
72}
73
74void InventoryMenu::removed(shared_ptr<Player> player)
75{
76 AbstractContainerMenu::removed(player);
77 for (int i = 0; i < 4; i++)
78 {
79 shared_ptr<ItemInstance> item = craftSlots->removeItemNoUpdate(i);
80 if (item != NULL)
81 {
82 player->drop(item);
83 craftSlots->setItem(i, nullptr);
84 }
85 }
86 resultSlots->setItem(0, nullptr);
87}
88
89bool InventoryMenu::stillValid(shared_ptr<Player> player)
90{
91 return true;
92}
93
94shared_ptr<ItemInstance> InventoryMenu::quickMoveStack(shared_ptr<Player> player, int slotIndex)
95{
96 shared_ptr<ItemInstance> clicked = nullptr;
97 Slot *slot = slots.at(slotIndex);
98
99 Slot *HelmetSlot = slots.at(ARMOR_SLOT_START);
100 Slot *ChestplateSlot = slots.at(ARMOR_SLOT_START+1);
101 Slot *LeggingsSlot = slots.at(ARMOR_SLOT_START+2);
102 Slot *BootsSlot = slots.at(ARMOR_SLOT_START+3);
103
104
105 if (slot != NULL && slot->hasItem())
106 {
107 shared_ptr<ItemInstance> stack = slot->getItem();
108 clicked = stack->copy();
109
110 if (slotIndex == RESULT_SLOT)
111 {
112 // 4J Stu - Brought forward change from 1.2
113 if(!moveItemStackTo(stack, INV_SLOT_START, USE_ROW_SLOT_END, true))
114 {
115 return nullptr;
116 }
117 slot->onQuickCraft(stack, clicked);
118 }
119 else if (slotIndex >= INV_SLOT_START && slotIndex < INV_SLOT_END)
120 {
121 // 4J-PB - added for quick equip
122 if(ArmorRecipes::GetArmorType(stack->id)==ArmorRecipes::eArmorType_Helmet && (!HelmetSlot->hasItem() ) )
123 {
124 if(!moveItemStackTo(stack, ARMOR_SLOT_START, ARMOR_SLOT_START+1, false))
125 {
126 return nullptr;
127 }
128 }
129 else if(ArmorRecipes::GetArmorType(stack->id)==ArmorRecipes::eArmorType_Chestplate && (!ChestplateSlot->hasItem() ) )
130 {
131 if(!moveItemStackTo(stack, ARMOR_SLOT_START+1, ARMOR_SLOT_START+2, false))
132 {
133 return nullptr;
134 }
135 }
136 else if(ArmorRecipes::GetArmorType(stack->id)==ArmorRecipes::eArmorType_Leggings && (!LeggingsSlot->hasItem() ) )
137 {
138 if(!moveItemStackTo(stack, ARMOR_SLOT_START+2, ARMOR_SLOT_START+3, false))
139 {
140 return nullptr;
141 }
142 }
143 else if(ArmorRecipes::GetArmorType(stack->id)==ArmorRecipes::eArmorType_Boots && (!BootsSlot->hasItem() ) )
144 {
145 if(!moveItemStackTo(stack, ARMOR_SLOT_START+3, ARMOR_SLOT_START+4, false))
146 {
147 return nullptr;
148 }
149 }
150 // 4J Stu - Brought forward change from 1.2
151 else if(!moveItemStackTo(stack, USE_ROW_SLOT_START, USE_ROW_SLOT_END, false))
152 {
153 return nullptr;
154 }
155 }
156 else if (slotIndex >= USE_ROW_SLOT_START && slotIndex < USE_ROW_SLOT_END)
157 {
158 //ArmorRecipes::_eArmorType eArmourType=ArmorRecipes::GetArmorType(stack->id);
159
160 if(ArmorRecipes::GetArmorType(stack->id)==ArmorRecipes::eArmorType_Helmet && (!HelmetSlot->hasItem() ) )
161 {
162 if(!moveItemStackTo(stack, ARMOR_SLOT_START, ARMOR_SLOT_START+1, false))
163 {
164 return nullptr;
165 }
166 }
167 else if(ArmorRecipes::GetArmorType(stack->id)==ArmorRecipes::eArmorType_Chestplate && (!ChestplateSlot->hasItem() ) )
168 {
169 if(!moveItemStackTo(stack, ARMOR_SLOT_START+1, ARMOR_SLOT_START+2, false))
170 {
171 return nullptr;
172 }
173 }
174 else if(ArmorRecipes::GetArmorType(stack->id)==ArmorRecipes::eArmorType_Leggings && (!LeggingsSlot->hasItem() ) )
175 {
176 if(!moveItemStackTo(stack, ARMOR_SLOT_START+2, ARMOR_SLOT_START+3, false))
177 {
178 return nullptr;
179 }
180 }
181 else if(ArmorRecipes::GetArmorType(stack->id)==ArmorRecipes::eArmorType_Boots && (!BootsSlot->hasItem() ) )
182 {
183 if(!moveItemStackTo(stack, ARMOR_SLOT_START+3, ARMOR_SLOT_START+4, false))
184 {
185 return nullptr;
186 }
187 }
188 // 4J Stu - Brought forward change from 1.2
189 else if(!moveItemStackTo(stack, INV_SLOT_START, INV_SLOT_END, false))
190 {
191 return nullptr;
192 }
193 }
194 else
195 {
196 // 4J Stu - Brought forward change from 1.2
197 if(!moveItemStackTo(stack, INV_SLOT_START, USE_ROW_SLOT_END, false))
198 {
199 return nullptr;
200 }
201 }
202 if (stack->count == 0)
203 {
204 slot->set(nullptr);
205 }
206 else
207 {
208 slot->setChanged();
209 }
210 if (stack->count == clicked->count)
211 {
212 // nothing moved
213 return nullptr;
214 }
215 else
216 {
217 slot->onTake(player, stack);
218 }
219 }
220 return clicked;
221}
222
223bool InventoryMenu::mayCombine(Slot *slot, shared_ptr<ItemInstance> item)
224{
225 return slot->mayCombine(item);
226}
227
228bool InventoryMenu::canTakeItemForPickAll(shared_ptr<ItemInstance> carried, Slot *target)
229{
230 return target->container != resultSlots && AbstractContainerMenu::canTakeItemForPickAll(carried, target);
231}
232
233// 4J-JEV: Added for achievement 'Iron Man'.
234shared_ptr<ItemInstance> InventoryMenu::clicked(int slotIndex, int buttonNum, int clickType, shared_ptr<Player> player, bool looped) // 4J Added looped param
235{
236 shared_ptr<ItemInstance> out = AbstractContainerMenu::clicked(slotIndex, buttonNum, clickType, player, looped);
237
238#ifdef _EXTENDED_ACHIEVEMENTS
239 static int ironItems[4] = {Item::helmet_iron_Id,Item::chestplate_iron_Id,Item::leggings_iron_Id,Item::boots_iron_Id};
240 for (int i = ARMOR_SLOT_START; i < ARMOR_SLOT_END; i++)
241 {
242 Slot *slot = slots.at(i);
243 if ( (slot==NULL) || (!slot->hasItem()) || (slot->getItem()->getItem()->id != ironItems[i-ARMOR_SLOT_START]) )
244 {
245 return out;
246 }
247 }
248 player->awardStat(GenericStats::ironMan(),GenericStats::param_ironMan());
249#endif
250
251 return out;
252}