diff options
author | makamys <makamys@outlook.com> | 2023-12-02 15:09:58 +0100 |
---|---|---|
committer | makamys <makamys@outlook.com> | 2023-12-03 00:38:36 +0100 |
commit | 3aa975f0a53a9d95fef1b4efffaf1483f9e7945a (patch) | |
tree | ce2351cafaeb921587fdda41938d1ed3dd42dff3 | |
parent | dd40c5c4b5ad6e4ff03d60892c2498dff708e003 (diff) | |
download | Neodymium-3aa975f0a53a9d95fef1b4efffaf1483f9e7945a.tar.gz Neodymium-3aa975f0a53a9d95fef1b4efffaf1483f9e7945a.tar.bz2 Neodymium-3aa975f0a53a9d95fef1b4efffaf1483f9e7945a.zip |
Don't apply OF compat if OF has failed to load
Should fix reports like https://github.com/makamys/Neodymium/issues/13
-rw-r--r-- | src/main/java/makamys/neodymium/util/OFUtil.java | 34 |
1 files changed, 32 insertions, 2 deletions
diff --git a/src/main/java/makamys/neodymium/util/OFUtil.java b/src/main/java/makamys/neodymium/util/OFUtil.java index c98a651..6c007ca 100644 --- a/src/main/java/makamys/neodymium/util/OFUtil.java +++ b/src/main/java/makamys/neodymium/util/OFUtil.java @@ -1,13 +1,19 @@ package makamys.neodymium.util; -import makamys.neodymium.MixinConfigPlugin; +import static makamys.neodymium.Constants.LOGGER; + +import java.lang.reflect.Field; + import makamys.neodymium.ducks.IMixinGameSettings_OptiFine; import net.minecraft.client.Minecraft; public class OFUtil { - private static boolean isOptiFinePresent = MixinConfigPlugin.class.getResource("/optifine/OptiFineTweaker.class") != null; + private static boolean isOptiFinePresent, hasCheckedIsOptiFinePresent; public static boolean isOptiFinePresent() { + if(!hasCheckedIsOptiFinePresent) { + checkIfOptiFineIsPresent(); + } return isOptiFinePresent; } @@ -15,6 +21,30 @@ public class OFUtil { return isOptiFinePresent && getIsFogOff(); } + private static void checkIfOptiFineIsPresent() { + try { + Class<?> optiFineClassTransformerClass = Class.forName("optifine.OptiFineClassTransformer"); + Field instanceField = optiFineClassTransformerClass.getField("instance"); + Object optiFineClassTransformer = instanceField.get(null); + Field ofZipFileField = optiFineClassTransformerClass.getDeclaredField("ofZipFile"); + ofZipFileField.setAccessible(true); + Object ofZipFile = ofZipFileField.get(optiFineClassTransformer); + + if(ofZipFile != null) { + LOGGER.debug("OptiFineClassTransformer#ofZipFile is initialized, assuming OptiFine is present"); + isOptiFinePresent = true; + } else { + LOGGER.debug("OptiFineClassTransformer#ofZipFile is null, assuming OptiFine is not present"); + } + } catch (ClassNotFoundException e) { + LOGGER.debug("Couldn't load OptiFineClassTransformer class, assuming OptiFine is not present"); + // no OF + } catch(Exception e) { + LOGGER.error("Failed to check if OptiFine is loaded, assuming it isn't. Exception: " + e); + } + hasCheckedIsOptiFinePresent = true; + } + private static boolean getIsFogOff() { return ((IMixinGameSettings_OptiFine)Minecraft.getMinecraft().gameSettings).getOfFogType() == 3; } |