diff options
-rw-r--r-- | CHANGELOG.md | 6 | ||||
-rw-r--r-- | gradle.properties | 2 | ||||
-rw-r--r-- | src/main/java/com/anthonyhilyard/iceberg/util/ItemColor.java | 4 | ||||
-rw-r--r-- | src/main/java/com/anthonyhilyard/iceberg/util/Selectors.java | 4 | ||||
-rw-r--r-- | src/main/java/com/anthonyhilyard/iceberg/util/Tooltips.java | 40 |
5 files changed, 41 insertions, 15 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index f72768f..6ecf7bb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +### 1.0.38 +- Added support for tooltip components that use tooltip component generation event. + +### 1.0.37 +- Reverted first change from 1.0.35, as it was causing conflicts with other mod's tooltips. + ### 1.0.36 - Fixed a crash issue with modded tooltip components that are not properly added to tooltip component factory. diff --git a/gradle.properties b/gradle.properties index bf52eb2..7d3e3c0 100644 --- a/gradle.properties +++ b/gradle.properties @@ -6,7 +6,7 @@ org.gradle.daemon=false name=${rootProject.name} group=com.anthonyhilyard.${name.toLowerCase()} author=anthonyhilyard -version=1.0.36 +version=1.0.38 mcVersion=1.18.1 fabricVersion=0.45.0+1.18 diff --git a/src/main/java/com/anthonyhilyard/iceberg/util/ItemColor.java b/src/main/java/com/anthonyhilyard/iceberg/util/ItemColor.java index 74e4772..8d31054 100644 --- a/src/main/java/com/anthonyhilyard/iceberg/util/ItemColor.java +++ b/src/main/java/com/anthonyhilyard/iceberg/util/ItemColor.java @@ -10,6 +10,7 @@ import net.minecraft.network.chat.Style; import java.util.List; import net.minecraft.ChatFormatting; +import net.minecraft.client.Minecraft; public class ItemColor { @@ -105,7 +106,8 @@ public class ItemColor // This is slow, so it better get cached externally! if (result == null || result.equals(item.getDisplayName().getStyle().getColor())) { - List<Component> lines = item.getTooltipLines(null, TooltipFlag.Default.ADVANCED); + Minecraft mc = Minecraft.getInstance(); + List<Component> lines = item.getTooltipLines(mc.player, TooltipFlag.Default.ADVANCED); if (!lines.isEmpty()) { result = lines.get(0).getStyle().getColor(); diff --git a/src/main/java/com/anthonyhilyard/iceberg/util/Selectors.java b/src/main/java/com/anthonyhilyard/iceberg/util/Selectors.java index c097323..04e46da 100644 --- a/src/main/java/com/anthonyhilyard/iceberg/util/Selectors.java +++ b/src/main/java/com/anthonyhilyard/iceberg/util/Selectors.java @@ -1,5 +1,6 @@ package com.anthonyhilyard.iceberg.util; +import net.minecraft.client.Minecraft; import net.minecraft.core.Registry; import net.minecraft.world.item.ItemStack; @@ -194,7 +195,8 @@ public class Selectors // Tooltip text else if (selector.startsWith("^")) { - List<Component> lines = item.getTooltipLines(null, TooltipFlag.Default.ADVANCED); + Minecraft mc = Minecraft.getInstance(); + List<Component> lines = item.getTooltipLines(mc.player, TooltipFlag.Default.ADVANCED); String tooltipText = ""; // Skip title line. diff --git a/src/main/java/com/anthonyhilyard/iceberg/util/Tooltips.java b/src/main/java/com/anthonyhilyard/iceberg/util/Tooltips.java index 277f5c4..57c998e 100644 --- a/src/main/java/com/anthonyhilyard/iceberg/util/Tooltips.java +++ b/src/main/java/com/anthonyhilyard/iceberg/util/Tooltips.java @@ -13,6 +13,7 @@ import com.anthonyhilyard.iceberg.events.RenderTooltipEvents.GatherResult; import com.anthonyhilyard.iceberg.events.RenderTooltipEvents.PreExtResult; import com.mojang.blaze3d.vertex.PoseStack; +import net.fabricmc.fabric.api.client.rendering.v1.TooltipComponentCallback; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.Font; import net.minecraft.client.gui.screens.inventory.tooltip.ClientTooltipComponent; @@ -190,6 +191,31 @@ public class Tooltips RenderTooltipEvents.POSTEXT.invoker().onPost(stack, info.getComponents(), poseStack, rectX, rectY, info.getFont(), rect.getWidth(), rect.getHeight(), comparison, index); } + private static ClientTooltipComponent getClientComponent(TooltipComponent componentData) + { + ClientTooltipComponent result = null; + + // First try using the create method, for vanilla and mixed-in tooltip components. + try { result = ClientTooltipComponent.create(componentData); } + catch (IllegalArgumentException e) { } + + // If that fails, try using the Fabric API event. + if (result == null) + { + result = TooltipComponentCallback.EVENT.invoker().getComponent(componentData); + } + + // Finally, if all else fails, try casting (some mods implement it this way). + try { result = (ClientTooltipComponent)componentData; } + catch (ClassCastException e) { } + + if (result == null) + { + throw new IllegalArgumentException("Unknown TooltipComponent"); + } + return result; + } + public static List<ClientTooltipComponent> gatherTooltipComponents(ItemStack stack, List<? extends FormattedText> textElements, Optional<TooltipComponent> itemComponent, int mouseX, int screenWidth, int screenHeight, Font forcedFont, Font fallbackFont, int maxWidth) { @@ -249,22 +275,12 @@ public class Tooltips { return eventResult.tooltipElements().stream().flatMap(either -> either.map(text -> font.split(text, tooltipTextWidthFinal).stream().map(ClientTooltipComponent::create), - component -> Stream.of(ClientTooltipComponent.create(component)))).toList(); + component -> Stream.of(getClientComponent(component)))).toList(); } return eventResult.tooltipElements().stream().map(either -> either.map(text -> ClientTooltipComponent.create(text instanceof Component ? ((Component) text).getVisualOrderText() : Language.getInstance().getVisualOrder(text)), - x -> { - // First try using the create method, for vanilla and properly-implemented tooltip components. - try { - return ClientTooltipComponent.create(x); - } - // If that fails, attempt just casting it. - catch (IllegalArgumentException e) - { - return (ClientTooltipComponent)x; - } - })).toList(); + Tooltips::getClientComponent)).toList(); } public static Rect2i calculateRect(final ItemStack stack, PoseStack poseStack, List<ClientTooltipComponent> components, |