Skip to content

Commit 2413490

Browse files
committed
Fix #3931 Simplify tooltip handling and allow modifying rich tooltips
1 parent a17e830 commit 2413490

File tree

28 files changed

+156
-203
lines changed

28 files changed

+156
-203
lines changed

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,15 @@ public void addAll(Collection<? extends FormattedText> components) {
9494
}
9595

9696
@Override
97-
public void clear() {
98-
this.lines.clear();
97+
public void clearIngredient() {
9998
this.typedIngredient = null;
10099
}
101100

101+
@Override
102+
public List<Either<FormattedText, TooltipComponent>> getLines() {
103+
return lines;
104+
}
105+
102106
public void addAll(JeiTooltip tooltip) {
103107
lines.addAll(tooltip.lines);
104108
}
@@ -107,9 +111,8 @@ public boolean isEmpty() {
107111
return lines.isEmpty() && typedIngredient == null;
108112
}
109113

110-
@SuppressWarnings("removal")
111-
@Override
112-
public List<Component> toLegacyToComponents() {
114+
@Deprecated
115+
public List<Component> getLegacyComponents() {
113116
return lines.stream()
114117
.<Component>mapMulti((e, consumer) -> {
115118
e.left().ifPresent(f -> {
@@ -123,6 +126,14 @@ public List<Component> toLegacyToComponents() {
123126

124127
@SuppressWarnings("removal")
125128
@Override
129+
@Deprecated
130+
public List<Component> toLegacyToComponents() {
131+
return getLegacyComponents();
132+
}
133+
134+
@SuppressWarnings("removal")
135+
@Override
136+
@Deprecated
126137
public void removeAll(List<Component> components) {
127138
for (Component component : components) {
128139
lines.remove(Either.left(component));

Common/src/main/java/mezz/jei/common/platform/IPlatformFluidHelperInternal.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package mezz.jei.common.platform;
22

33
import com.mojang.serialization.Codec;
4-
import mezz.jei.api.gui.builder.ITooltipBuilder;
54
import mezz.jei.api.helpers.IPlatformFluidHelper;
65
import mezz.jei.api.ingredients.IIngredientRenderer;
76
import mezz.jei.api.ingredients.ITypedIngredient;
@@ -10,6 +9,7 @@
109
import net.minecraft.network.chat.Component;
1110
import net.minecraft.world.item.TooltipFlag;
1211

12+
import java.util.List;
1313
import java.util.Optional;
1414

1515
public interface IPlatformFluidHelperInternal<T> extends IPlatformFluidHelper<T> {
@@ -26,7 +26,7 @@ public interface IPlatformFluidHelperInternal<T> extends IPlatformFluidHelper<T>
2626

2727
DataComponentPatch getComponentsPatch(T ingredient);
2828

29-
void getTooltip(ITooltipBuilder tooltip, T ingredient, TooltipFlag tooltipFlag);
29+
void getTooltip(List<Component> tooltip, T ingredient, TooltipFlag tooltipFlag);
3030

3131
T copy(T ingredient);
3232

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

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import net.minecraft.world.item.TooltipFlag;
2424
import org.apache.logging.log4j.LogManager;
2525
import org.apache.logging.log4j.Logger;
26+
import org.jetbrains.annotations.Unmodifiable;
2627

2728
import java.util.HashSet;
2829
import java.util.List;
@@ -37,14 +38,14 @@ public final class SafeIngredientUtil {
3738
private SafeIngredientUtil() {
3839
}
3940

40-
public static <T> void getTooltip(ITooltipBuilder tooltip, IIngredientManager ingredientManager, IIngredientRenderer<T> ingredientRenderer, ITypedIngredient<T> typedIngredient) {
41+
public static <T> void getRichTooltip(ITooltipBuilder tooltip, IIngredientManager ingredientManager, IIngredientRenderer<T> ingredientRenderer, ITypedIngredient<T> typedIngredient) {
4142
Minecraft minecraft = Minecraft.getInstance();
4243
TooltipFlag.Default tooltipFlag = minecraft.options.advancedItemTooltips ? TooltipFlag.Default.ADVANCED : TooltipFlag.Default.NORMAL;
4344
tooltipFlag = tooltipFlag.asCreative();
44-
getTooltip(tooltip, ingredientManager, ingredientRenderer, typedIngredient, tooltipFlag);
45+
getRichTooltip(tooltip, ingredientManager, ingredientRenderer, typedIngredient, tooltipFlag);
4546
}
4647

47-
public static <T> void getTooltip(
48+
public static <T> void getRichTooltip(
4849
ITooltipBuilder tooltip,
4950
IIngredientManager ingredientManager,
5051
IIngredientRenderer<T> ingredientRenderer,
@@ -74,6 +75,28 @@ public static <T> void getTooltip(
7475
}
7576
}
7677

78+
@Unmodifiable
79+
public static <T> List<Component> getPlainTooltipForSearch(
80+
IIngredientManager ingredientManager,
81+
IIngredientRenderer<T> ingredientRenderer,
82+
ITypedIngredient<T> typedIngredient,
83+
TooltipFlag.Default tooltipFlag
84+
) {
85+
T ingredient = typedIngredient.getIngredient();
86+
87+
if (CRASHING_INGREDIENT_TOOLTIPS.contains(ingredient)) {
88+
return List.of();
89+
}
90+
91+
try {
92+
return ingredientRenderer.getTooltip(ingredient, tooltipFlag);
93+
} catch (RuntimeException | LinkageError e) {
94+
CRASHING_INGREDIENT_TOOLTIPS.add(ingredient);
95+
ErrorUtil.logIngredientCrash(e, "Caught an error getting an Ingredient's tooltip", ingredientManager, typedIngredient.getType(), ingredient);
96+
return List.of();
97+
}
98+
}
99+
77100
private static void getTooltipErrorTooltip(ITooltipBuilder tooltip) {
78101
MutableComponent crash = Component.translatable("jei.tooltip.error.crash");
79102
tooltip.add(crash.withStyle(ChatFormatting.RED));

CommonApi/src/main/java/mezz/jei/api/gui/builder/ITooltipBuilder.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package mezz.jei.api.gui.builder;
22

3+
import com.mojang.datafixers.util.Either;
34
import mezz.jei.api.ingredients.ITypedIngredient;
45
import net.minecraft.client.gui.screens.inventory.tooltip.ClientTooltipComponent;
56
import net.minecraft.network.chat.Component;
@@ -57,7 +58,27 @@ public interface ITooltipBuilder {
5758
*
5859
* @since 19.16.4
5960
*/
60-
void clear();
61+
default void clear() {
62+
clearIngredient();
63+
getLines().clear();
64+
}
65+
66+
/**
67+
* Remove the ingredient from this tooltip.
68+
*
69+
* @see #setIngredient(ITypedIngredient)
70+
*
71+
* @since 19.22.0
72+
*/
73+
void clearIngredient();
74+
75+
/**
76+
* Get the lines stored by this tooltip builder.
77+
* These lines are directly modifiable.
78+
*
79+
* @since 19.22.0
80+
*/
81+
List<Either<FormattedText, TooltipComponent>> getLines();
6182

6283
/**
6384
* @deprecated this is only for legacy tooltip support and will be removed

CommonApi/src/main/java/mezz/jei/api/gui/ingredient/IRecipeSlotDrawable.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,27 @@ public interface IRecipeSlotDrawable extends IRecipeSlotView {
4343
* Get the plain tooltip for this recipe slot.
4444
*
4545
* @since 11.5.0
46+
* @deprecated use {@link #drawTooltip}
4647
*/
48+
@Deprecated(since = "19.22.0", forRemoval = true)
4749
List<Component> getTooltip();
4850

4951
/**
5052
* Get the rich tooltip for this recipe slot.
5153
*
5254
* @since 19.5.4
55+
* @deprecated use {@link #drawTooltip}
5356
*/
57+
@Deprecated(since = "19.22.0", forRemoval = true)
5458
void getTooltip(ITooltipBuilder tooltipBuilder);
5559

60+
/**
61+
* Draw the tooltip for this recipe slot at the given mouse position.
62+
*
63+
* @since 19.22.0
64+
*/
65+
void drawTooltip(GuiGraphics guiGraphics, int mouseX, int mouseY);
66+
5667
/**
5768
* Return true if the mouse is over the slot.
5869
*

CommonApi/src/main/java/mezz/jei/api/gui/ingredient/IRecipeSlotTooltipCallback.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public interface IRecipeSlotTooltipCallback {
2424
* Change the tooltip for an ingredient.
2525
*
2626
* @since 9.3.0
27-
* @deprecated in favor of {@link IRecipeSlotRichTooltipCallback}
27+
* @deprecated use {@link IRecipeSlotRichTooltipCallback} instead
2828
*/
2929
@Deprecated(since = "19.5.4", forRemoval = true)
3030
void onTooltip(IRecipeSlotView recipeSlotView, List<Component> tooltip);
@@ -33,7 +33,7 @@ public interface IRecipeSlotTooltipCallback {
3333
* Add to the tooltip for an ingredient.
3434
*
3535
* @since 19.5.4
36-
* @deprecated in favor of {@link IRecipeSlotRichTooltipCallback}
36+
* @deprecated use {@link IRecipeSlotRichTooltipCallback} instead
3737
*/
3838
@Deprecated(since = "19.8.5", forRemoval = true)
3939
@SuppressWarnings({"removal", "DeprecatedIsStillUsed"})

CommonApi/src/main/java/mezz/jei/api/ingredients/IIngredientRenderer.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,12 @@ default void renderBatch(GuiGraphics guiGraphics, List<BatchRenderElement<T>> el
6363
}
6464

6565
/**
66-
* Get the tooltip text for this ingredient. JEI renders the tooltip based on this.
66+
* Get the tooltip text for this ingredient. JEI searches tooltips based on this.
6767
*
6868
* @param ingredient The ingredient to get the tooltip for.
6969
* @param tooltipFlag Whether to show advanced information on item tooltips, toggled by F3+H
7070
* @return The tooltip text for the ingredient.
71-
*
72-
* @deprecated use {@link #getTooltip(ITooltipBuilder, Object, TooltipFlag)}
7371
*/
74-
@Deprecated(since = "19.5.4", forRemoval = true)
7572
List<Component> getTooltip(T ingredient, TooltipFlag tooltipFlag);
7673

7774
/**

CommonApi/src/main/java/mezz/jei/api/recipe/category/extensions/IRecipeCategoryExtension.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public interface IRecipeCategoryExtension<T> {
3030
/**
3131
* Draw additional info about the recipe.
3232
* Use the mouse position for things like button highlights.
33-
* Tooltips are handled by {@link #getTooltipStrings(Object, double, double)}
33+
* Tooltips are handled by {@link #getTooltip(ITooltipBuilder, Object, double, double)}
3434
*
3535
* @param mouseX the X position of the mouse, relative to the recipe.
3636
* @param mouseY the Y position of the mouse, relative to the recipe.

Gui/src/main/java/mezz/jei/gui/ingredients/ListElementInfo.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import mezz.jei.common.util.SafeIngredientUtil;
1010
import mezz.jei.common.util.StringUtil;
1111
import mezz.jei.common.util.Translator;
12+
import net.minecraft.network.chat.Component;
13+
import net.minecraft.network.chat.FormattedText;
1214
import net.minecraft.resources.ResourceLocation;
1315
import net.minecraft.world.item.CreativeModeTab;
1416
import net.minecraft.world.item.CreativeModeTabs;
@@ -123,9 +125,8 @@ public final Set<String> getTooltipStrings(IIngredientFilterConfig config, IIngr
123125
TooltipFlag.Default tooltipFlag = config.getSearchAdvancedTooltips() ? TooltipFlag.Default.ADVANCED : TooltipFlag.Default.NORMAL;
124126
tooltipFlag = tooltipFlag.asCreative();
125127

126-
ListElementInfoTooltip tooltip = new ListElementInfoTooltip();
127-
SafeIngredientUtil.getTooltip(tooltip, ingredientManager, ingredientRenderer, value, tooltipFlag);
128-
Set<String> strings = tooltip.getStrings();
128+
List<Component> tooltip = SafeIngredientUtil.getPlainTooltipForSearch(ingredientManager, ingredientRenderer, value, tooltipFlag);
129+
Set<String> strings = getStrings(tooltip);
129130

130131
strings.remove(this.names.getFirst());
131132
strings.remove(this.modNames.getFirst().toLowerCase(Locale.ENGLISH));
@@ -135,6 +136,20 @@ public final Set<String> getTooltipStrings(IIngredientFilterConfig config, IIngr
135136
return strings;
136137
}
137138

139+
public static Set<String> getStrings(@Unmodifiable List<Component> tooltip) {
140+
Set<String> result = new HashSet<>();
141+
for (FormattedText component : tooltip) {
142+
String string = component.getString();
143+
string = StringUtil.removeChatFormatting(string);
144+
string = Translator.toLowercaseWithLocale(string);
145+
// Split tooltip strings into words to keep them from being too long.
146+
// Longer strings are more expensive for the suffix tree to handle.
147+
String[] strings = string.split(" ");
148+
Collections.addAll(result, strings);
149+
}
150+
return result;
151+
}
152+
138153
@Override
139154
public Collection<String> getTagStrings(IIngredientManager ingredientManager) {
140155
ITypedIngredient<V> value = element.getTypedIngredient();

Gui/src/main/java/mezz/jei/gui/ingredients/ListElementInfoTooltip.java

Lines changed: 0 additions & 80 deletions
This file was deleted.

0 commit comments

Comments
 (0)