From 702d1f0829f8ecc13eddb7495579b49c48e6fe74 Mon Sep 17 00:00:00 2001 From: rlnt Date: Fri, 20 Sep 2024 11:40:07 +0200 Subject: [PATCH] fix crash with JEI fixes #93 --- Common/build.gradle.kts | 1 - .../unified/compat/AlmostJEI.java | 46 ++++++++++++---- .../unified/compat/RecipeIndicator.java | 15 +++++ .../unified/config/DuplicationConfig.java | 18 ++++-- .../unified/mixin/AlmostMixinPlugin.java | 55 ------------------- .../mixin/compat/JeiRecipeLayoutMixin.java | 31 ----------- .../unified/mixin/compat/package-info.java | 6 -- .../almostunified-common.mixins.json | 2 - gradle.properties | 2 +- 9 files changed, 65 insertions(+), 111 deletions(-) delete mode 100644 Common/src/main/java/com/almostreliable/unified/mixin/AlmostMixinPlugin.java 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/build.gradle.kts b/Common/build.gradle.kts index dea7f65..0998863 100644 --- a/Common/build.gradle.kts +++ b/Common/build.gradle.kts @@ -34,7 +34,6 @@ dependencies { // compile time mods modCompileOnly("mezz.jei:jei-$minecraftVersion-common-api:$jeiVersion") // required for jei plugin - modCompileOnly("mezz.jei:jei-$minecraftVersion-lib:$jeiVersion") // required for jei mixin modCompileOnly("me.shedaniel:RoughlyEnoughItems-api:$reiVersion") // required for rei plugin modCompileOnly("dev.emi:emi-xplat-intermediary:$emiVersion+$minecraftVersion:api") // required for emi plugin 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 f58d5c4..d04a4fc 100644 --- a/Common/src/main/java/com/almostreliable/unified/compat/AlmostJEI.java +++ b/Common/src/main/java/com/almostreliable/unified/compat/AlmostJEI.java @@ -5,17 +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; 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.renderer.Rect2i; import net.minecraft.resources.ResourceLocation; import net.minecraft.world.item.ItemStack; +import org.jetbrains.annotations.Nullable; import java.util.Collection; @@ -44,19 +49,40 @@ public class AlmostJEI implements IModPlugin { } } - public static void handleIndicator(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(stack, area); - if (mX >= area.getX() && mX <= area.getX() + area.getWidth() && - mY >= area.getY() && mY <= area.getY() + area.getHeight()) { - Utils.renderTooltip(stack, mX, mY, RecipeIndicator.constructTooltip(link)); + @Override + public void draw(T recipe, IRecipeCategory recipeCategory, IRecipeSlotsView recipeSlotsView, PoseStack poseStack, 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( + poseStack, + new Rect2i(pX, pY, RecipeIndicator.RENDER_SIZE, RecipeIndicator.RENDER_SIZE) + ); + + if (mouseX >= pX && mouseX <= pX + RecipeIndicator.RENDER_SIZE && + mouseY >= pY && mouseY <= pY + RecipeIndicator.RENDER_SIZE) { + RecipeIndicator.renderTooltip(poseStack, recipeLink, mouseX, mouseY); + } + } + + @Nullable + private static ClientRecipeTracker.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/RecipeIndicator.java b/Common/src/main/java/com/almostreliable/unified/compat/RecipeIndicator.java index e66c19c..f20c1d3 100644 --- a/Common/src/main/java/com/almostreliable/unified/compat/RecipeIndicator.java +++ b/Common/src/main/java/com/almostreliable/unified/compat/RecipeIndicator.java @@ -5,6 +5,7 @@ import com.almostreliable.unified.utils.Utils; import com.mojang.blaze3d.systems.RenderSystem; import com.mojang.blaze3d.vertex.PoseStack; import net.minecraft.ChatFormatting; +import net.minecraft.client.Minecraft; import net.minecraft.client.gui.GuiComponent; import net.minecraft.client.renderer.GameRenderer; import net.minecraft.client.renderer.Rect2i; @@ -42,6 +43,20 @@ public final class RecipeIndicator { ); } + static void renderTooltip(PoseStack poseStack, ClientRecipeLink link, double mouseX, double mouseY) { + var mc = Minecraft.getInstance(); + var font = mc.font; + var screen = mc.screen; + if (screen == null) return; + + var tooltip = constructTooltip(link).stream() + .map(c -> font.split(c, screen.width - (int) mouseX - 200)) + .flatMap(List::stream) + .toList(); + + screen.renderTooltip(poseStack, tooltip, (int) mouseX, (int) mouseY); + } + static void renderIndicator(PoseStack poseStack, Rect2i area) { poseStack.pushPose(); poseStack.translate(area.getX(), area.getY(), 0); diff --git a/Common/src/main/java/com/almostreliable/unified/config/DuplicationConfig.java b/Common/src/main/java/com/almostreliable/unified/config/DuplicationConfig.java index a1c39b5..aefddaa 100644 --- a/Common/src/main/java/com/almostreliable/unified/config/DuplicationConfig.java +++ b/Common/src/main/java/com/almostreliable/unified/config/DuplicationConfig.java @@ -90,17 +90,25 @@ public class DuplicationConfig extends Config { JsonCompare.CompareSettings defaultRules = safeGet(() -> createCompareSet(json.getAsJsonObject( DEFAULT_DUPLICATE_RULES)), Defaults.getDefaultDuplicateRules(platform)); - LinkedHashMap overrideRules = safeGet(() -> json + LinkedHashMap overrideRules = safeGet( + () -> getOverrideRules(json), Defaults.getDefaultDuplicateOverrides(platform) + ); + boolean strictMode = safeGet(() -> json.get(STRICT_MODE).getAsBoolean(), false); + + return new DuplicationConfig(defaultRules, overrideRules, ignoreRecipeTypes, ignoreRecipes, strictMode); + } + + // Extracted as method because `safeGet` couldn't cast the type... Seems to be an old SDK bug :-) + // https://bugs.openjdk.org/browse/JDK-8324860 + private LinkedHashMap getOverrideRules(JsonObject json) { + return json .getAsJsonObject(OVERRIDE_DUPLICATE_RULES) .entrySet() .stream() .collect(Collectors.toMap(entry -> new ResourceLocation(entry.getKey()), entry -> createCompareSet(entry.getValue().getAsJsonObject()), (a, b) -> b, - LinkedHashMap::new)), Defaults.getDefaultDuplicateOverrides(platform)); - boolean strictMode = safeGet(() -> json.get(STRICT_MODE).getAsBoolean(), false); - - return new DuplicationConfig(defaultRules, overrideRules, ignoreRecipeTypes, ignoreRecipes, strictMode); + LinkedHashMap::new)); } private JsonCompare.CompareSettings createCompareSet(JsonObject rules) { diff --git a/Common/src/main/java/com/almostreliable/unified/mixin/AlmostMixinPlugin.java b/Common/src/main/java/com/almostreliable/unified/mixin/AlmostMixinPlugin.java deleted file mode 100644 index 03a5cad..0000000 --- a/Common/src/main/java/com/almostreliable/unified/mixin/AlmostMixinPlugin.java +++ /dev/null @@ -1,55 +0,0 @@ -package com.almostreliable.unified.mixin; - -import com.almostreliable.unified.AlmostUnifiedPlatform; -import com.almostreliable.unified.api.ModConstants; -import com.google.common.collect.ImmutableMap; -import org.objectweb.asm.tree.ClassNode; -import org.spongepowered.asm.mixin.extensibility.IMixinConfigPlugin; -import org.spongepowered.asm.mixin.extensibility.IMixinInfo; - -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.function.BooleanSupplier; - -@SuppressWarnings("unused") -public class AlmostMixinPlugin implements IMixinConfigPlugin { - - private static final BooleanSupplier TRUE = () -> true; - private static final Map CONDITIONS = ImmutableMap.of( - "com.almostreliable.unified.mixin.compat.JeiRecipeLayoutMixin", modLoaded(ModConstants.JEI) - ); - - private static BooleanSupplier modLoaded(String id) { - return () -> AlmostUnifiedPlatform.INSTANCE.isModLoaded(id); - } - - @Override - public void onLoad(String mixinPackage) {} - - @SuppressWarnings({ "ReturnOfNull", "DataFlowIssue" }) - @Override - public String getRefMapperConfig() { - return null; - } - - @Override - public boolean shouldApplyMixin(String targetClassName, String mixinClassName) { - return CONDITIONS.getOrDefault(mixinClassName, TRUE).getAsBoolean(); - } - - @Override - public void acceptTargets(Set myTargets, Set otherTargets) {} - - @SuppressWarnings({ "ReturnOfNull", "DataFlowIssue" }) - @Override - public List getMixins() { - return null; - } - - @Override - public void preApply(String targetClassName, ClassNode targetClass, String mixinClassName, IMixinInfo mixinInfo) {} - - @Override - public void postApply(String targetClassName, ClassNode targetClass, String mixinClassName, IMixinInfo mixinInfo) {} -} 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 d3fc26d..0000000 --- a/Common/src/main/java/com/almostreliable/unified/mixin/compat/JeiRecipeLayoutMixin.java +++ /dev/null @@ -1,31 +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 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(PoseStack stack, int mouseX, int mouseY, CallbackInfo ci, IDrawable background, int mX, int mY, int x, int y) { - var posX = x - RecipeIndicator.RENDER_SIZE; - var posY = y - RecipeIndicator.RENDER_SIZE; - AlmostJEI.handleIndicator(stack, 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/resources/almostunified-common.mixins.json b/Common/src/main/resources/almostunified-common.mixins.json index 363932b..7aa1a7c 100644 --- a/Common/src/main/resources/almostunified-common.mixins.json +++ b/Common/src/main/resources/almostunified-common.mixins.json @@ -3,7 +3,6 @@ "minVersion": "0.8.5", "package": "com.almostreliable.unified.mixin", "compatibilityLevel": "JAVA_17", - "plugin": "com.almostreliable.unified.mixin.AlmostMixinPlugin", "mixins": [ "runtime.RecipeManagerMixin", "runtime.TagLoaderMixin", @@ -11,7 +10,6 @@ "unifier.TieredItemMixin" ], "client": [ - "compat.JeiRecipeLayoutMixin", "runtime.ClientPacketListenerMixin" ], "injectors": { diff --git a/gradle.properties b/gradle.properties index 0297c68..7883b0d 100644 --- a/gradle.properties +++ b/gradle.properties @@ -21,7 +21,7 @@ junitVersion = 5.9.0 parchmentVersion = 2022.11.27 # Mod Dependencies -jeiVersion = 11.6.0.1012 +jeiVersion = 11.8.0.1030 reiVersion = 9.1.580 emiVersion = 1.1.2