From 645f0103fcd29af3e09494a4bda6439610f93097 Mon Sep 17 00:00:00 2001 From: Rime <81419447+Emirlol@users.noreply.github.com> Date: Tue, 22 Oct 2024 18:38:35 +0300 Subject: [PATCH] Fix broken tooltips with JEI --- .../mixins/jei/RenderHelperMixin.java | 43 +++++++++++++++++++ src/main/resources/skyblocker.mixins.json | 3 +- 2 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 src/main/java/de/hysky/skyblocker/mixins/jei/RenderHelperMixin.java diff --git a/src/main/java/de/hysky/skyblocker/mixins/jei/RenderHelperMixin.java b/src/main/java/de/hysky/skyblocker/mixins/jei/RenderHelperMixin.java new file mode 100644 index 0000000000..2e2abc695a --- /dev/null +++ b/src/main/java/de/hysky/skyblocker/mixins/jei/RenderHelperMixin.java @@ -0,0 +1,43 @@ +package de.hysky.skyblocker.mixins.jei; + +import com.llamalad7.mixinextras.injector.wrapmethod.WrapMethod; +import com.llamalad7.mixinextras.injector.wrapoperation.Operation; +import com.mojang.datafixers.util.Either; +import de.hysky.skyblocker.mixins.accessors.DrawContextInvoker; +import de.hysky.skyblocker.utils.render.gui.AlignedTooltipComponent; +import mezz.jei.fabric.platform.RenderHelper; +import net.minecraft.client.font.TextRenderer; +import net.minecraft.client.gui.DrawContext; +import net.minecraft.client.gui.tooltip.HoveredTooltipPositioner; +import net.minecraft.client.gui.tooltip.TooltipComponent; +import net.minecraft.item.ItemStack; +import net.minecraft.item.tooltip.TooltipData; +import net.minecraft.text.MutableText; +import net.minecraft.text.StringVisitable; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; + +import java.util.List; +import java.util.stream.Stream; + +@Mixin(value = RenderHelper.class, remap = false) +public abstract class RenderHelperMixin { + @WrapMethod(method = "renderTooltip", require = 0) + private void renderTooltip(DrawContext drawContext, List> elements, int x, int y, TextRenderer textRenderer, ItemStack stack, Operation original) { + List components = elements.stream().flatMap(e -> e.map( + text -> { + if (text instanceof MutableText mutableText) { + MutableText firstOfChain = mutableText.getFirstOfChain(); + if (firstOfChain != null) return Stream.of(new AlignedTooltipComponent(firstOfChain)); + else if (mutableText.getAlignedText() != null) return Stream.of(new AlignedTooltipComponent(mutableText)); + } + return textRenderer.wrapLines(text, 400).stream().map(TooltipComponent::of); + }, + tooltipComponent -> Stream.of(this.createClientTooltipComponent(tooltipComponent))) + ).toList(); + ((DrawContextInvoker) drawContext).invokeDrawTooltip(textRenderer, components, x, y, HoveredTooltipPositioner.INSTANCE); + } + + @Shadow + protected abstract TooltipComponent createClientTooltipComponent(TooltipData tooltipComponent); +} diff --git a/src/main/resources/skyblocker.mixins.json b/src/main/resources/skyblocker.mixins.json index a5fdea6530..f8b065e2f7 100644 --- a/src/main/resources/skyblocker.mixins.json +++ b/src/main/resources/skyblocker.mixins.json @@ -58,7 +58,8 @@ "discordipc.ConnectionMixin", "jgit.SystemReaderMixin", "rei.ScreenOverlayImplFabricMixin", - "emi.FakeScreenMixin" + "emi.FakeScreenMixin", + "jei.RenderHelperMixin" ], "injectors": { "defaultRequire": 1