Skip to content

Commit 7b1ebde

Browse files
committed
Protect against more types of tooltip crashes
1 parent 4fa49b9 commit 7b1ebde

File tree

3 files changed

+72
-19
lines changed

3 files changed

+72
-19
lines changed

Common/src/main/java/mezz/jei/common/gui/JeiTooltip.java

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,8 @@
1515
import mezz.jei.common.config.DebugConfig;
1616
import mezz.jei.common.platform.IPlatformRenderHelper;
1717
import mezz.jei.common.platform.Services;
18-
import mezz.jei.common.util.ErrorUtil;
18+
import mezz.jei.common.util.SafeIngredientUtil;
1919
import net.minecraft.ChatFormatting;
20-
import net.minecraft.CrashReport;
21-
import net.minecraft.ReportedException;
2220
import net.minecraft.client.Minecraft;
2321
import net.minecraft.client.gui.Font;
2422
import net.minecraft.client.gui.GuiGraphics;
@@ -31,6 +29,7 @@
3129

3230
import java.util.ArrayList;
3331
import java.util.Collection;
32+
import java.util.Collections;
3433
import java.util.List;
3534
import java.util.stream.Collectors;
3635

@@ -179,15 +178,17 @@ public <T> void draw(
179178
if (isEmpty()) {
180179
return;
181180
}
182-
try {
183-
IPlatformRenderHelper renderHelper = Services.PLATFORM.getRenderHelper();
184-
renderHelper.renderTooltip(guiGraphics, lines, x, y, font, itemStack);
185-
} catch (RuntimeException e) {
186-
CrashReport crashReport = ErrorUtil.createIngredientCrashReport(e, "Rendering ingredient tooltip", ingredientManager, typedIngredient);
187-
crashReport.addCategory("tooltip")
188-
.setDetail("value", this);
189-
throw new ReportedException(crashReport);
190-
}
181+
182+
SafeIngredientUtil.renderTooltip(
183+
guiGraphics,
184+
this,
185+
x,
186+
y,
187+
font,
188+
itemStack,
189+
typedIngredient,
190+
ingredientManager
191+
);
191192
}
192193

193194
private <T> void addDebugInfo(IIngredientManager ingredientManager, ITypedIngredient<T> typedIngredient) {
@@ -221,4 +222,8 @@ private <T> void addDebugInfo(IIngredientManager ingredientManager, ITypedIngre
221222
);
222223
add(Component.empty());
223224
}
225+
226+
public List<Either<FormattedText, TooltipComponent>> getLines() {
227+
return Collections.unmodifiableList(lines);
228+
}
224229
}

Common/src/main/java/mezz/jei/common/util/ErrorUtil.java

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import mezz.jei.api.ingredients.IIngredientHelper;
44
import mezz.jei.api.ingredients.IIngredientType;
5-
import mezz.jei.api.ingredients.ITypedIngredient;
65
import mezz.jei.api.ingredients.subtypes.UidContext;
76
import mezz.jei.api.recipe.RecipeType;
87
import mezz.jei.api.runtime.IIngredientManager;
@@ -26,7 +25,9 @@
2625
import org.apache.logging.log4j.Logger;
2726
import org.jetbrains.annotations.Nullable;
2827

28+
import java.util.ArrayList;
2929
import java.util.Collection;
30+
import java.util.List;
3031

3132
public final class ErrorUtil {
3233
private static final Logger LOGGER = LogManager.getLogger();
@@ -169,10 +170,6 @@ public static <T> void validateRecipes(RecipeType<T> recipeType, Iterable<? exte
169170
}
170171
}
171172

172-
public static <T> CrashReport createIngredientCrashReport(Throwable throwable, String title, IIngredientManager ingredientManager, ITypedIngredient<T> typedIngredient) {
173-
return createIngredientCrashReport(throwable, title, ingredientManager, typedIngredient.getType(), typedIngredient.getIngredient());
174-
}
175-
176173
public static <T> CrashReport createIngredientCrashReport(Throwable throwable, String title, IIngredientManager ingredientManager, IIngredientType<T> ingredientType, T ingredient) {
177174
CrashReport crashReport = CrashReport.forThrowable(throwable, title);
178175
CrashReportCategory category = crashReport.addCategory("Ingredient");
@@ -183,7 +180,23 @@ public static <T> CrashReport createIngredientCrashReport(Throwable throwable, S
183180
public static <T> void logIngredientCrash(Throwable throwable, String title, IIngredientManager ingredientManager, IIngredientType<T> ingredientType, T ingredient) {
184181
CrashReportCategory category = new CrashReportCategory("Ingredient");
185182
setIngredientCategoryDetails(category, ingredientType, ingredient, ingredientManager);
186-
LOGGER.error(crashReportToString(throwable, title, category));
183+
LOGGER.error(crashReportToString(throwable, title, List.of(category)));
184+
}
185+
186+
public static <T> void logIngredientCrash(
187+
Throwable throwable,
188+
String title,
189+
IIngredientManager ingredientManager,
190+
IIngredientType<T> ingredientType,
191+
T ingredient,
192+
CrashReportCategory... extraCategories
193+
) {
194+
CrashReportCategory category = new CrashReportCategory("Ingredient");
195+
setIngredientCategoryDetails(category, ingredientType, ingredient, ingredientManager);
196+
List<CrashReportCategory> categoryList = new ArrayList<>();
197+
categoryList.add(category);
198+
categoryList.addAll(List.of(extraCategories));
199+
LOGGER.error(crashReportToString(throwable, title, categoryList));
187200
}
188201

189202
private static <T> void setIngredientCategoryDetails(CrashReportCategory category, IIngredientType<T> ingredientType, T ingredient, IIngredientManager ingredientManager) {
@@ -203,7 +216,7 @@ private static <T> void setIngredientCategoryDetails(CrashReportCategory categor
203216
category.setDetail("Error Info gathered from JEI", () -> ingredientHelper.getErrorInfo(ingredient));
204217
}
205218

206-
private static String crashReportToString(Throwable t, String title, CrashReportCategory... categories) {
219+
public static String crashReportToString(Throwable t, String title, Collection<CrashReportCategory> categories) {
207220
StringBuilder sb = new StringBuilder();
208221
sb.append(title);
209222
sb.append(":\n\n");

Common/src/main/java/mezz/jei/common/util/SafeIngredientUtil.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,19 @@
1010
import mezz.jei.common.Internal;
1111
import mezz.jei.common.config.IClientConfig;
1212
import mezz.jei.common.config.IJeiClientConfigs;
13+
import mezz.jei.common.gui.JeiTooltip;
14+
import mezz.jei.common.platform.IPlatformRenderHelper;
15+
import mezz.jei.common.platform.Services;
1316
import net.minecraft.ChatFormatting;
1417
import net.minecraft.CrashReport;
18+
import net.minecraft.CrashReportCategory;
1519
import net.minecraft.ReportedException;
1620
import net.minecraft.client.Minecraft;
1721
import net.minecraft.client.gui.Font;
1822
import net.minecraft.client.gui.GuiGraphics;
1923
import net.minecraft.network.chat.Component;
2024
import net.minecraft.network.chat.MutableComponent;
25+
import net.minecraft.world.item.ItemStack;
2126
import net.minecraft.world.item.TooltipFlag;
2227
import org.apache.logging.log4j.LogManager;
2328
import org.apache.logging.log4j.Logger;
@@ -69,6 +74,36 @@ public static <T> void getTooltip(
6974
}
7075
}
7176

77+
public static <T> void renderTooltip(
78+
GuiGraphics guiGraphics,
79+
JeiTooltip tooltip,
80+
int x,
81+
int y,
82+
Font font,
83+
ItemStack itemStack,
84+
ITypedIngredient<T> typedIngredient,
85+
IIngredientManager ingredientManager
86+
) {
87+
T ingredient = typedIngredient.getIngredient();
88+
IPlatformRenderHelper renderHelper = Services.PLATFORM.getRenderHelper();
89+
90+
if (CRASHING_INGREDIENT_TOOLTIPS.contains(ingredient)) {
91+
JeiTooltip errorTooltip = new JeiTooltip();
92+
getTooltipErrorTooltip(errorTooltip);
93+
renderHelper.renderTooltip(guiGraphics, errorTooltip.getLines(), x, y, font, ItemStack.EMPTY);
94+
return;
95+
}
96+
97+
try {
98+
renderHelper.renderTooltip(guiGraphics, tooltip.getLines(), x, y, font, itemStack);
99+
} catch (RuntimeException e) {
100+
CRASHING_INGREDIENT_TOOLTIPS.add(ingredient);
101+
CrashReportCategory category = new CrashReportCategory("tooltip");
102+
category.setDetail("value", tooltip);
103+
ErrorUtil.logIngredientCrash(e, "Rendering ingredient tooltip", ingredientManager, typedIngredient.getType(), ingredient, category);
104+
}
105+
}
106+
72107
private static void getTooltipErrorTooltip(ITooltipBuilder tooltip) {
73108
MutableComponent crash = Component.translatable("jei.tooltip.error.crash");
74109
tooltip.add(crash.withStyle(ChatFormatting.RED));

0 commit comments

Comments
 (0)