Skip to content

Commit

Permalink
Fix broken tooltips with JEI
Browse files Browse the repository at this point in the history
  • Loading branch information
Emirlol committed Oct 29, 2024
1 parent 24679a8 commit 645f010
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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<Either<StringVisitable, TooltipData>> elements, int x, int y, TextRenderer textRenderer, ItemStack stack, Operation<Void> original) {
List<TooltipComponent> 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);
}
3 changes: 2 additions & 1 deletion src/main/resources/skyblocker.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@
"discordipc.ConnectionMixin",
"jgit.SystemReaderMixin",
"rei.ScreenOverlayImplFabricMixin",
"emi.FakeScreenMixin"
"emi.FakeScreenMixin",
"jei.RenderHelperMixin"
],
"injectors": {
"defaultRequire": 1
Expand Down

0 comments on commit 645f010

Please sign in to comment.