/
ErrorUtil.java
120 lines (102 loc) · 3.52 KB
/
ErrorUtil.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
package mezz.jei.util;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
import mezz.jei.Internal;
import mezz.jei.api.recipe.IRecipeHandler;
import mezz.jei.api.recipe.IRecipeWrapper;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.ResourceLocation;
public class ErrorUtil {
@Nonnull
public static <T> String getInfoFromBrokenRecipe(@Nonnull T recipe, @Nonnull IRecipeHandler<T> recipeHandler) {
StringBuilder recipeInfoBuilder = new StringBuilder();
try {
recipeInfoBuilder.append(recipe);
} catch (RuntimeException e) {
Log.error("Failed recipe.toString", e);
recipeInfoBuilder.append(recipe.getClass());
}
IRecipeWrapper recipeWrapper;
try {
recipeWrapper = recipeHandler.getRecipeWrapper(recipe);
} catch (RuntimeException ignored) {
recipeInfoBuilder.append("\nFailed to create recipe wrapper");
return recipeInfoBuilder.toString();
}
recipeInfoBuilder.append("\nOutput ItemStacks: ");
try {
List outputs = recipeWrapper.getOutputs();
List<String> itemStackIngredientsInfo = getItemStackIngredientsInfo(outputs);
recipeInfoBuilder.append(itemStackIngredientsInfo);
} catch (RuntimeException e) {
recipeInfoBuilder.append(e.getMessage());
}
recipeInfoBuilder.append("\nOutput Fluids: ");
try {
recipeInfoBuilder.append(recipeWrapper.getFluidOutputs());
} catch (RuntimeException e) {
recipeInfoBuilder.append(e.getMessage());
}
recipeInfoBuilder.append("\nInput ItemStacks: ");
try {
List inputs = recipeWrapper.getInputs();
List<String> itemStackIngredientsInfo = getItemStackIngredientsInfo(inputs);
recipeInfoBuilder.append(itemStackIngredientsInfo);
} catch (RuntimeException e) {
recipeInfoBuilder.append(e.getMessage());
}
recipeInfoBuilder.append("\nInput Fluids: ");
try {
recipeInfoBuilder.append(recipeWrapper.getFluidInputs());
} catch (RuntimeException e) {
recipeInfoBuilder.append(e.getMessage());
}
return recipeInfoBuilder.toString();
}
public static List<String> getItemStackIngredientsInfo(@Nullable List list) {
if (list == null) {
return null;
}
StackHelper stackHelper = Internal.getStackHelper();
List<String> ingredientsInfo = new ArrayList<>();
for (Object ingredient : list) {
List<String> ingredientInfo = new ArrayList<>();
List<ItemStack> stacks = stackHelper.toItemStackList(ingredient);
String oreDict = stackHelper.getOreDictEquivalent(stacks);
if (oreDict != null) {
ingredientInfo.add("OreDict: " + oreDict);
}
for (ItemStack stack : stacks) {
String itemStackInfo = getItemStackInfo(stack);
ingredientInfo.add(itemStackInfo);
}
ingredientsInfo.add(ingredientInfo.toString() + "\n");
}
return ingredientsInfo;
}
public static String getItemStackInfo(@Nonnull ItemStack itemStack) {
Item item = itemStack.getItem();
if (item == null) {
return itemStack.stackSize + "x (null)";
}
final String itemName;
ResourceLocation registryName = item.getRegistryName();
if (registryName != null) {
itemName = registryName.toString();
} else if (item instanceof ItemBlock) {
itemName = "ItemBlock(" + ((ItemBlock) item).getBlock() + ")";
} else {
itemName = item.getClass().getName();
}
NBTTagCompound nbt = itemStack.getTagCompound();
if (nbt != null) {
return itemStack.toString() + " " + itemName + " nbt:" + nbt;
}
return itemStack.toString() + " " + itemName;
}
}