Skip to content

Commit 0a92eef

Browse files
authored
Fix GroovyScript crashing on servers (#293)
* fix TextureBinder causing a client class to load serverside * mark IncompatibleJavaException as client * extract java version code to own class * extract client-only exception to separate method
1 parent 08aaa3c commit 0a92eef

File tree

4 files changed

+78
-26
lines changed

4 files changed

+78
-26
lines changed

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

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@
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;
5958
import org.apache.logging.log4j.LogManager;
6059
import org.apache.logging.log4j.Logger;
6160
import org.jetbrains.annotations.ApiStatus;
@@ -102,8 +101,7 @@ public class GroovyScript {
102101

103102
@Mod.EventHandler
104103
public void onConstruction(FMLConstructionEvent event) {
105-
int jv = getJavaVersion();
106-
if (jv > 21) handleJavaVersionException(jv, event.getSide());
104+
JavaVersionCheck.validateJavaVersion(event.getSide());
107105
if (!SandboxData.isInitialised()) {
108106
LOGGER.throwing(new IllegalStateException("Sandbox data should have been initialised by now, but isn't! Trying to initialize again."));
109107
SandboxData.initialize((File) FMLInjectionData.data()[6], LOGGER);
@@ -130,17 +128,6 @@ public void onConstruction(FMLConstructionEvent event) {
130128
getRunConfig().initPackmode();
131129
}
132130

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-
144131
@Mod.EventHandler
145132
public void onInit(FMLInitializationEvent event) {
146133
if (ModSupport.TINKERS_CONSTRUCT.isLoaded()) TinkersConstruct.init();
@@ -325,16 +312,4 @@ public static void doForGroovyScript(Runnable runnable) {
325312
runnable.run();
326313
Loader.instance().setActiveModContainer(current);
327314
}
328-
329-
public static int getJavaVersion() {
330-
// from stack overflow
331-
String version = System.getProperty("java.version");
332-
if (version.startsWith("1.")) {
333-
version = version.substring(2, 3);
334-
} else {
335-
int dot = version.indexOf(".");
336-
if (dot != -1) version = version.substring(0, dot);
337-
}
338-
return Integer.parseInt(version);
339-
}
340315
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
import net.minecraft.client.gui.FontRenderer;
44
import net.minecraft.client.gui.GuiErrorScreen;
55
import net.minecraftforge.fml.client.CustomModLoadingErrorDisplayException;
6+
import net.minecraftforge.fml.relauncher.Side;
7+
import net.minecraftforge.fml.relauncher.SideOnly;
68

79
import java.util.List;
810

11+
@SideOnly(Side.CLIENT)
912
public class IncompatibleJavaException extends CustomModLoadingErrorDisplayException {
1013

1114
private final String msg;
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.cleanroommc.groovyscript;
2+
3+
import net.minecraftforge.fml.relauncher.Side;
4+
import net.minecraftforge.fml.relauncher.SideOnly;
5+
6+
/**
7+
* Checks that the Java version being used can be used by the Groovy that GroovyScript uses.
8+
* Our version of Groovy is currently not compatible with java versions above 21.
9+
*/
10+
public class JavaVersionCheck {
11+
12+
private static final int MAXIMUM_VERSION = 21;
13+
14+
/**
15+
* Checks that the Java version being used can run GroovyScript scripts.
16+
*/
17+
public static void validateJavaVersion(Side side) {
18+
int version = getJavaVersion();
19+
if (version > MAXIMUM_VERSION) handleJavaVersionException(version, side);
20+
}
21+
22+
private static void handleJavaVersionException(int version, Side side) {
23+
String msg1 = "GroovyScript's version of Groovy does not work with Java versions greater than " + MAXIMUM_VERSION + " currently.";
24+
String msg2 = "Please downgrade to Java " + MAXIMUM_VERSION + " or lower. Your current Java version is " + version + ".";
25+
if (side.isClient()) {
26+
throwIncompatibleJavaException(msg1 + "\n" + msg2);
27+
} else {
28+
throw new IllegalStateException(msg1 + " " + msg2);
29+
}
30+
}
31+
32+
/**
33+
* Because the super class of this exception is client only (since the screen only works on client)
34+
* this has to be in a separate method.
35+
*/
36+
@SideOnly(Side.CLIENT)
37+
private static void throwIncompatibleJavaException(String msg) {
38+
throw new IncompatibleJavaException(msg);
39+
}
40+
41+
/**
42+
* Gets the major version of Java currently running.
43+
* <table>
44+
* <tr>
45+
* <th>1.8.0_51</th>
46+
* <th>=</th>
47+
* <th>8</th>
48+
* </tr>
49+
* <tr>
50+
* <th>21.0.6</th>
51+
* <th>=</th>
52+
* <th>21</th>
53+
* </tr>
54+
* </table>
55+
* Code comes from
56+
* <a href="https://stackoverflow.com/questions/2591083/getting-java-version-at-runtime">Stack Overflow</a>.
57+
*/
58+
private static int getJavaVersion() {
59+
String version = System.getProperty("java.version");
60+
if (version.startsWith("1.")) {
61+
version = version.substring(2, 3);
62+
} else {
63+
int dot = version.indexOf(".");
64+
if (dot != -1) version = version.substring(0, dot);
65+
}
66+
return Integer.parseInt(version);
67+
}
68+
}

src/main/java/com/cleanroommc/groovyscript/mapper/TextureBinder.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
1111
import net.minecraft.item.ItemStack;
1212
import net.minecraftforge.fluids.FluidStack;
13+
import net.minecraftforge.fml.common.FMLCommonHandler;
14+
import net.minecraftforge.fml.relauncher.Side;
15+
import net.minecraftforge.fml.relauncher.SideOnly;
1316
import org.jetbrains.annotations.ApiStatus;
1417

1518
import java.util.List;
@@ -46,6 +49,7 @@ static <A, T> TextureBinder<A> ofArray(Function<A, T[]> mapper, TextureBinder<T>
4649
}
4750

4851
static TextureBinder<ItemStack> ofItem() {
52+
if (FMLCommonHandler.instance().getSide().isServer()) return x -> {};
4953
return item -> {
5054
GlStateManager.enableDepth();
5155
RenderHelper.enableGUIStandardItemLighting();
@@ -63,6 +67,7 @@ static TextureBinder<ItemStack> ofItem() {
6367
}
6468

6569
static TextureBinder<FluidStack> ofFluid() {
70+
if (FMLCommonHandler.instance().getSide().isServer()) return x -> {};
6671
return fluid -> {
6772
GlStateManager.enableBlend();
6873
GlStateManager.enableAlpha();
@@ -79,6 +84,7 @@ static TextureBinder<FluidStack> ofFluid() {
7984
};
8085
}
8186

87+
@SideOnly(Side.CLIENT)
8288
static void drawSprite(TextureAtlasSprite textureSprite) {
8389
double uMin = textureSprite.getMinU();
8490
double uMax = textureSprite.getMaxU();

0 commit comments

Comments
 (0)