fix crash with JEI

fixes #93
This commit is contained in:
rlnt 2024-09-20 11:40:07 +02:00
parent e0649dedd7
commit 702d1f0829
No known key found for this signature in database
9 changed files with 65 additions and 111 deletions

View file

@ -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

View file

@ -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 <R> void handleIndicator(PoseStack stack, int mX, int mY, int posX, int posY, IRecipeCategory<R> 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<T> implements IRecipeCategoryDecorator<T> {
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<T> 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 <R> ClientRecipeTracker.ClientRecipeLink resolveLink(IRecipeCategory<R> recipeCategory, R recipe) {
var recipeId = recipeCategory.getRegistryName(recipe);
if (recipeId == null) return null;
return CRTLookup.getLink(recipeId);
}
}
}

View file

@ -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);

View file

@ -90,17 +90,25 @@ public class DuplicationConfig extends Config {
JsonCompare.CompareSettings defaultRules = safeGet(() -> createCompareSet(json.getAsJsonObject(
DEFAULT_DUPLICATE_RULES)),
Defaults.getDefaultDuplicateRules(platform));
LinkedHashMap<ResourceLocation, JsonCompare.CompareSettings> overrideRules = safeGet(() -> json
LinkedHashMap<ResourceLocation, JsonCompare.CompareSettings> 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<ResourceLocation, JsonCompare.CompareSettings> 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) {

View file

@ -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<String, BooleanSupplier> 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<String> myTargets, Set<String> otherTargets) {}
@SuppressWarnings({ "ReturnOfNull", "DataFlowIssue" })
@Override
public List<String> 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) {}
}

View file

@ -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<R> {
@Shadow(remap = false) @Final
private IRecipeCategory<R> 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);
}
}

View file

@ -1,6 +0,0 @@
@ParametersAreNonnullByDefault @MethodsReturnNonnullByDefault
package com.almostreliable.unified.mixin.compat;
import net.minecraft.MethodsReturnNonnullByDefault;
import javax.annotation.ParametersAreNonnullByDefault;

View file

@ -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": {

View file

@ -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