Skip to content

Commit a3fea2c

Browse files
committed
Simplify tooltip handling and allow modifying rich tooltips
1 parent 65a1a7e commit a3fea2c

File tree

28 files changed

+155
-200
lines changed

28 files changed

+155
-200
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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import net.minecraft.network.chat.Component;
1111
import net.minecraft.world.item.TooltipFlag;
1212

13+
import java.util.List;
1314
import java.util.Optional;
1415

1516
public interface IPlatformFluidHelperInternal<T> extends IPlatformFluidHelper<T> {
@@ -26,7 +27,7 @@ public interface IPlatformFluidHelperInternal<T> extends IPlatformFluidHelper<T>
2627

2728
DataComponentPatch getComponentsPatch(T ingredient);
2829

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

3132
T copy(T ingredient);
3233

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

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

2425
import java.util.HashSet;
2526
import java.util.List;
@@ -34,14 +35,14 @@ public final class SafeIngredientUtil {
3435
private SafeIngredientUtil() {
3536
}
3637

37-
public static <T> void getTooltip(ITooltipBuilder tooltip, IIngredientManager ingredientManager, IIngredientRenderer<T> ingredientRenderer, ITypedIngredient<T> typedIngredient) {
38+
public static <T> void getRichTooltip(ITooltipBuilder tooltip, IIngredientManager ingredientManager, IIngredientRenderer<T> ingredientRenderer, ITypedIngredient<T> typedIngredient) {
3839
Minecraft minecraft = Minecraft.getInstance();
3940
TooltipFlag.Default tooltipFlag = minecraft.options.advancedItemTooltips ? TooltipFlag.Default.ADVANCED : TooltipFlag.Default.NORMAL;
4041
tooltipFlag = tooltipFlag.asCreative();
41-
getTooltip(tooltip, ingredientManager, ingredientRenderer, typedIngredient, tooltipFlag);
42+
getRichTooltip(tooltip, ingredientManager, ingredientRenderer, typedIngredient, tooltipFlag);
4243
}
4344

44-
public static <T> void getTooltip(
45+
public static <T> void getRichTooltip(
4546
ITooltipBuilder tooltip,
4647
IIngredientManager ingredientManager,
4748
IIngredientRenderer<T> ingredientRenderer,
@@ -68,6 +69,28 @@ public static <T> void getTooltip(
6869
}
6970
}
7071

72+
@Unmodifiable
73+
public static <T> List<Component> getPlainTooltipForSearch(
74+
IIngredientManager ingredientManager,
75+
IIngredientRenderer<T> ingredientRenderer,
76+
ITypedIngredient<T> typedIngredient,
77+
TooltipFlag.Default tooltipFlag
78+
) {
79+
T ingredient = typedIngredient.getIngredient();
80+
81+
if (CRASHING_INGREDIENT_TOOLTIPS.contains(ingredient)) {
82+
return List.of();
83+
}
84+
85+
try {
86+
return ingredientRenderer.getTooltip(ingredient, tooltipFlag);
87+
} catch (RuntimeException | LinkageError e) {
88+
CRASHING_INGREDIENT_TOOLTIPS.add(ingredient);
89+
ErrorUtil.logIngredientCrash(e, "Caught an error getting an Ingredient's tooltip", ingredientManager, typedIngredient.getType(), ingredient);
90+
return List.of();
91+
}
92+
}
93+
7194
private static void getTooltipErrorTooltip(ITooltipBuilder tooltip) {
7295
MutableComponent crash = Component.translatable("jei.tooltip.error.crash");
7396
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;
@@ -56,7 +57,27 @@ public interface ITooltipBuilder {
5657
*
5758
* @since 19.16.4
5859
*/
59-
void clear();
60+
default void clear() {
61+
clearIngredient();
62+
getLines().clear();
63+
}
64+
65+
/**
66+
* Remove the ingredient from this tooltip.
67+
*
68+
* @see #setIngredient(ITypedIngredient)
69+
*
70+
* @since 21.1.0
71+
*/
72+
void clearIngredient();
73+
74+
/**
75+
* Get the lines stored by this tooltip builder.
76+
* These lines are directly modifiable.
77+
*
78+
* @since 21.1.0
79+
*/
80+
List<Either<FormattedText, TooltipComponent>> getLines();
6081

6182
/**
6283
* @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 = "21.1.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 = "21.1.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 21.1.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
@@ -29,7 +29,7 @@ public interface IRecipeCategoryExtension<T> {
2929
/**
3030
* Draw additional info about the recipe.
3131
* Use the mouse position for things like button highlights.
32-
* Tooltips are handled by {@link #getTooltipStrings(Object, double, double)}
32+
* Tooltips are handled by {@link #getTooltip(ITooltipBuilder, Object, double, double)}
3333
*
3434
* @param mouseX the X position of the mouse, relative to the recipe.
3535
* @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)