Skip to content

Commit 3f096ff

Browse files
committed
display error screen when java version is > 21
1 parent de31ca1 commit 3f096ff

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

src/main/java/com/cleanroommc/groovyscript/GroovyScript.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import net.minecraftforge.fml.common.gameevent.InputEvent;
5656
import net.minecraftforge.fml.relauncher.FMLInjectionData;
5757
import net.minecraftforge.fml.relauncher.FMLLaunchHandler;
58+
import net.minecraftforge.fml.relauncher.Side;
5859
import org.apache.logging.log4j.LogManager;
5960
import org.apache.logging.log4j.Logger;
6061
import 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
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.cleanroommc.groovyscript;
2+
3+
import net.minecraft.client.gui.FontRenderer;
4+
import net.minecraft.client.gui.GuiErrorScreen;
5+
import net.minecraftforge.fml.client.CustomModLoadingErrorDisplayException;
6+
7+
import java.util.List;
8+
9+
public class IncompatibleJavaException extends CustomModLoadingErrorDisplayException {
10+
11+
private final String msg;
12+
13+
public IncompatibleJavaException(String msg) {
14+
this.msg = msg;
15+
}
16+
17+
@Override
18+
public void initGui(GuiErrorScreen errorScreen, FontRenderer fontRenderer) {}
19+
20+
@Override
21+
public void drawScreen(GuiErrorScreen errorScreen, FontRenderer fontRenderer, int mouseRelX, int mouseRelY, float tickTime) {
22+
List<String> lines = fontRenderer.listFormattedStringToWidth(this.msg, errorScreen.width - 40);
23+
int buttonSpace = 20 + 2 * 18; // button is 18 pixel high + 18 pixel margin on both sides
24+
int y = (errorScreen.height - buttonSpace) / 2 - (fontRenderer.FONT_HEIGHT * 2 + 1) / 2; // font height * 2 /
25+
for (String line : lines) {
26+
int width = fontRenderer.getStringWidth(line);
27+
int x = errorScreen.width / 2 - width / 2;
28+
fontRenderer.drawStringWithShadow(line, x, y, 0xFFFFFFFF);
29+
y += fontRenderer.FONT_HEIGHT;
30+
}
31+
}
32+
}

src/main/java/com/cleanroommc/groovyscript/core/mixin/LoaderControllerMixin.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@
22

33
import com.cleanroommc.groovyscript.GroovyScript;
44
import com.cleanroommc.groovyscript.sandbox.LoadStage;
5+
import net.minecraftforge.fml.client.CustomModLoadingErrorDisplayException;
56
import net.minecraftforge.fml.common.LoadController;
67
import net.minecraftforge.fml.common.LoaderState;
8+
import net.minecraftforge.fml.common.ModContainer;
79
import org.spongepowered.asm.mixin.Mixin;
810
import org.spongepowered.asm.mixin.injection.At;
911
import org.spongepowered.asm.mixin.injection.Inject;
1012
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
1113

14+
import java.lang.reflect.InvocationTargetException;
15+
1216
@Mixin(value = LoadController.class, remap = false)
1317
public class LoaderControllerMixin {
1418

@@ -24,4 +28,15 @@ public void preInit(LoaderState state, Object[] eventData, CallbackInfo ci) {
2428
GroovyScript.runGroovyScriptsInLoader(LoadStage.POST_INIT);
2529
}
2630
}
31+
32+
@Inject(method = "errorOccurred", at = @At(value = "NEW", target = "(Ljava/lang/String;Ljava/lang/Object;Ljava/lang/Object;)Lorg/apache/logging/log4j/message/FormattedMessage;", shift = At.Shift.BEFORE))
33+
public void errorOccured(ModContainer modContainer, Throwable exception, CallbackInfo ci) throws Throwable {
34+
if (exception instanceof InvocationTargetException) {
35+
exception = exception.getCause();
36+
}
37+
if (exception instanceof CustomModLoadingErrorDisplayException) {
38+
// normally every exception gets wrapped in a LoaderException making these """custom""" exceptions useless, thanks cpw
39+
throw exception;
40+
}
41+
}
2742
}

0 commit comments

Comments
 (0)