5555import net .minecraftforge .fml .common .gameevent .InputEvent ;
5656import net .minecraftforge .fml .relauncher .FMLInjectionData ;
5757import net .minecraftforge .fml .relauncher .FMLLaunchHandler ;
58+ import net .minecraftforge .fml .relauncher .Side ;
5859import org .apache .logging .log4j .LogManager ;
5960import org .apache .logging .log4j .Logger ;
6061import org .jetbrains .annotations .ApiStatus ;
@@ -101,6 +102,8 @@ public class GroovyScript {
101102
102103 @ Mod .EventHandler
103104 public void onConstruction (FMLConstructionEvent event ) {
105+ int jv = getJavaVersion ();
106+ if (jv > 21 ) handleJavaVersionException (jv , event .getSide ());
104107 if (!SandboxData .isInitialised ()) {
105108 LOGGER .throwing (new IllegalStateException ("Sandbox data should have been initialised by now, but isn't! Trying to initialize again." ));
106109 SandboxData .initialize ((File ) FMLInjectionData .data ()[6 ], LOGGER );
@@ -127,6 +130,17 @@ public void onConstruction(FMLConstructionEvent event) {
127130 getRunConfig ().initPackmode ();
128131 }
129132
133+ private static void handleJavaVersionException (int version , Side side ) {
134+ String msg1 = "Groovy does not work with Java versions greater than 21 currently." ;
135+ String msg2 = "Please downgrade to Java 21 or lower. Your current Java version is " + version + "." ;
136+ if (side .isClient ()) {
137+ // the super class of this exception is client only (since the screen only works on client)
138+ throw new IncompatibleJavaException (msg1 + "\n " + msg2 );
139+ } else {
140+ throw new IllegalStateException (msg1 + " " + msg2 );
141+ }
142+ }
143+
130144 @ Mod .EventHandler
131145 public void onInit (FMLInitializationEvent event ) {
132146 if (ModSupport .TINKERS_CONSTRUCT .isLoaded ()) TinkersConstruct .init ();
@@ -312,4 +326,16 @@ public static void doForGroovyScript(Runnable runnable) {
312326 runnable .run ();
313327 Loader .instance ().setActiveModContainer (current );
314328 }
329+
330+ public static int getJavaVersion () {
331+ // from stack overflow
332+ String version = System .getProperty ("java.version" );
333+ if (version .startsWith ("1." )) {
334+ version = version .substring (2 , 3 );
335+ } else {
336+ int dot = version .indexOf ("." );
337+ if (dot != -1 ) version = version .substring (0 , dot );
338+ }
339+ return Integer .parseInt (version );
340+ }
315341}
0 commit comments