/
CraftingHandler.java
329 lines (315 loc) · 12.9 KB
/
CraftingHandler.java
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
package mantle.utils;
import static mantle.lib.CoreRepo.logger;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import cpw.mods.fml.common.registry.GameRegistry;
import mantle.lib.CoreConfig;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.CraftingManager;
import net.minecraft.item.crafting.IRecipe;
import net.minecraft.item.crafting.ShapedRecipes;
import net.minecraft.item.crafting.ShapelessRecipes;
import net.minecraftforge.oredict.OreDictionary;
import net.minecraftforge.oredict.ShapedOreRecipe;
import net.minecraftforge.oredict.ShapelessOreRecipe;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class CraftingHandler {
private static boolean ia1NG;
public static void logConflicts () {
//GameRegistry.addShapelessRecipe(new ItemStack(Items.diamond_hoe), Blocks.planks, Items.stick, Items.chainmail_boots);
//GameRegistry.addShapelessRecipe(new ItemStack(Items.apple), Blocks.planks, Items.stick, Items.chainmail_boots);
if (CoreConfig.dumpRecipeConflicts) {
for (Map.Entry<IRecipe, List<IRecipe>> me : getPotentialConflicts().entrySet()) {
String s = "";
for (IRecipe i : me.getValue()) {
s += print(i);
}
logger.error("found conflict: \n" + s);
}
}
}
public static Map<IRecipe, List<IRecipe>> getPotentialConflicts () {
logger.info("dumping recipe conflicts to log this can take a while");
Map<IRecipe, List<IRecipe>> conflicts = Maps.newHashMap();
CraftingManager inst = CraftingManager.getInstance();
List<IRecipe> recipes = inst.getRecipeList();
for (IRecipe i : recipes) {
for (int r = 0; r < recipes.size(); r++) {
IRecipe rc = recipes.get(r);
if (i != null && rc != null) {
boolean comp = compare(i, rc);
if (comp && !areItemStacksEqual(i.getRecipeOutput(), rc.getRecipeOutput())) {
if (conflicts.containsKey(i)) {
List<IRecipe> l = conflicts.get(i);
conflicts.remove(i);
if (!l.contains(r)) {
l.add(recipes.get(r));
}
conflicts.put(i, l);
} else if (conflicts.containsKey(rc)) {
List<IRecipe> l = conflicts.get(rc);
conflicts.remove(rc);
if (!l.contains(i)) {
l.add(i);
}
conflicts.put(rc, l);
} else {
List<IRecipe> l = Lists.newArrayList(i, rc);
conflicts.put(i, l);
}
if (ia1NG) {
ia1NG = false;
break;
}
}
}
}
}
return conflicts;
}
private static boolean compare (IRecipe i1, IRecipe i2) {
if (areItemStacksEqual(i1.getRecipeOutput(), i2.getRecipeOutput())) {
return false;
}
boolean is1l = false;
boolean is2l = false;
List<ItemStack> il1 = Lists.newArrayList();
List<ItemStack> il2 = Lists.newArrayList();
ItemStack[] ia1 = null;
boolean invalid = false;
ItemStack[] ia2 = null;
if (i1 instanceof ShapedRecipes) {
ia1 = ((ShapedRecipes) i1).recipeItems.clone();
} else if (i1 instanceof ShapelessRecipes) {
il1 = new ArrayList<ItemStack>(((ShapelessRecipes) i1).recipeItems);
//logger.error("SL Recipe: \n" + print(i1));
is1l = true;
} else if (i1 instanceof ShapedOreRecipe) {
Object[] o = ((ShapedOreRecipe) i1).getInput();
ia1 = new ItemStack[o.length];
for (int in = 0; in < o.length; in++) {
if (o[in] != null && o[in] instanceof ItemStack) {
ia1[in] = (ItemStack) o[in];
} else if (o[in] != null) {
ia1[in] = ((ArrayList<ItemStack>) o[in]).get(0);
} else {
ia1[in] = null;
}
}
} else if (i1 instanceof ShapelessOreRecipe) {
il1 = new ArrayList<ItemStack>();
for (Object o : ((ShapelessOreRecipe) i1).getInput()) {
if (o != null && o instanceof ItemStack) {
ItemStack is = (ItemStack) o;
il1.add(is);
} else if (o != null) {
ItemStack is = ((ArrayList<ItemStack>) o).get(0);
il1.add(is);
} else {
il1.add(null);
}
}
is1l = true;
} else {
ia1NG = true;
invalid = true;
}
if (i2 instanceof ShapedRecipes) {
ia2 = ((ShapedRecipes) i2).recipeItems.clone();
} else if (i2 instanceof ShapelessRecipes) {
il2 = new ArrayList<ItemStack>(((ShapelessRecipes) i2).recipeItems);
is2l = true;
} else if (i2 instanceof ShapedOreRecipe) {
Object[] o = ((ShapedOreRecipe) i2).getInput();
ia2 = new ItemStack[o.length];
for (int in = 0; in < o.length; in++) {
if (o.length == 6) {
print(i2);
}
if (o[in] != null && o[in] instanceof ItemStack) {
ia2[in] = (ItemStack) o[in];
} else if (o[in] != null) {
ItemStack is = ((ArrayList<ItemStack>) o[in]).get(0);
ia2[in] = is;
} else {
ia2[in] = null;
}
}
} else if (i2 instanceof ShapelessOreRecipe) {
il2 = new ArrayList<ItemStack>();
for (Object o : ((ShapelessOreRecipe) i2).getInput()) {
if (o != null && o instanceof ItemStack) {
ItemStack is = (ItemStack) o;
il2.add(is);
} else if (o != null) {
ItemStack is = ((ArrayList<ItemStack>) o).get(0);
il2.add(is);
} else {
il2.add(null);
}
}
is2l = true;
} else {
invalid = true;
}
if (!invalid) {
if (!is1l && !is2l) {
if (ia1 == null) {
logger.error("ia1 null");
}
if (ia2 == null) {
logger.error("ia2 null");
}
if (ia1.length != ia2.length) {
return false;
}
for (int i = 0; i < ia1.length; i++) {
if (ia1[i] != null && ia2[i] != null) {
if (!areItemStacksEqual(ia1[i], ia2[i])) {
return false;
}
} else {
return false;
}
}
} else if (is1l && is2l) {
if (il1.size() == il2.size()) {
boolean has = false;
for (int i = 0; i < il1.size(); i++) {
ItemStack ti1 = il1.get(i);
for (int j = 0; j < il2.size(); j++) {
if (areItemStacksEqual(ti1, il2.get(j))) {
il2.remove(j);
has = true;
break;
}
}
}
if (!has) {
return false;
}
if (il2.size() != 0) {
return false;
}
} else {
return false;
}
} else {
List<ItemStack> stacks = is1l ? il1 : il2;
ItemStack[] stcks = is2l ? ia1 : ia2;
for (ItemStack i : stcks) {
if (!stacks.contains(i)) {
return false;
} else {
stacks.remove(i);
}
}
}
} else {
return false;
}
if (i1 instanceof ShapedOreRecipe && i2 instanceof ShapedOreRecipe) {
int i1w, i2w, i1h, i2h;
try {
Class cl = ShapedOreRecipe.class;
Field w = cl.getDeclaredField("width");
w.setAccessible(true);
Field h = cl.getDeclaredField("height");
h.setAccessible(true);
i1w = (Integer) w.get((ShapedOreRecipe) i1);
i2w = (Integer) w.get((ShapedOreRecipe) i2);
i1h = (Integer) h.get((ShapedOreRecipe) i1);
i2h = (Integer) h.get((ShapedOreRecipe) i2);
if (i1w != i2w || i1h != i2h) {
return false;
}
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
}
//logger.error("CONFLICT SET: \n" + print(i1) + print(i2));
return true;
}
private static String print (IRecipe i1) {
String out = "";
if (i1 instanceof ShapedRecipes) {
out += "SHAPED " + toStr(i1.getRecipeOutput()) + "\n";
for (ItemStack i : ((ShapedRecipes) i1).recipeItems) {
if (i != null && i.getItem() != null) {
out += toStr((ItemStack) i) + "\n";
} else {
out += "null \n";
}
}
} else if (i1 instanceof ShapelessRecipes) {
out += "SHAPELESS " + toStr(i1.getRecipeOutput()) + "\n";
for (Object i : ((ShapelessRecipes) i1).recipeItems) {
if ((ItemStack) i != null && ((ItemStack) i).getItem() != null) {
out += toStr((ItemStack) i) + "\n";
} else {
out += "null \n";
}
}
} else if (i1 instanceof ShapedOreRecipe) {
out += "SHAPED ORE " + toStr(i1.getRecipeOutput()) + "\n";
Object[] o = ((ShapedOreRecipe) i1).getInput();
ItemStack[] ia1 = new ItemStack[o.length];
for (int in = 0; in < o.length; in++) {
if (o[in] != null && o[in] instanceof ItemStack) {
ia1[in] = (ItemStack) o[in];
} else if (o[in] != null) {
ia1[in] = ((ArrayList<ItemStack>) o[in]).get(0);
} else {
ia1[in] = null;
}
}
for (Object i : ia1) {
if (i != null && ((ItemStack) i).getItem() != null) {
out += toStr((ItemStack) i) + "\n";
} else {
out += "null \n";
}
}
} else if (i1 instanceof ShapelessOreRecipe) {
out += "SHAPELESS ORE " + toStr(i1.getRecipeOutput()) + "\n";
ItemStack i;
for (Object ob : ((ShapelessOreRecipe) i1).getInput()) {
if (ob instanceof ItemStack) {
i = (ItemStack) ob;
} else {
i = ((ArrayList<ItemStack>) ob).get(0);
}
if ((ItemStack) i != null && ((ItemStack) i).getItem() != null) {
out += toStr((ItemStack) i) + "\n";
} else {
out += "null \n";
}
}
}
return out;
}
private static String toStr (ItemStack is) {
return GameRegistry.findUniqueIdentifierFor(((ItemStack) is).getItem()) + " " + ((ItemStack) is).toString();
}
public static boolean areItemStacksEqual (ItemStack a, ItemStack b) {
if ((a == null || b == null) && !(a == null && b == null)) {
return false;
}
if ((a == null && b == null)) {
return true;
}
return a.stackSize != b.stackSize
? false
: (a.getItem() != b.getItem()
? false
: (compdamage(a, b)
? false
: (a.stackTagCompound == null && b.stackTagCompound != null ? false : a.stackTagCompound == null || a.stackTagCompound.equals(b.stackTagCompound))));
}
public static boolean compdamage (ItemStack a, ItemStack b) {
return a.getItemDamage() != b.getItemDamage() || a.getItemDamage() == OreDictionary.WILDCARD_VALUE || b.getItemDamage() == OreDictionary.WILDCARD_VALUE;
}
}