From c3fd29e6ccdf00bdec743b0fa65b109169a67d7a Mon Sep 17 00:00:00 2001 From: nea Date: Wed, 13 Apr 2022 04:47:56 +0200 Subject: skytils interop --- .../notenoughupdates/miscgui/GuiItemRecipe.java | 2 +- .../notenoughupdates/util/SkytilsCompat.java | 77 ++++++++++++++++++++++ .../moulberry/notenoughupdates/util/Utils.java | 13 +++- 3 files changed, 88 insertions(+), 4 deletions(-) create mode 100644 src/main/java/io/github/moulberry/notenoughupdates/util/SkytilsCompat.java (limited to 'src') diff --git a/src/main/java/io/github/moulberry/notenoughupdates/miscgui/GuiItemRecipe.java b/src/main/java/io/github/moulberry/notenoughupdates/miscgui/GuiItemRecipe.java index 9256a193..f14e2e0e 100644 --- a/src/main/java/io/github/moulberry/notenoughupdates/miscgui/GuiItemRecipe.java +++ b/src/main/java/io/github/moulberry/notenoughupdates/miscgui/GuiItemRecipe.java @@ -117,7 +117,7 @@ public class GuiItemRecipe extends GuiScreen { List slots = getAllRenderedSlots(); for (RecipeSlot slot : slots) { - Utils.drawItemStack(slot.getItemStack(), slot.getX(this), slot.getY(this)); + Utils.drawItemStack(slot.getItemStack(), slot.getX(this), slot.getY(this), true); } drawArrows(currentRecipe, mouseX, mouseY); diff --git a/src/main/java/io/github/moulberry/notenoughupdates/util/SkytilsCompat.java b/src/main/java/io/github/moulberry/notenoughupdates/util/SkytilsCompat.java new file mode 100644 index 00000000..623a6c1a --- /dev/null +++ b/src/main/java/io/github/moulberry/notenoughupdates/util/SkytilsCompat.java @@ -0,0 +1,77 @@ +package io.github.moulberry.notenoughupdates.util; + +import net.minecraft.item.ItemStack; + +import java.lang.invoke.MethodHandle; +import java.lang.invoke.MethodHandles; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.security.cert.TrustAnchor; + +public class SkytilsCompat { + // Defer static initialization + private static class Holder { + // Skytils is present in some capacity + static boolean isSkytilsPresent = false; + // All classes successfully loaded + static boolean isSkytilsFullyPresent = false; + + static Class skytilsClass = null; + static Method renderRarityMethod = null; + static Class renderUtilClass = null; + + static Object skytilsCompanionObject = null; + + static Class skytilsConfigClass = null; + + static Object skytilsConfigObject = null; + static Method skytilsGetShowItemRarity = null; + + static { + try { + skytilsClass = Class.forName("skytils.skytilsmod.Skytils"); + isSkytilsPresent = true; + } catch (ClassNotFoundException ignored) { + } + try { + Class skytilsCompanionClass = Class.forName("skytils.skytilsmod.Skytils$Companion"); + skytilsConfigClass = Class.forName("skytils.skytilsmod.core.Config"); + Field skytilsCompanionField = skytilsClass.getField("Companion"); + skytilsCompanionObject = skytilsCompanionField.get(null); + Method skytilsGetConfigMethod = skytilsCompanionClass.getMethod("getConfig"); + skytilsConfigObject = skytilsGetConfigMethod.invoke(skytilsCompanionObject); + skytilsGetShowItemRarity = skytilsConfigClass.getMethod("getShowItemRarity"); + renderUtilClass = Class.forName("skytils.skytilsmod.utils.RenderUtil"); + renderRarityMethod = renderUtilClass.getDeclaredMethod( + "renderRarity", + ItemStack.class, + Integer.TYPE, + Integer.TYPE + ); + isSkytilsFullyPresent = true; + } catch (ClassNotFoundException | NoSuchMethodException | NoSuchFieldException | IllegalAccessException | + InvocationTargetException e) { + System.err.println("Failed to get Skytils class even tho Skytils mod is present. This is (probably) a NEU bug"); + e.printStackTrace(); + } + } + + } + + public static void renderSkytilsRarity(ItemStack stack, int x, int y) { + renderSkytilsRarity(stack, x, y, false); + } + + public static void renderSkytilsRarity(ItemStack stack, int x, int y, boolean force) { + if (Holder.isSkytilsFullyPresent) { + try { + if (force || (boolean) Holder.skytilsGetShowItemRarity.invoke(Holder.skytilsConfigObject)) + Holder.renderRarityMethod.invoke(null, stack, x, y); + } catch (IllegalAccessException | InvocationTargetException e) { + throw new RuntimeException(e); + } + } + } + +} diff --git a/src/main/java/io/github/moulberry/notenoughupdates/util/Utils.java b/src/main/java/io/github/moulberry/notenoughupdates/util/Utils.java index 31499f7f..d6c0fb27 100644 --- a/src/main/java/io/github/moulberry/notenoughupdates/util/Utils.java +++ b/src/main/java/io/github/moulberry/notenoughupdates/util/Utils.java @@ -175,8 +175,13 @@ public class Utils { } public static void drawItemStackWithText(ItemStack stack, int x, int y, String text) { - if (stack == null) return; + drawItemStackWithText(stack, x, y, text, false); + } + public static void drawItemStackWithText(ItemStack stack, int x, int y, String text, boolean skytilsRarity) { + if (stack == null) return; + if (skytilsRarity) + SkytilsCompat.renderSkytilsRarity(stack, x, y); RenderItem itemRender = Minecraft.getMinecraft().getRenderItem(); disableCustomDungColours = true; @@ -190,11 +195,13 @@ public class Utils { } public static void drawItemStack(ItemStack stack, int x, int y) { - if (stack == null) return; - drawItemStackWithText(stack, x, y, null); } + public static void drawItemStack(ItemStack stack, int x, int y, boolean skytilsRarity) { + drawItemStackWithText(stack, x, y, null, skytilsRarity); + } + private static final EnumChatFormatting[] rainbow = new EnumChatFormatting[]{ EnumChatFormatting.RED, EnumChatFormatting.GOLD, -- cgit