From dc957b7ab8191592be04caea801dea3e152dfeb4 Mon Sep 17 00:00:00 2001 From: Relentless Date: Sat, 29 Jul 2023 01:43:23 +0200 Subject: [PATCH] implement JEI decorators for indicator --- .../unified/AlmostUnifiedPlatform.java | 2 +- .../unified/compat/AlmostJEI.java | 45 ++++++++++++----- .../unified/compat/AlmostREI.java | 32 +++++++----- .../unified/compat/RecipeIndicator.java | 49 +++++++++++++------ .../mixin/compat/JeiRecipeLayoutMixin.java | 32 ------------ .../unified/mixin/compat/package-info.java | 6 --- .../almostreliable/unified/utils/Utils.java | 19 ------- .../almostunified-common.mixins.json | 1 - Fabric/build.gradle.kts | 4 +- Forge/build.gradle.kts | 6 +-- gradle.properties | 2 +- 11 files changed, 92 insertions(+), 106 deletions(-) delete mode 100644 Common/src/main/java/com/almostreliable/unified/mixin/compat/JeiRecipeLayoutMixin.java delete mode 100644 Common/src/main/java/com/almostreliable/unified/mixin/compat/package-info.java diff --git a/Common/src/main/java/com/almostreliable/unified/AlmostUnifiedPlatform.java b/Common/src/main/java/com/almostreliable/unified/AlmostUnifiedPlatform.java index 48e461b..681d4ff 100644 --- a/Common/src/main/java/com/almostreliable/unified/AlmostUnifiedPlatform.java +++ b/Common/src/main/java/com/almostreliable/unified/AlmostUnifiedPlatform.java @@ -48,6 +48,6 @@ public interface AlmostUnifiedPlatform { enum Platform { FORGE, - FABRIC; + FABRIC } } diff --git a/Common/src/main/java/com/almostreliable/unified/compat/AlmostJEI.java b/Common/src/main/java/com/almostreliable/unified/compat/AlmostJEI.java index 9fd4d66..1d004f0 100644 --- a/Common/src/main/java/com/almostreliable/unified/compat/AlmostJEI.java +++ b/Common/src/main/java/com/almostreliable/unified/compat/AlmostJEI.java @@ -5,19 +5,22 @@ import com.almostreliable.unified.AlmostUnifiedFallbackRuntime; import com.almostreliable.unified.api.ModConstants; import com.almostreliable.unified.config.UnifyConfig; import com.almostreliable.unified.recipe.CRTLookup; +import com.almostreliable.unified.recipe.ClientRecipeTracker.ClientRecipeLink; import com.almostreliable.unified.utils.Utils; -import com.mojang.blaze3d.vertex.PoseStack; import me.shedaniel.rei.plugincompatibilities.api.REIPluginCompatIgnore; import mezz.jei.api.IModPlugin; import mezz.jei.api.JeiPlugin; import mezz.jei.api.constants.VanillaTypes; +import mezz.jei.api.gui.ingredient.IRecipeSlotsView; import mezz.jei.api.recipe.category.IRecipeCategory; +import mezz.jei.api.recipe.category.extensions.IRecipeCategoryDecorator; +import mezz.jei.api.registration.IAdvancedRegistration; import mezz.jei.api.runtime.IJeiRuntime; import net.minecraft.client.gui.GuiGraphics; -import net.minecraft.client.renderer.Rect2i; import net.minecraft.resources.ResourceLocation; import net.minecraft.world.item.ItemStack; +import javax.annotation.Nullable; import java.util.Collection; @REIPluginCompatIgnore @@ -45,19 +48,37 @@ public class AlmostJEI implements IModPlugin { } } - public static void handleIndicator(GuiGraphics guiGraphics, PoseStack stack, int mX, int mY, int posX, int posY, IRecipeCategory recipeCategory, R recipe) { - var recipeId = recipeCategory.getRegistryName(recipe); - if (recipeId == null) return; + @Override + public void registerAdvanced(IAdvancedRegistration registration) { + var recipeTypes = registration.getJeiHelpers().getAllRecipeTypes(); + recipeTypes.forEach(rt -> registration.addRecipeCategoryDecorator(rt, new Decorator<>())); + } + private static class Decorator implements IRecipeCategoryDecorator { - var link = CRTLookup.getLink(recipeId); - if (link == null) return; + private static final int RECIPE_BORDER_PADDING = 4; - var area = new Rect2i(posX, posY, RecipeIndicator.RENDER_SIZE, RecipeIndicator.RENDER_SIZE); - RecipeIndicator.renderIndicator(guiGraphics, stack, area); - if (mX >= area.getX() && mX <= area.getX() + area.getWidth() && - mY >= area.getY() && mY <= area.getY() + area.getHeight()) { - Utils.renderTooltip(guiGraphics, mX, mY, RecipeIndicator.constructTooltip(link)); + @Override + public void draw(T recipe, IRecipeCategory recipeCategory, IRecipeSlotsView recipeSlotsView, GuiGraphics guiGraphics, double mouseX, double mouseY) { + var recipeLink = resolveLink(recipeCategory, recipe); + if (recipeLink == null) return; + + var pX = recipeCategory.getWidth() + (2 * RECIPE_BORDER_PADDING) - RecipeIndicator.RENDER_SIZE; + var pY = recipeCategory.getHeight() + (2 * RECIPE_BORDER_PADDING) - RecipeIndicator.RENDER_SIZE; + RecipeIndicator.renderIndicator(guiGraphics, pX, pY, RecipeIndicator.RENDER_SIZE); + + if (mouseX >= pX && mouseX <= pX + RecipeIndicator.RENDER_SIZE && + mouseY >= pY && mouseY <= pY + RecipeIndicator.RENDER_SIZE) { + RecipeIndicator.renderTooltip(guiGraphics, recipeLink, mouseX, mouseY); + } + } + + @Nullable + private static ClientRecipeLink resolveLink(IRecipeCategory recipeCategory, R recipe) { + var recipeId = recipeCategory.getRegistryName(recipe); + if (recipeId == null) return null; + + return CRTLookup.getLink(recipeId); } } } diff --git a/Common/src/main/java/com/almostreliable/unified/compat/AlmostREI.java b/Common/src/main/java/com/almostreliable/unified/compat/AlmostREI.java index 7b08f90..1cd158e 100644 --- a/Common/src/main/java/com/almostreliable/unified/compat/AlmostREI.java +++ b/Common/src/main/java/com/almostreliable/unified/compat/AlmostREI.java @@ -23,7 +23,6 @@ import me.shedaniel.rei.api.common.display.Display; import me.shedaniel.rei.api.common.plugins.PluginManager; import me.shedaniel.rei.api.common.registry.ReloadStage; import me.shedaniel.rei.api.common.util.EntryIngredients; -import net.minecraft.client.renderer.Rect2i; import javax.annotation.Nullable; import java.util.List; @@ -99,21 +98,30 @@ public class AlmostREI implements REIClientPlugin { @Override public List setupDisplay(Display display, Rectangle bounds) { - var widgets = lastView.setupDisplay(display, bounds); - var area = calculateArea(bounds); - widgets.add(Widgets.createDrawableWidget((guiGraphics, mX, mY, delta) -> - RecipeIndicator.renderIndicator(guiGraphics, guiGraphics.pose(), area))); - var tooltipArea = new Rectangle(area.getX(), area.getY(), area.getWidth(), area.getHeight()); - widgets.add(Widgets.createTooltip(tooltipArea, RecipeIndicator.constructTooltip(link))); - return widgets; - } + int pX; + int pY; + int size; - private Rect2i calculateArea(Rectangle bounds) { if (plusButtonArea != null) { var area = plusButtonArea.get(bounds); - return new Rect2i(area.x, area.y - area.height - 2, area.width, area.height); + pX = area.x; + pY = area.y - area.height - 2; + size = area.width; + } else { + pX = bounds.x - RecipeIndicator.RENDER_SIZE / 2; + pY = bounds.y - RecipeIndicator.RENDER_SIZE / 2; + size = RecipeIndicator.RENDER_SIZE; } - return new Rect2i(bounds.x, bounds.y, bounds.width, bounds.height); + + var widgets = lastView.setupDisplay(display, bounds); + widgets.add(Widgets.createDrawableWidget( + (guiGraphics, mX, mY, delta) -> RecipeIndicator.renderIndicator(guiGraphics, pX, pY, size) + )); + + var tooltipArea = new Rectangle(pX, pY, size, size); + widgets.add(Widgets.createTooltip(tooltipArea, RecipeIndicator.constructTooltip(link))); + + return widgets; } } } diff --git a/Common/src/main/java/com/almostreliable/unified/compat/RecipeIndicator.java b/Common/src/main/java/com/almostreliable/unified/compat/RecipeIndicator.java index 64def60..6862046 100644 --- a/Common/src/main/java/com/almostreliable/unified/compat/RecipeIndicator.java +++ b/Common/src/main/java/com/almostreliable/unified/compat/RecipeIndicator.java @@ -2,23 +2,34 @@ package com.almostreliable.unified.compat; import com.almostreliable.unified.recipe.ClientRecipeTracker.ClientRecipeLink; import com.almostreliable.unified.utils.Utils; -import com.mojang.blaze3d.vertex.PoseStack; import net.minecraft.ChatFormatting; +import net.minecraft.client.Minecraft; import net.minecraft.client.gui.GuiGraphics; -import net.minecraft.client.renderer.Rect2i; import net.minecraft.network.chat.Component; import net.minecraft.resources.ResourceLocation; import java.util.List; -public final class RecipeIndicator { +final class RecipeIndicator { - public static final int RENDER_SIZE = 10; + static final int RENDER_SIZE = 10; private static final int TEXTURE_SIZE = 16; private static final ResourceLocation TEXTURE = Utils.getRL("textures/ingot.png"); private RecipeIndicator() {} + static void renderIndicator(GuiGraphics guiGraphics, int pX, int pY, int size) { + var poseStack = guiGraphics.pose(); + poseStack.pushPose(); + + poseStack.translate(pX, pY, 0); + var scale = size / (float) TEXTURE_SIZE; + poseStack.scale(scale, scale, scale); + guiGraphics.blit(TEXTURE, 0, 0, 0, 0, TEXTURE_SIZE, TEXTURE_SIZE, TEXTURE_SIZE, TEXTURE_SIZE); + + poseStack.popPose(); + } + static List constructTooltip(ClientRecipeLink link) { var unified = Component.translatable(Utils.prefix("unified")).append(": ") .withStyle(c -> c.withColor(ChatFormatting.AQUA)); @@ -32,22 +43,30 @@ public final class RecipeIndicator { return List.of( Component.translatable(Utils.prefix("description")).withStyle(c -> c.withColor(ChatFormatting.GOLD)), - Component.literal(""), + Component.literal(" "), unified, duplicate, - Component.literal(""), + Component.literal(" "), Component.translatable(Utils.prefix("warning")).withStyle(c -> c.withColor(ChatFormatting.RED)) ); } - static void renderIndicator(GuiGraphics guiGraphics, PoseStack poseStack, Rect2i area) { - poseStack.pushPose(); - poseStack.translate(area.getX(), area.getY(), 0); - var scale = area.getWidth() / (float) TEXTURE_SIZE; - poseStack.scale(scale, scale, scale); - guiGraphics.blit(TEXTURE, 0, 0, 0, 0, TEXTURE_SIZE, TEXTURE_SIZE, TEXTURE_SIZE, TEXTURE_SIZE); - poseStack.popPose(); - } + static void renderTooltip(GuiGraphics guiGraphics, ClientRecipeLink link, double mouseX, double mouseY) { + var mc = Minecraft.getInstance(); + var font = mc.font; + var screen = mc.screen; + if (screen == null) return; - public record RenderEntry(int pX, int pY) {} + var tooltip = constructTooltip(link).stream() + .map(c -> font.split(c, screen.width - (int) mouseX - 200)) + .flatMap(List::stream) + .toList(); + + guiGraphics.renderTooltip( + font, + tooltip, + (int) mouseX, + (int) mouseY + ); + } } diff --git a/Common/src/main/java/com/almostreliable/unified/mixin/compat/JeiRecipeLayoutMixin.java b/Common/src/main/java/com/almostreliable/unified/mixin/compat/JeiRecipeLayoutMixin.java deleted file mode 100644 index d49f2af..0000000 --- a/Common/src/main/java/com/almostreliable/unified/mixin/compat/JeiRecipeLayoutMixin.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.almostreliable.unified.mixin.compat; - -import com.almostreliable.unified.compat.AlmostJEI; -import com.almostreliable.unified.compat.RecipeIndicator; -import com.mojang.blaze3d.vertex.PoseStack; -import mezz.jei.api.gui.drawable.IDrawable; -import mezz.jei.api.recipe.category.IRecipeCategory; -import mezz.jei.library.gui.recipes.RecipeLayout; -import net.minecraft.client.gui.GuiGraphics; -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.callback.CallbackInfo; -import org.spongepowered.asm.mixin.injection.callback.LocalCapture; - -@Mixin(RecipeLayout.class) -public abstract class JeiRecipeLayoutMixin { - - @Shadow(remap = false) @Final - private IRecipeCategory recipeCategory; - @Shadow(remap = false) @Final - private R recipe; - - @Inject(method = "drawRecipe", at = @At(value = "INVOKE", target = "Lcom/mojang/blaze3d/vertex/PoseStack;pushPose()V", ordinal = 1), locals = LocalCapture.CAPTURE_FAILHARD) - private void unified$catchLayoutInfo(GuiGraphics guiGraphics, int mouseX, int mouseY, CallbackInfo ci, IDrawable background, int mX, int mY, PoseStack poseStack, int x, int y) { - var posX = x - RecipeIndicator.RENDER_SIZE; - var posY = y - RecipeIndicator.RENDER_SIZE; - AlmostJEI.handleIndicator(guiGraphics, poseStack, mX, mY, posX, posY, recipeCategory, recipe); - } -} diff --git a/Common/src/main/java/com/almostreliable/unified/mixin/compat/package-info.java b/Common/src/main/java/com/almostreliable/unified/mixin/compat/package-info.java deleted file mode 100644 index ad8dc16..0000000 --- a/Common/src/main/java/com/almostreliable/unified/mixin/compat/package-info.java +++ /dev/null @@ -1,6 +0,0 @@ -@ParametersAreNonnullByDefault @MethodsReturnNonnullByDefault -package com.almostreliable.unified.mixin.compat; - -import net.minecraft.MethodsReturnNonnullByDefault; - -import javax.annotation.ParametersAreNonnullByDefault; diff --git a/Common/src/main/java/com/almostreliable/unified/utils/Utils.java b/Common/src/main/java/com/almostreliable/unified/utils/Utils.java index db025d1..81ce543 100644 --- a/Common/src/main/java/com/almostreliable/unified/utils/Utils.java +++ b/Common/src/main/java/com/almostreliable/unified/utils/Utils.java @@ -1,17 +1,10 @@ package com.almostreliable.unified.utils; import com.almostreliable.unified.BuildConfig; -import net.minecraft.client.Minecraft; -import net.minecraft.client.gui.GuiGraphics; -import net.minecraft.client.gui.components.ComponentRenderUtils; -import net.minecraft.network.chat.Component; import net.minecraft.resources.ResourceLocation; -import net.minecraft.util.FormattedCharSequence; import net.minecraft.world.item.Item; import javax.annotation.Nullable; -import java.util.ArrayList; -import java.util.List; public final class Utils { public static final ResourceLocation UNUSED_ID = new ResourceLocation(BuildConfig.MOD_ID, "unused_id"); @@ -44,16 +37,4 @@ public final class Utils { public static String prefix(String path) { return BuildConfig.MOD_ID + "." + path; } - - public static void renderTooltip(GuiGraphics guiGraphics, int mX, int mY, List tooltip) { - var mc = Minecraft.getInstance(); - var screen = mc.screen; - if (screen == null) return; - - List formattedTooltip = new ArrayList<>(tooltip.size()); - for (Component line : tooltip) { - formattedTooltip.addAll(ComponentRenderUtils.wrapComponents(line, 300, mc.font)); - } - guiGraphics.renderTooltip(mc.font, formattedTooltip, mX, mY); - } } diff --git a/Common/src/main/resources/almostunified-common.mixins.json b/Common/src/main/resources/almostunified-common.mixins.json index 1b57a7a..1811ff6 100644 --- a/Common/src/main/resources/almostunified-common.mixins.json +++ b/Common/src/main/resources/almostunified-common.mixins.json @@ -12,7 +12,6 @@ "unifier.TieredItemMixin" ], "client": [ - "compat.JeiRecipeLayoutMixin", "runtime.ClientPacketListenerMixin" ], "injectors": { diff --git a/Fabric/build.gradle.kts b/Fabric/build.gradle.kts index 19a0181..0a8cca9 100644 --- a/Fabric/build.gradle.kts +++ b/Fabric/build.gradle.kts @@ -34,10 +34,8 @@ dependencies { shadowCommon(project(":Common", "transformProductionFabric")) { isTransitive = false } // compile time mods + modCompileOnly("mezz.jei:jei-$minecraftVersion-fabric-api:$jeiVersion") // required for common jei plugin modCompileOnly("me.shedaniel:RoughlyEnoughItems-api-fabric:$reiVersion") // required for common rei plugin - compileOnly("me.shedaniel:REIPluginCompatibilities-forge-annotations:9.+") // required to disable rei compat layer on jei plugin - testCompileOnly("me.shedaniel:REIPluginCompatibilities-forge-annotations:9.+") // don't question this, it's required for compiling - modCompileOnly("mezz.jei:jei-$minecraftVersion-fabric-api:$jeiVersion") // required for common jei plugin and mixin // runtime dependencies modLocalRuntime( diff --git a/Forge/build.gradle.kts b/Forge/build.gradle.kts index 8524b56..69afb4f 100644 --- a/Forge/build.gradle.kts +++ b/Forge/build.gradle.kts @@ -52,12 +52,10 @@ dependencies { shadowCommon(project(":Common", "transformProductionForge")) { isTransitive = false } // compile time mods - modCompileOnly("me.shedaniel:RoughlyEnoughItems-forge:$reiVersion") // required for common rei plugin | api does not work here - compileOnly("me.shedaniel:REIPluginCompatibilities-forge-annotations:9.+") // required to disable rei compat layer on jei plugin - testCompileOnly("me.shedaniel:REIPluginCompatibilities-forge-annotations:9.+") // don't question this, it's required for compiling - modCompileOnly("mezz.jei:jei-$minecraftVersion-forge-api:$jeiVersion") { // required for common jei plugin and mixin + modCompileOnly("mezz.jei:jei-$minecraftVersion-forge-api:$jeiVersion") { // required for common jei plugin isTransitive = false // prevents breaking the forge runtime } + modCompileOnly("me.shedaniel:RoughlyEnoughItems-forge:$reiVersion") // required for common rei plugin // runtime mods when (forgeRecipeViewer) { diff --git a/gradle.properties b/gradle.properties index 1606012..e95ff33 100644 --- a/gradle.properties +++ b/gradle.properties @@ -21,7 +21,7 @@ junitVersion = 5.9.0 parchmentVersion = 2023.07.23 # Mod Dependencies -jeiVersion = 15.0.0.12 +jeiVersion = 15.1.0.18 reiVersion = 12.0.625 # Fabric Dependencies