Skip to content
This repository was archived by the owner on Jul 4, 2022. It is now read-only.

Commit ed579af

Browse files
committed
Improve Java16 support for legacy mc versions
1 parent 4164d22 commit ed579af

File tree

1 file changed

+45
-8
lines changed
  • simplixcore-minecraft/simplixcore-minecraft-spigot/simplixcore-minecraft-spigot-plugin/src/main/java/dev/simplix/core/minecraft/spigot/plugin/libloader

1 file changed

+45
-8
lines changed

simplixcore-minecraft/simplixcore-minecraft-spigot/simplixcore-minecraft-spigot-plugin/src/main/java/dev/simplix/core/minecraft/spigot/plugin/libloader/PluginClassLoaderFabricator.java

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.google.common.io.ByteStreams;
44
import dev.simplix.core.common.libloader.SimplixClassLoader;
5-
import dev.simplix.core.common.updater.Version;
65
import dev.simplix.core.minecraft.spigot.plugin.SimplixPlugin;
76
import java.io.File;
87
import java.lang.reflect.*;
@@ -27,8 +26,17 @@
2726
@Slf4j
2827
public final class PluginClassLoaderFabricator implements Function<File, ClassLoader> {
2928

29+
private static boolean hasClassLoaderAccess = false;
3030
private static ClassLoader cachedResult;
31-
private final Version javaVersion = Version.parse(System.getProperty("java.version"));
31+
32+
static {
33+
try {
34+
Method addMethod = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
35+
addMethod.setAccessible(true);
36+
hasClassLoaderAccess = true;
37+
} catch (Throwable ignored) {
38+
}
39+
}
3240

3341
private void unfinalize(@NonNull Field loadersField)
3442
throws NoSuchFieldException, IllegalAccessException {
@@ -48,14 +56,15 @@ public ClassLoader apply(@NonNull File file) {
4856
ClassLoader out;
4957
try {
5058
injectFakeClass(file);
51-
5259
SimplixPlugin plugin = JavaPlugin.getPlugin(SimplixPlugin.class);
60+
5361
PluginDescriptionFile pluginDescriptionFile = new PluginDescriptionFile(
5462
SimplixPlugin.class.getResourceAsStream("/fakeplugin.yml"));
5563

5664
Object pluginClassloader;
5765

5866
try {
67+
5968
Class<?> classLoaderClass = Class.forName("org.bukkit.plugin.java.PluginClassLoader");
6069
Constructor<?> constructor = classLoaderClass.getDeclaredConstructor(
6170
JavaPluginLoader.class,
@@ -73,8 +82,22 @@ public ClassLoader apply(@NonNull File file) {
7382
file
7483
);
7584
out = (ClassLoader) pluginClassloader;
85+
86+
// Check whether the fabricated classloader will actually work
87+
if (!hasClassLoaderAccess) {
88+
log.error("SimplixCore can not fabricate ClassLoaders");
89+
log.error("Your minecraft version is EOL & SimplixCore can't access java.base/java.net");
90+
log.error("-> New api is not (yet) present and legacy api is blocked");
91+
log.error("1) Upgrade to Minecraft 1.17 (Recommended)");
92+
log.error("2) Add: --add-opens java.base/java.net=ALL-UNNAMED as jvm flag");
93+
log.error("- Example java command: java --add-opens java.base/java.net=ALL-UNNAMED -Xmx2048M -Xms2048M -jar paper-1.12.2.jar");
94+
log.error("3) Downgrade to Java11 ");
95+
throw new IllegalStateException(
96+
"Can not fabricate ClassLoader with outdated Minecraft and Java16");
97+
}
98+
7699
} catch (Throwable throwable) {
77-
if (javaVersion.olderThen(Version.parse("16.0.0"))) {
100+
if (hasClassLoaderAccess && !Bukkit.getBukkitVersion().startsWith("1.17")) {
78101
log.info("Used Java11 fabricator");
79102
// Spigot 1.16 or newer using old reflection
80103
Class<?> classLoaderClass = Class.forName("org.bukkit.plugin.java.PluginClassLoader");
@@ -96,9 +119,25 @@ public ClassLoader apply(@NonNull File file) {
96119
null
97120
);
98121
out = (ClassLoader) pluginClassloader;
122+
123+
// Spigot 1.17 newer - Compatible with Java16
99124
} else {
125+
126+
if (!hasClassLoaderAccess) {
127+
log.error("SimplixCore can not fabricate ClassLoaders");
128+
log.error(
129+
"Your minecraft version contains a bug & SimplixCore can't access java.base/java.net");
130+
log.error("Please see: https://hub.spigotmc.org/jira/browse/SPIGOT-6502");
131+
log.error("To resolve this you can either:");
132+
log.error("1) Upgrade to Minecraft 1.17 (Recommended)");
133+
log.error("2) Add: --add-opens java.base/java.net=ALL-UNNAMED as jvm flag");
134+
log.error(
135+
"- Example java command: java --add-opens java.base/java.net=ALL-UNNAMED -Xmx2048M -Xms2048M -jar paper-1.16.5.jar");
136+
log.error("3) Downgrade to Java11");
137+
throw new IllegalStateException(
138+
"Can not fabricate ClassLoader with outdated Minecraft and Java16");
139+
}
100140
log.info("Used Java16 fabricator");
101-
// Spigot 1.16 newer - Compatible with Java16
102141
Class<?> classLoaderClass = Class.forName("org.bukkit.plugin.java.PluginClassLoader");
103142
Constructor<?> constructor = classLoaderClass.getDeclaredConstructor(
104143
JavaPluginLoader.class,
@@ -110,9 +149,7 @@ public ClassLoader apply(@NonNull File file) {
110149
);
111150
constructor.setAccessible(true);
112151

113-
final ClassLoader simplixCoreClassLoader = Bukkit
114-
.getPluginManager()
115-
.getPlugin("SimplixCore")
152+
final ClassLoader simplixCoreClassLoader = plugin
116153
.getClass()
117154
.getClassLoader();
118155

0 commit comments

Comments
 (0)