diff options
author | Anthony Hilyard <anthony.hilyard@gmail.com> | 2022-01-07 09:35:06 -0800 |
---|---|---|
committer | Anthony Hilyard <anthony.hilyard@gmail.com> | 2022-01-07 09:35:06 -0800 |
commit | 39398536fb13a0d08308229f9b38e9d1b58d0f54 (patch) | |
tree | 75484253cbce4a2a0c98c4da7927333408761c2f /src/main/java/com/anthonyhilyard | |
parent | 2f42181876880c5a88de8b8fb29d9be84c3e0424 (diff) | |
download | Iceberg-39398536fb13a0d08308229f9b38e9d1b58d0f54.tar.gz Iceberg-39398536fb13a0d08308229f9b38e9d1b58d0f54.tar.bz2 Iceberg-39398536fb13a0d08308229f9b38e9d1b58d0f54.zip |
Added support for casted and component generation event created tooltip
components.
Diffstat (limited to 'src/main/java/com/anthonyhilyard')
3 files changed, 34 insertions, 14 deletions
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, |