diff --git a/src/main/java/com/cleanroommc/groovyscript/GroovyScript.java b/src/main/java/com/cleanroommc/groovyscript/GroovyScript.java index 4e888f7e8..b83f9dda0 100644 --- a/src/main/java/com/cleanroommc/groovyscript/GroovyScript.java +++ b/src/main/java/com/cleanroommc/groovyscript/GroovyScript.java @@ -55,7 +55,6 @@ import net.minecraftforge.fml.common.gameevent.InputEvent; import net.minecraftforge.fml.relauncher.FMLInjectionData; import net.minecraftforge.fml.relauncher.FMLLaunchHandler; -import net.minecraftforge.fml.relauncher.Side; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.jetbrains.annotations.ApiStatus; @@ -102,8 +101,7 @@ public class GroovyScript { @Mod.EventHandler public void onConstruction(FMLConstructionEvent event) { - int jv = getJavaVersion(); - if (jv > 21) handleJavaVersionException(jv, event.getSide()); + JavaVersionCheck.validateJavaVersion(event.getSide()); if (!SandboxData.isInitialised()) { LOGGER.throwing(new IllegalStateException("Sandbox data should have been initialised by now, but isn't! Trying to initialize again.")); SandboxData.initialize((File) FMLInjectionData.data()[6], LOGGER); @@ -130,17 +128,6 @@ public void onConstruction(FMLConstructionEvent event) { getRunConfig().initPackmode(); } - private static void handleJavaVersionException(int version, Side side) { - String msg1 = "Groovy does not work with Java versions greater than 21 currently."; - String msg2 = "Please downgrade to Java 21 or lower. Your current Java version is " + version + "."; - if (side.isClient()) { - // the super class of this exception is client only (since the screen only works on client) - throw new IncompatibleJavaException(msg1 + "\n" + msg2); - } else { - throw new IllegalStateException(msg1 + " " + msg2); - } - } - @Mod.EventHandler public void onInit(FMLInitializationEvent event) { if (ModSupport.TINKERS_CONSTRUCT.isLoaded()) TinkersConstruct.init(); @@ -325,16 +312,4 @@ public static void doForGroovyScript(Runnable runnable) { runnable.run(); Loader.instance().setActiveModContainer(current); } - - public static int getJavaVersion() { - // from stack overflow - String version = System.getProperty("java.version"); - if (version.startsWith("1.")) { - version = version.substring(2, 3); - } else { - int dot = version.indexOf("."); - if (dot != -1) version = version.substring(0, dot); - } - return Integer.parseInt(version); - } } diff --git a/src/main/java/com/cleanroommc/groovyscript/IncompatibleJavaException.java b/src/main/java/com/cleanroommc/groovyscript/IncompatibleJavaException.java index be9022ad0..115c30837 100644 --- a/src/main/java/com/cleanroommc/groovyscript/IncompatibleJavaException.java +++ b/src/main/java/com/cleanroommc/groovyscript/IncompatibleJavaException.java @@ -3,9 +3,12 @@ import net.minecraft.client.gui.FontRenderer; import net.minecraft.client.gui.GuiErrorScreen; import net.minecraftforge.fml.client.CustomModLoadingErrorDisplayException; +import net.minecraftforge.fml.relauncher.Side; +import net.minecraftforge.fml.relauncher.SideOnly; import java.util.List; +@SideOnly(Side.CLIENT) public class IncompatibleJavaException extends CustomModLoadingErrorDisplayException { private final String msg; diff --git a/src/main/java/com/cleanroommc/groovyscript/JavaVersionCheck.java b/src/main/java/com/cleanroommc/groovyscript/JavaVersionCheck.java new file mode 100644 index 000000000..e078c2ef4 --- /dev/null +++ b/src/main/java/com/cleanroommc/groovyscript/JavaVersionCheck.java @@ -0,0 +1,68 @@ +package com.cleanroommc.groovyscript; + +import net.minecraftforge.fml.relauncher.Side; +import net.minecraftforge.fml.relauncher.SideOnly; + +/** + * Checks that the Java version being used can be used by the Groovy that GroovyScript uses. + * Our version of Groovy is currently not compatible with java versions above 21. + */ +public class JavaVersionCheck { + + private static final int MAXIMUM_VERSION = 21; + + /** + * Checks that the Java version being used can run GroovyScript scripts. + */ + public static void validateJavaVersion(Side side) { + int version = getJavaVersion(); + if (version > MAXIMUM_VERSION) handleJavaVersionException(version, side); + } + + private static void handleJavaVersionException(int version, Side side) { + String msg1 = "GroovyScript's version of Groovy does not work with Java versions greater than " + MAXIMUM_VERSION + " currently."; + String msg2 = "Please downgrade to Java " + MAXIMUM_VERSION + " or lower. Your current Java version is " + version + "."; + if (side.isClient()) { + throwIncompatibleJavaException(msg1 + "\n" + msg2); + } else { + throw new IllegalStateException(msg1 + " " + msg2); + } + } + + /** + * Because the super class of this exception is client only (since the screen only works on client) + * this has to be in a separate method. + */ + @SideOnly(Side.CLIENT) + private static void throwIncompatibleJavaException(String msg) { + throw new IncompatibleJavaException(msg); + } + + /** + * Gets the major version of Java currently running. + *
| 1.8.0_51 | + *= | + *8 | + *
|---|---|---|
| 21.0.6 | + *= | + *21 | + *