Put the multimc zip in the github release.
LWJGL3ify
Brings LWJGL3 and modern Java versions to Minecraft 1.7.10.
Usage
Client
To install in PrismLauncher or MultiMC:
Copy the contents of lwjgl3ify-VERSION-multimc.zip to instances/My Modpack/. mmc-pack.json needs to be overwritten.
Reload the launcher, and it should show up modified versions of Forge, Minecraft and LWJGL in the instance window.
Put the mod jar (lwjgl3ify-VERSION.jar) in mods/ in your instance's minecraft folder, it will get loaded as a coremod.
The forgePatches jar will get automatically downloaded by the launcher.
Change the instance java version to 11, 17 or 19 (these are the currently supported versions).
Server
Install the mod jar in mods/, and download the forgePatches jar. Put the forgePatches jar in the same folder as your forge-universal and minecraft server jars.
Rename the forgePatches jar to just lwjgl3ify-forgePatches.jar. Create a file named java9args.txt with the contents of <java9args.txt>.
You can now launch the server with a command like the following, assuming the first java executable on your PATH is java 11/17/19:
java -Xmx6G -Xms6G -XX:+UseShenandoahGC @java9args.txt nogui
How does it work?
There are a few components to enabling modern java and lwjgl usage on old minecraft versions, lwjgl3ify implements these for 1.7.10 specifically:
- A modified launcher profile (templates are in
prism-libraries) to modify the library list loaded into the java classpath - to install LWJGL3, ASM 9.4, new Apache Commons and our forgePatches jar. - A custom server launcher jar with the modified
Class-PathMETA-INF manifest entry (integrated into our forgePatches jar) - An updated launchwrapper with a java 9+-compatible classpath discovery mechanism at https://github.com/GTNewHorizons/LegacyLauncher
- A partially auto-generated compatibility shim redirecting lwjgl2 api calls into the matching lwjgl3 calls (in
src/main/java/org/lwjglx,src/generated/java/org/lwjglx), the generator is insrc/util/java - Dummy
org/lwjgl/*classes matching the classes removed from lwjgl3 (e.g. Display) for mods that rely on finding them under that location in the classpath - e.g. for mixin metadata - Asm transformers:
- Asm transformer renaming any access to org/lwjgl to org/lwjglx after all other transformations have been done to the class - in
LwjglRedirectTransformer.java javax/xml/bind->jakarta/xml/bind/asm transformer and a shaded version of jakarta.xml.bind, because that API was removed from JRE 11- Extensible enum transformer (copied under the lgpl license from Forge for minecraft 1.19.3) to replace a lot of the EnumHelper usages
- Object holders unfinalizer, to remove the need for overwriting
static finalfields in Forge. FixConstantPoolInterfaceMethodRefHelperis a transformer to fix a javac 8 bug which generated wrong entries in the constant pool if lambda functions were declared as fields in interfaces
- Asm transformer renaming any access to org/lwjgl to org/lwjglx after all other transformations have been done to the class - in
- Mixins:
- Object holder modifications to no longer write to
static finalfields - JarDiscoverer change to ignore
module-info.classandMETA-INF/versions/*classes because it's not Java 9-aware - PostMixinTransformInjector used to move our asm transformer to run after the mixin processing
- Object holder modifications to no longer write to
UnsafeHacksclass from modern forge, modified to work in more usecases, for replacing somestatic finalwrites where it was not feasible/easy to remove final.- Forge patches, which are modified classes from Forge that cannot be transformed because they are loaded too early - so we load a
forgePatchesjar before Forge in the classpath to replace them:- EventSubscriptionTransformer: the format of subclass annotation descriptors has slightly changed in Java 17, this makes the annotation discovery compatible with both
- ClassPatchManager: switches the Pack200 implementation to an Apache Commons Compress one because it was removed from newer JDKs
- TracingPrintStream: commons-compress closes System.out, this blocks this from happening
- CoreModManager: work around the fact that the system classloader is no longer a URLClassLoader
- FMLSecurityManager: fix some array index out of bounds exceptions, it is also deprecated in java 17 and will need to be replaced once a new API is available
- EnumHelper: replace all the enumhelper apis with new variants using the extensible enum transformer and unsafe hacks.
Fixing mods
This mod is not enough to make modpacks work in Java 9+, mods had to be updated. Here's a potentially incomplete list of breaking changes that needed code changes in mods, or mixins to fix non-open-source mods:
System class loader is no longer a URLClassLoader
To get the classpath, simply split System.getProperty("java.class.path") by File.pathSeparator.
Do note that for most mods, their classloader is still a LaunchClassLoader which does extend URLClassLoader.
To modify the URLClassPath you can use the following code:
public static void addUrlToClassloader(ClassLoader loader, URL coreModUrl) {
try {
if (loader instanceof URLClassLoader) {
if (ADDURL == null) {
ADDURL = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
ADDURL.setAccessible(true);
}
ADDURL.invoke(loader, coreModUrl);
} else {
Field ucpField;
try {
// Java 8-11
ucpField = loader.getClass().getDeclaredField("ucp");
} catch (NoSuchFieldException e) {
// Java 17
ucpField = loader.getClass().getSuperclass().getDeclaredField("ucp");
}
ucpField.setAccessible(true);
// URLClassPath is in different packages in different Java versions, so we use Object.
final Object ucp = ucpField.get(loader);
final Method urlAdder = ucp.getClass().getDeclaredMethod("addURL", URL.class);
urlAdder.invoke(ucp, coreModUrl);
}
} catch (ReflectiveOperationException e) {
throw new RuntimeException("Couldn't add url to classpath in loader " + loader.getClass(), e);
}
}
java.base/java.lang.reflect classes are protected from reflective access
E.g. Field.class.getDeclaredFields() returns an empty list. The most common use of this was to change the modifiers field to strip final from the Field accessor to trick it into letting you write into static final fields.
Instead of that, use an access transformer (public-f class field_srg # field_mcp) to remove the final attribute at class load time.
Alternatively, inject a different initializer via Mixins/asm by modifying the class static constructor special method <clinit>.
If that is not feasible (e.g. runtime reflection discovering fields), you can use sun.misc.Unsafe to directly write to the memory location of the final field like so:
final Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
theUnsafe.setAccessible(true);
final Unsafe UNSAFE = (Unsafe) theUnsafe.get(null); // Cache this
public static void setField(Field data, Object object, Object value) {
if (object == null) {
long offset = UNSAFE.staticFieldOffset(data);
Object base = UNSAFE.staticFieldBase(data);
UNSAFE.putObject(base, offset, value);
} else {
long offset = UNSAFE.objectFieldOffset(data);
UNSAFE.putObject(object, offset, value);
}
}
Beware, the Java JIT compiler will assume that final fields don't change value at runtime, so the value you write there using Unsafe might not actually be read back by some code. This can lead to inconsistent states, where some methods might see the old value while other ones will see the new value, especially if you change the value down a callchain that already read the value earlier.
Development
For developing locally, modify the patches/me.eigenraven.lwjgl3ify.forgepatches.json file with the following content:
{
"formatVersion": 1,
"name": "Forge-J9-Patches",
"uid": "me.eigenraven.lwjgl3ify.forgepatches",
"version": "1",
"order": 3,
"libraries": [
{
"name": "com.gtnewhorizons:lwjgl3ify:1.1.3:forgePatches",
"MMC-hint": "local"
}
]
}
Thanks to MMC-hint you can now put the locally built forgePatches jar into instances/My Modpack/libraries/lwjgl3ify-1.1.3-forgePatches.jar.