the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
1// package net.minecraft.world.item.crafting;
2//
3// import net.minecraft.world.inventory.CraftingContainer;
4// import net.minecraft.world.item.ItemInstance;
5
6#include "stdafx.h"
7#include "net.minecraft.world.item.h"
8#include "net.minecraft.world.inventory.h"
9#include "Tile.h"
10#include "Recipy.h"
11#include "Recipes.h"
12#include "ShapedRecipy.h"
13
14// 4J-PB - for new crafting - Adding group to define type of item that the recipe produces
15ShapedRecipy::ShapedRecipy(int width, int height, ItemInstance **recipeItems, ItemInstance *result, int iGroup)
16 : resultId(result->id)
17{
18 this->width = width;
19 this->height = height;
20 this->recipeItems = recipeItems;
21 this->result = result;
22 this->group = iGroup;
23 _keepTag = false;
24}
25
26const int ShapedRecipy::getGroup()
27{
28 return group;
29}
30
31const ItemInstance *ShapedRecipy::getResultItem()
32{
33 return result;
34}
35
36bool ShapedRecipy::matches(shared_ptr<CraftingContainer> craftSlots, Level *level)
37{
38 for (int xOffs = 0; xOffs <= (3 - width); xOffs++)
39 {
40 for (int yOffs = 0; yOffs <= (3 - height); yOffs++)
41 {
42 if (matches(craftSlots, xOffs, yOffs, true)) return true;
43 if (matches(craftSlots, xOffs, yOffs, false)) return true;
44 }
45 }
46 return false;
47}
48
49bool ShapedRecipy::matches(shared_ptr<CraftingContainer> craftSlots, int xOffs, int yOffs, bool xFlip)
50{
51 for (int x = 0; x < 3; x++) {
52 for (int y = 0; y < 3; y++) {
53 int xs = x - xOffs;
54 int ys = y - yOffs;
55 ItemInstance *expected = NULL;
56 if (xs >= 0 && ys >= 0 && xs < width && ys < height)
57 {
58 if (xFlip) expected = recipeItems[(width - xs - 1) + ys * width];
59 else expected = recipeItems[xs + ys * width];
60 }
61 shared_ptr<ItemInstance> item = craftSlots->getItem(x, y);
62 if (item == NULL && expected == NULL)
63 {
64 continue;
65 }
66 if ((item == NULL && expected != NULL) || (item != NULL && expected == NULL))
67 {
68 return false;
69 }
70 if (expected->id != item->id)
71 {
72 return false;
73 }
74 if (expected->getAuxValue() != Recipes::ANY_AUX_VALUE && expected->getAuxValue() != item->getAuxValue())
75 {
76 return false;
77 }
78 }
79 }
80 return true;
81}
82
83shared_ptr<ItemInstance> ShapedRecipy::assemble(shared_ptr<CraftingContainer> craftSlots)
84{
85 shared_ptr<ItemInstance> result = getResultItem()->copy();
86
87 if (_keepTag && craftSlots != NULL)
88 {
89 for (int i = 0; i < craftSlots->getContainerSize(); i++)
90 {
91 shared_ptr<ItemInstance> item = craftSlots->getItem(i);
92
93 if (item != NULL && item->hasTag())
94 {
95 result->setTag((CompoundTag *) item->tag->copy());
96 }
97 }
98 }
99
100 return result;
101}
102
103int ShapedRecipy::size()
104{
105 return width * height;
106}
107
108// 4J-PB
109bool ShapedRecipy::requires(int iRecipe)
110{
111 app.DebugPrintf("ShapedRecipy %d\n",iRecipe);
112 int iCount=0;
113 for (int x = 0; x < 3; x++)
114 {
115 for (int y = 0; y < 3; y++)
116 {
117 if (x < width && y < height)
118 {
119 ItemInstance *expected = recipeItems[x+y*width];
120 if (expected!=NULL)
121 {
122 //printf("\tIngredient %d is %d\n",iCount++,expected->id);
123 }
124 }
125 }
126 }
127
128
129
130 return false;
131}
132
133void ShapedRecipy::requires(INGREDIENTS_REQUIRED *pIngReq)
134{
135 //printf("ShapedRecipy %d\n",iRecipe);
136
137 int iCount=0;
138 bool bFound;
139 int j;
140 INGREDIENTS_REQUIRED TempIngReq;
141 TempIngReq.iIngC=0;
142 TempIngReq.iType = ((width>2) ||(height>2))?RECIPE_TYPE_3x3:RECIPE_TYPE_2x2; // 3x3
143 // 3x3
144 TempIngReq.uiGridA = new unsigned int [9];
145 TempIngReq.iIngIDA= new int [9];
146 TempIngReq.iIngValA = new int [9];
147 TempIngReq.iIngAuxValA = new int [9];
148
149 ZeroMemory(TempIngReq.iIngIDA,sizeof(int)*9);
150 ZeroMemory(TempIngReq.iIngValA,sizeof(int)*9);
151 memset(TempIngReq.iIngAuxValA,Recipes::ANY_AUX_VALUE,sizeof(int)*9);
152 ZeroMemory(TempIngReq.uiGridA,sizeof(unsigned int)*9);
153
154 for (int x = 0; x < 3; x++)
155 {
156 for (int y = 0; y < 3; y++)
157 {
158 if (x < width && y < height)
159 {
160 ItemInstance *expected = recipeItems[x+y*width];
161
162 if (expected!=NULL)
163 {
164 int iAuxVal = expected->getAuxValue();
165 TempIngReq.uiGridA[x+y*3]=expected->id | iAuxVal<<24;
166
167 bFound=false;
168 for(j=0;j<TempIngReq.iIngC;j++)
169 {
170 if((TempIngReq.iIngIDA[j]==expected->id) && (iAuxVal == Recipes::ANY_AUX_VALUE || TempIngReq.iIngAuxValA[j] == iAuxVal))
171 {
172 bFound= true;
173 break;
174 }
175 }
176 if(bFound)
177 {
178 TempIngReq.iIngValA[j]++;
179 }
180 else
181 {
182 TempIngReq.iIngIDA[TempIngReq.iIngC]=expected->id;
183 TempIngReq.iIngAuxValA[TempIngReq.iIngC]=iAuxVal;
184 TempIngReq.iIngValA[TempIngReq.iIngC++]++;
185 }
186 //printf("\tIngredient %d is %d\n",iCount++,expected->id);
187 }
188
189 }
190 }
191 }
192 pIngReq->iIngIDA= new int [TempIngReq.iIngC];
193 pIngReq->iIngValA= new int [TempIngReq.iIngC];
194 pIngReq->iIngAuxValA = new int [TempIngReq.iIngC];
195 pIngReq->uiGridA = new unsigned int [9];
196
197 pIngReq->iIngC=TempIngReq.iIngC;
198 pIngReq->iType=TempIngReq.iType;
199
200 pIngReq->pRecipy=this;
201
202 for(unsigned int i = 0; i < XUSER_MAX_COUNT; ++i)
203 {
204 pIngReq->bCanMake[i]=false;
205 }
206
207 for(j=0;j<9;j++)
208 {
209 pIngReq->uiGridA[j]=TempIngReq.uiGridA[j];
210 }
211
212 if(pIngReq->iIngC!=0)
213 {
214 memcpy(pIngReq->iIngIDA,TempIngReq.iIngIDA,sizeof(int)*TempIngReq.iIngC);
215 memcpy(pIngReq->iIngValA,TempIngReq.iIngValA,sizeof(int)*TempIngReq.iIngC);
216 memcpy(pIngReq->iIngAuxValA,TempIngReq.iIngAuxValA,sizeof(int)*TempIngReq.iIngC);
217 }
218 memcpy(pIngReq->uiGridA,TempIngReq.uiGridA,sizeof(unsigned int)*9);
219
220 delete [] TempIngReq.iIngIDA;
221 delete [] TempIngReq.iIngValA;
222 delete [] TempIngReq.iIngAuxValA;
223 delete [] TempIngReq.uiGridA;
224}
225
226ShapedRecipy *ShapedRecipy::keepTag()
227{
228 _keepTag = true;
229 return this;
230}