/
ErrorUtil.java
299 lines (264 loc) · 11.2 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
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
package mezz.jei.util;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import mezz.jei.api.ingredients.subtypes.UidContext;
import mezz.jei.ingredients.IngredientsForType;
import net.minecraftforge.registries.IForgeRegistryEntry;
import net.minecraft.block.Block;
import net.minecraft.client.Minecraft;
import net.minecraft.crash.CrashReport;
import net.minecraft.crash.CrashReportCategory;
import net.minecraft.crash.ReportedException;
import net.minecraft.item.BlockItem;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.IRecipe;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.util.NonNullList;
import net.minecraft.util.ResourceLocation;
import mezz.jei.Internal;
import mezz.jei.api.helpers.IModIdHelper;
import mezz.jei.api.ingredients.IIngredientHelper;
import mezz.jei.api.ingredients.IIngredientType;
import mezz.jei.api.ingredients.IIngredients;
import mezz.jei.api.recipe.category.IRecipeCategory;
import mezz.jei.api.runtime.IIngredientManager;
import mezz.jei.config.ClientConfig;
import mezz.jei.config.IWorldConfig;
import mezz.jei.ingredients.IngredientManager;
import mezz.jei.ingredients.Ingredients;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public final class ErrorUtil {
private static final Logger LOGGER = LogManager.getLogger();
@Nullable
private static IModIdHelper modIdHelper;
@Nullable
private static IWorldConfig worldConfig;
private ErrorUtil() {
}
public static void setModIdHelper(IModIdHelper modIdHelper) {
ErrorUtil.modIdHelper = modIdHelper;
}
public static void setWorldConfig(IWorldConfig worldConfig) {
ErrorUtil.worldConfig = worldConfig;
}
public static <T> String getInfoFromRecipe(T recipe, IRecipeCategory<T> recipeCategory) {
StringBuilder recipeInfoBuilder = new StringBuilder();
String recipeName = getNameForRecipe(recipe);
recipeInfoBuilder.append(recipeName);
Ingredients ingredients = new Ingredients();
try {
recipeCategory.setIngredients(recipe, ingredients);
} catch (RuntimeException | LinkageError ignored) {
recipeInfoBuilder.append("\nFailed to get ingredients from recipe wrapper");
return recipeInfoBuilder.toString();
}
recipeInfoBuilder.append("\nOutputs:");
List<IngredientsForType<?>> outputIngredients = ingredients.getOutputIngredients();
for (IngredientsForType<?> output : outputIngredients) {
IIngredientType<?> outputType = output.getIngredientType();
List<String> ingredientOutputInfo = getIngredientOutputInfo(outputType, ingredients);
recipeInfoBuilder.append('\n').append(outputType.getIngredientClass().getName()).append(": ").append(ingredientOutputInfo);
}
recipeInfoBuilder.append("\nInputs:");
List<IngredientsForType<?>> inputIngredients = ingredients.getInputIngredients();
for (IngredientsForType<?> input : inputIngredients) {
IIngredientType<?> inputType = input.getIngredientType();
List<String> ingredientInputInfo = getIngredientInputInfo(inputType, ingredients);
recipeInfoBuilder.append('\n').append(inputType.getIngredientClass().getName()).append(": ").append(ingredientInputInfo);
}
return recipeInfoBuilder.toString();
}
private static <T> List<String> getIngredientOutputInfo(IIngredientType<T> ingredientType, IIngredients ingredients) {
List<List<T>> outputs = ingredients.getOutputs(ingredientType);
return getIngredientInfo(ingredientType, outputs);
}
private static <T> List<String> getIngredientInputInfo(IIngredientType<T> ingredientType, IIngredients ingredients) {
List<List<T>> inputs = ingredients.getInputs(ingredientType);
return getIngredientInfo(ingredientType, inputs);
}
public static String getNameForRecipe(Object recipe) {
ResourceLocation registryName = null;
if (recipe instanceof IRecipe) {
registryName = ((IRecipe<?>) recipe).getId();
} else if (recipe instanceof IForgeRegistryEntry) {
IForgeRegistryEntry<?> registryEntry = (IForgeRegistryEntry<?>) recipe;
registryName = registryEntry.getRegistryName();
}
if (registryName != null) {
if (modIdHelper != null) {
String modId = registryName.getNamespace();
String modName = modIdHelper.getModNameForModId(modId);
return modName + " " + registryName + " " + recipe.getClass();
}
return registryName + " " + recipe.getClass();
}
try {
return recipe.toString();
} catch (RuntimeException e) {
LOGGER.error("Failed recipe.toString", e);
return recipe.getClass().toString();
}
}
public static <T> String getIngredientInfo(T ingredient) {
IIngredientHelper<T> ingredientHelper = Internal.getIngredientManager().getIngredientHelper(ingredient);
return ingredientHelper.getErrorInfo(ingredient);
}
public static <T> List<String> getIngredientInfo(IIngredientType<T> ingredientType, List<? extends List<T>> ingredients) {
IIngredientHelper<T> ingredientHelper = Internal.getIngredientManager().getIngredientHelper(ingredientType);
List<String> allInfos = new ArrayList<>(ingredients.size());
for (List<T> inputList : ingredients) {
List<String> infos = new ArrayList<>(inputList.size());
for (T input : inputList) {
String errorInfo = ingredientHelper.getErrorInfo(input);
infos.add(errorInfo);
}
allInfos.add(infos.toString());
}
return allInfos;
}
@SuppressWarnings("ConstantConditions")
public static String getItemStackInfo(@Nullable ItemStack itemStack) {
if (itemStack == null) {
return "null";
}
Item item = itemStack.getItem();
final String itemName;
ResourceLocation registryName = item.getRegistryName();
if (registryName != null) {
itemName = registryName.toString();
} else if (item instanceof BlockItem) {
final String blockName;
Block block = ((BlockItem) item).getBlock();
if (block == null) {
blockName = "null";
} else {
ResourceLocation blockRegistryName = block.getRegistryName();
if (blockRegistryName != null) {
blockName = blockRegistryName.toString();
} else {
blockName = block.getClass().getName();
}
}
itemName = "BlockItem(" + blockName + ")";
} else {
itemName = item.getClass().getName();
}
CompoundNBT nbt = itemStack.getTag();
if (nbt != null) {
return itemStack + " " + itemName + " nbt:" + nbt;
}
return itemStack + " " + itemName;
}
public static void checkNotEmpty(@Nullable ItemStack itemStack) {
if (itemStack == null) {
throw new NullPointerException("ItemStack must not be null.");
} else if (itemStack.isEmpty()) {
String info = getItemStackInfo(itemStack);
throw new IllegalArgumentException("ItemStack value must not be empty. " + info);
}
}
public static void checkNotEmpty(@Nullable ItemStack itemStack, String name) {
if (itemStack == null) {
throw new NullPointerException(name + " must not be null.");
} else if (itemStack.isEmpty()) {
String info = getItemStackInfo(itemStack);
throw new IllegalArgumentException("ItemStack " + name + " must not be empty. " + info);
}
}
public static <T> void checkNotEmpty(@Nullable T[] values, String name) {
if (values == null) {
throw new NullPointerException(name + " must not be null.");
} else if (values.length <= 0) {
throw new IllegalArgumentException(name + " must not be empty.");
}
for (T value : values) {
if (value == null) {
throw new NullPointerException(name + " must not contain null values.");
}
}
}
public static void checkNotEmpty(@Nullable Collection<?> values, String name) {
if (values == null) {
throw new NullPointerException(name + " must not be null.");
} else if (values.isEmpty()) {
throw new IllegalArgumentException(name + " must not be empty.");
} else if (!(values instanceof NonNullList)) {
for (Object value : values) {
if (value == null) {
throw new NullPointerException(name + " must not contain null values.");
}
}
}
}
public static <T> void checkNotNull(@Nullable T object, String name) {
if (object == null) {
throw new NullPointerException(name + " must not be null.");
}
}
public static void checkNotNull(@Nullable Collection<?> values, String name) {
if (values == null) {
throw new NullPointerException(name + " must not be null.");
} else if (!(values instanceof NonNullList)) {
for (Object value : values) {
if (value == null) {
throw new NullPointerException(name + " must not contain null values.");
}
}
}
}
public static <T> void checkIsValidIngredient(@Nullable T ingredient, String name) {
checkNotNull(ingredient, name);
IngredientManager ingredientManager = Internal.getIngredientManager();
IIngredientHelper<T> ingredientHelper = ingredientManager.getIngredientHelper(ingredient);
if (!ingredientHelper.isValidIngredient(ingredient)) {
String ingredientInfo = ingredientHelper.getErrorInfo(ingredient);
throw new IllegalArgumentException("Invalid ingredient found. Parameter Name: " + name + " Ingredient Info: " + ingredientInfo);
}
}
@SuppressWarnings("ConstantConditions")
public static void assertMainThread() {
Minecraft minecraft = Minecraft.getInstance();
if (minecraft != null && !minecraft.isOnExecutionThread()) {
Thread currentThread = Thread.currentThread();
throw new IllegalStateException(
"A JEI API method is being called by another mod from the wrong thread:\n" +
currentThread + "\n" +
"It must be called on the main thread by using Minecraft.addScheduledTask."
);
}
}
public static <T> ReportedException createRenderIngredientException(Throwable throwable, final T ingredient) {
IIngredientManager ingredientManager = Internal.getIngredientManager();
IIngredientType<T> ingredientType = ingredientManager.getIngredientType(ingredient);
IIngredientHelper<T> ingredientHelper = ingredientManager.getIngredientHelper(ingredientType);
CrashReport crashreport = CrashReport.makeCrashReport(throwable, "Rendering ingredient");
CrashReportCategory ingredientCategory = crashreport.makeCategory("Ingredient being rendered");
if (modIdHelper != null) {
ingredientCategory.addDetail("Mod Name", () -> {
String modId = ingredientHelper.getDisplayModId(ingredient);
return modIdHelper.getModNameForModId(modId);
});
}
ingredientCategory.addDetail("Registry Name", () -> {
String modId = ingredientHelper.getModId(ingredient);
String resourceId = ingredientHelper.getResourceId(ingredient);
return modId + ":" + resourceId;
});
ingredientCategory.addDetail("Display Name", () -> ingredientHelper.getDisplayName(ingredient));
ingredientCategory.addDetail("String Name", ingredient::toString);
CrashReportCategory jeiCategory = crashreport.makeCategory("JEI render details");
jeiCategory.addDetail("Unique Id (for Blacklist)", () -> ingredientHelper.getUniqueId(ingredient, UidContext.Ingredient));
jeiCategory.addDetail("Ingredient Type", () -> ingredientType.getIngredientClass().toString());
jeiCategory.addDetail("Error Info", () -> ingredientHelper.getErrorInfo(ingredient));
if (worldConfig != null) {
jeiCategory.addDetail("Filter Text", () -> worldConfig.getFilterText());
jeiCategory.addDetail("Edit Mode Enabled", () -> Boolean.toString(worldConfig.isEditModeEnabled()));
}
jeiCategory.addDetail("Debug Mode Enabled", () -> Boolean.toString(ClientConfig.getInstance().isDebugModeEnabled()));
throw new ReportedException(crashreport);
}
}