From e5e86bedca56374e40fe14fea76a16295cc1673e Mon Sep 17 00:00:00 2001 From: Anthony Hilyard Date: Mon, 18 Oct 2021 22:42:15 -0700 Subject: Readded RenderTooltipEvent.PostText, since it was removed in a recent version of Forge. --- build.gradle | 1 + gradle.properties | 2 +- .../anthonyhilyard/iceberg/mixin/ScreenMixin.java | 71 ++++++++++++++++++++++ .../com/anthonyhilyard/iceberg/util/Easing.java | 2 +- .../iceberg/util/StringRecomposer.java | 32 ++++++++++ src/main/resources/META-INF/accesstransformer.cfg | 1 + src/main/resources/iceberg.mixins.json | 3 + 7 files changed, 110 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/anthonyhilyard/iceberg/mixin/ScreenMixin.java create mode 100644 src/main/java/com/anthonyhilyard/iceberg/util/StringRecomposer.java create mode 100644 src/main/resources/META-INF/accesstransformer.cfg diff --git a/build.gradle b/build.gradle index 14f3be1..beb2d59 100644 --- a/build.gradle +++ b/build.gradle @@ -24,6 +24,7 @@ mixin { minecraft { mappings channel: 'official', version: project.mcVersion + accessTransformer = file('src/main/resources/META-INF/accesstransformer.cfg') runs { client = { diff --git a/gradle.properties b/gradle.properties index ce21702..5dc2d45 100644 --- a/gradle.properties +++ b/gradle.properties @@ -6,6 +6,6 @@ org.gradle.daemon=false name=${rootProject.name} group=com.anthonyhilyard.${name.toLowerCase()} author=anthonyhilyard -version=1.0.13 +version=1.0.14 mcVersion=1.17.1 forgeVersion=37.0.90 diff --git a/src/main/java/com/anthonyhilyard/iceberg/mixin/ScreenMixin.java b/src/main/java/com/anthonyhilyard/iceberg/mixin/ScreenMixin.java new file mode 100644 index 0000000..5b79256 --- /dev/null +++ b/src/main/java/com/anthonyhilyard/iceberg/mixin/ScreenMixin.java @@ -0,0 +1,71 @@ +package com.anthonyhilyard.iceberg.mixin; + +import java.util.ArrayList; +import java.util.List; + +import com.anthonyhilyard.iceberg.util.StringRecomposer; +import com.google.common.collect.Lists; +import com.mojang.blaze3d.vertex.PoseStack; + +import org.spongepowered.asm.mixin.Final; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.At.Shift; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; +import org.spongepowered.asm.mixin.injection.callback.LocalCapture; + +import net.minecraft.client.gui.Font; +import net.minecraft.client.gui.components.events.AbstractContainerEventHandler; +import net.minecraft.client.gui.components.events.GuiEventListener; +import net.minecraft.client.gui.screens.Screen; +import net.minecraft.client.gui.screens.inventory.tooltip.ClientTextTooltip; +import net.minecraft.client.gui.screens.inventory.tooltip.ClientTooltipComponent; +import net.minecraft.network.chat.FormattedText; +import net.minecraft.world.item.ItemStack; +import net.minecraftforge.client.event.RenderTooltipEvent; +import net.minecraftforge.common.MinecraftForge; + +@Mixin(Screen.class) +public class ScreenMixin extends AbstractContainerEventHandler +{ + @Shadow(remap = false) + private Font tooltipFont = null; + + @Shadow(remap = false) + private ItemStack tooltipStack = ItemStack.EMPTY; + + @Final + @Shadow + private final List children = Lists.newArrayList(); + + @SuppressWarnings("removal") + @Inject(method = "renderTooltipInternal", + at = @At(value = "FIELD", target = "Lnet/minecraft/client/renderer/entity/ItemRenderer;blitOffset:F", ordinal = 2, shift = Shift.AFTER), + locals = LocalCapture.CAPTURE_FAILEXCEPTION) + private void renderTooltipInternal(PoseStack poseStack, List components, int x, int y, CallbackInfo info, RenderTooltipEvent.Pre pre, int tooltipWidth, int tooltipHeight, int postX, int postY) + { + if (!components.isEmpty()) + { + List standinLines = new ArrayList<>(); + for (ClientTooltipComponent component : components) + { + if (component instanceof ClientTextTooltip) + { + StringRecomposer recomposer = new StringRecomposer(); + ((ClientTextTooltip)component).text.accept(recomposer); + standinLines.add(recomposer.getFormattedText()); + } + } + + MinecraftForge.EVENT_BUS.post(new RenderTooltipEvent.PostText(tooltipStack, standinLines, poseStack, postX, postY, tooltipFont, tooltipWidth, tooltipHeight)); + } + } + + @Override + public List children() + { + return children; + } +} diff --git a/src/main/java/com/anthonyhilyard/iceberg/util/Easing.java b/src/main/java/com/anthonyhilyard/iceberg/util/Easing.java index bc4e39b..2274ce2 100644 --- a/src/main/java/com/anthonyhilyard/iceberg/util/Easing.java +++ b/src/main/java/com/anthonyhilyard/iceberg/util/Easing.java @@ -3,7 +3,7 @@ package com.anthonyhilyard.iceberg.util; import net.minecraft.network.chat.TextColor; /** - * Helper functions for smooth easing/interpolation. If you need linear, use net.minecraft.util.math.MathHelper.lerp instead. + * Helper functions for smooth easing/interpolation. */ public final class Easing { diff --git a/src/main/java/com/anthonyhilyard/iceberg/util/StringRecomposer.java b/src/main/java/com/anthonyhilyard/iceberg/util/StringRecomposer.java new file mode 100644 index 0000000..d88ae41 --- /dev/null +++ b/src/main/java/com/anthonyhilyard/iceberg/util/StringRecomposer.java @@ -0,0 +1,32 @@ +package com.anthonyhilyard.iceberg.util; + +import net.minecraft.network.chat.FormattedText; +import net.minecraft.network.chat.MutableComponent; +import net.minecraft.network.chat.Style; +import net.minecraft.network.chat.TextComponent; +import net.minecraft.util.FormattedCharSink; + +public class StringRecomposer implements FormattedCharSink +{ + private StringBuilder builder = new StringBuilder(); + private MutableComponent text = new TextComponent("").withStyle(Style.EMPTY); + + @Override + public boolean accept(int index, Style style, int charCode) + { + builder.append(Character.toChars(charCode)); + + if (!style.equals(text.getStyle())) + { + text.append(new TextComponent(builder.toString()).withStyle(style)); + builder.setLength(0); + } + return true; + } + + public FormattedText getFormattedText() + { + text.append(new TextComponent(builder.toString()).withStyle(text.getStyle())); + return text; + } +} diff --git a/src/main/resources/META-INF/accesstransformer.cfg b/src/main/resources/META-INF/accesstransformer.cfg new file mode 100644 index 0000000..b3edbd7 --- /dev/null +++ b/src/main/resources/META-INF/accesstransformer.cfg @@ -0,0 +1 @@ +public net.minecraft.client.gui.screens.inventory.tooltip.ClientTextTooltip f_169936_ # text \ No newline at end of file diff --git a/src/main/resources/iceberg.mixins.json b/src/main/resources/iceberg.mixins.json index 8bb4253..d81f759 100644 --- a/src/main/resources/iceberg.mixins.json +++ b/src/main/resources/iceberg.mixins.json @@ -10,6 +10,9 @@ "server": [ "ForgeEventFactoryMixin" ], + "client": [ + "ScreenMixin" + ], "injectors": { "defaultRequire": 1 }, -- cgit