From d15348fe2682af83a253198af1925309224dfe8c Mon Sep 17 00:00:00 2001 From: hannibal2 <24389977+hannibal00212@users.noreply.github.com> Date: Sun, 31 Jul 2022 14:05:08 +0200 Subject: Support for older skytils versions (#199) * rarity coloring support for older skytils versions * fixed hiding stack trace when skytils is not found (only showing when refactor logic itself fails) --- .../notenoughupdates/util/SkytilsCompat.java | 64 +++++++++++++--------- 1 file changed, 39 insertions(+), 25 deletions(-) diff --git a/src/main/java/io/github/moulberry/notenoughupdates/util/SkytilsCompat.java b/src/main/java/io/github/moulberry/notenoughupdates/util/SkytilsCompat.java index 97c11fe2..f0cb5732 100644 --- a/src/main/java/io/github/moulberry/notenoughupdates/util/SkytilsCompat.java +++ b/src/main/java/io/github/moulberry/notenoughupdates/util/SkytilsCompat.java @@ -24,6 +24,7 @@ import net.minecraft.item.ItemStack; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import java.util.Arrays; public class SkytilsCompat { // Defer static initialization @@ -45,34 +46,47 @@ public class SkytilsCompat { static Method skytilsGetShowItemRarity = null; static { - try { - skytilsClass = Class.forName("gg.skytils.skytilsmod.Skytils"); - isSkytilsPresent = true; - } catch (ClassNotFoundException ignored) { + Exception exception = null; + for (String packageStart : Arrays.asList("gg.skytils", "skytils")) { + isSkytilsPresent = false; + try { + skytilsClass = Class.forName(packageStart + ".skytilsmod.Skytils"); + isSkytilsPresent = true; + } catch (ClassNotFoundException ignored) { + } + + if (isSkytilsPresent) { + try { + Class skytilsCompanionClass = Class.forName(packageStart + ".skytilsmod.Skytils$Companion"); + skytilsConfigClass = Class.forName(packageStart + ".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(packageStart + ".skytilsmod.utils.RenderUtil"); + renderRarityMethod = renderUtilClass.getDeclaredMethod( + "renderRarity", + ItemStack.class, + Integer.TYPE, + Integer.TYPE + ); + isSkytilsFullyPresent = true; + break; + } catch (ClassNotFoundException | NoSuchMethodException | NoSuchFieldException | IllegalAccessException | + InvocationTargetException e) { + exception = e; + } + } } - try { - Class skytilsCompanionClass = Class.forName("gg.skytils.skytilsmod.Skytils$Companion"); - skytilsConfigClass = Class.forName("gg.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("gg.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(); + + if (!isSkytilsFullyPresent) { + if (exception != null) { + System.err.println("Failed to get Skytils class even tho Skytils mod is present. This is (probably) a NEU bug"); + exception.printStackTrace(); + } } } - } public static boolean isSkytilsFullyLoaded() { -- cgit