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

Commit 17e456c

Browse files
committed
Make librayloading compatible with more legacy bukkit builds
1 parent 98e565a commit 17e456c

File tree

2 files changed

+95
-60
lines changed
  • simplixcore-common/simplixcore-common-implementation/src/main/java/dev/simplix/core/common/libloader
  • simplixcore-minecraft/simplixcore-minecraft-spigot/simplixcore-minecraft-spigot-plugin/src/main/java/dev/simplix/core/minecraft/spigot/plugin/libloader

2 files changed

+95
-60
lines changed

simplixcore-common/simplixcore-common-implementation/src/main/java/dev/simplix/core/common/libloader/SimpleLibraryLoader.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.google.gson.GsonBuilder;
55
import dev.simplix.core.common.aop.SimplixApplication;
66
import dev.simplix.core.common.inject.SimplixInstaller;
7+
import dev.simplix.core.common.updater.Version;
78
import java.io.File;
89
import java.io.IOException;
910
import java.io.InputStream;
@@ -13,7 +14,6 @@
1314
import java.net.URL;
1415
import java.net.URLClassLoader;
1516
import java.nio.charset.StandardCharsets;
16-
import java.util.Arrays;
1717
import java.util.HashSet;
1818
import java.util.Set;
1919
import java.util.function.Function;
@@ -23,13 +23,16 @@
2323

2424
public class SimpleLibraryLoader implements LibraryLoader {
2525

26+
private final Version javaVersion;
2627
private final Logger log;
2728
private final Gson gson = new GsonBuilder().create();
2829
private final Set<File> files = new HashSet<>();
2930
private Method addMethod;
3031

3132
public SimpleLibraryLoader(@NonNull Logger log) {
3233
this.log = log;
34+
javaVersion = Version.parse(System.getProperty("java.version"));
35+
3336
}
3437

3538
@Override
@@ -67,7 +70,14 @@ public void loadLibraryEncapsulated(@NonNull File file, @NotNull Class<?> owner)
6770
if (files.contains(file)) {
6871
return;
6972
}
70-
ClassLoader classLoader = createClassLoader(file);
73+
ClassLoader classLoader;
74+
75+
if (javaVersion.olderThen(Version.parse("16.0.0"))) {
76+
classLoader = owner.getClassLoader();
77+
} else {
78+
classLoader = createClassLoader(file);
79+
}
80+
7181
if (!(classLoader instanceof URLClassLoader)) {
7282
log.warn("[Simplix | LibLoader] "
7383
+ owner.getSimpleName()

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

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

33
import com.google.common.io.ByteStreams;
44
import dev.simplix.core.common.libloader.SimplixClassLoader;
5+
import dev.simplix.core.common.updater.Version;
56
import dev.simplix.core.minecraft.spigot.plugin.SimplixPlugin;
67
import java.io.File;
78
import java.lang.reflect.Constructor;
@@ -30,6 +31,7 @@
3031
public final class PluginClassLoaderFabricator implements Function<File, ClassLoader> {
3132

3233
private static ClassLoader cachedResult;
34+
private final Version javaVersion = Version.parse(System.getProperty("os.version"));
3335

3436
private void unfinalize(@NonNull Field loadersField)
3537
throws NoSuchFieldException, IllegalAccessException {
@@ -76,64 +78,87 @@ public ClassLoader apply(@NonNull File file) {
7678
);
7779
out = (ClassLoader) pluginClassloader;
7880
} catch (Throwable throwable) {
79-
// Spigot 1.16 - Compatible with Java16
80-
Class<?> classLoaderClass = Class.forName("org.bukkit.plugin.java.PluginClassLoader");
81-
Constructor<?> constructor = classLoaderClass.getDeclaredConstructor(
82-
JavaPluginLoader.class,
83-
ClassLoader.class,
84-
PluginDescriptionFile.class,
85-
File.class,
86-
File.class,
87-
ClassLoader.class
88-
);
89-
constructor.setAccessible(true);
90-
91-
final ClassLoader simplixCoreClassLoader = Bukkit
92-
.getPluginManager()
93-
.getPlugin("SimplixCore")
94-
.getClass()
95-
.getClassLoader();
96-
97-
final Method loadClass0 = simplixCoreClassLoader
98-
.getClass()
99-
.getDeclaredMethod(
100-
"loadClass0",
101-
String.class,
102-
boolean.class,
103-
boolean.class,
104-
boolean.class);
105-
106-
ClassLoader parentLoader = new URLClassLoader(new URL[]{
107-
}) {
108-
@Override
109-
public Class<?> loadClass(String name) throws ClassNotFoundException {
110-
try {
111-
return (Class<?>) loadClass0.invoke(
112-
plugin.getPluginLoader(),
113-
name,
114-
false,
115-
true,
116-
false);
117-
} catch (Exception exception) {
118-
throw new ClassNotFoundException(name);
81+
if (javaVersion.olderThen(Version.parse("16.0.0"))) {
82+
// Spigot 1.16 or newer using old reflection
83+
Class<?> classLoaderClass = Class.forName("org.bukkit.plugin.java.PluginClassLoader");
84+
Constructor<?> constructor = classLoaderClass.getDeclaredConstructor(
85+
JavaPluginLoader.class,
86+
ClassLoader.class,
87+
PluginDescriptionFile.class,
88+
File.class,
89+
File.class,
90+
ClassLoader.class
91+
);
92+
constructor.setAccessible(true);
93+
pluginClassloader = constructor.newInstance(
94+
plugin.getPluginLoader(),
95+
plugin.getClass().getClassLoader().getParent(),
96+
pluginDescriptionFile,
97+
plugin.getDataFolder(),
98+
file,
99+
null
100+
);
101+
out = (ClassLoader) pluginClassloader;
102+
} else {
103+
// Spigot 1.16 newer - Compatible with Java16
104+
Class<?> classLoaderClass = Class.forName("org.bukkit.plugin.java.PluginClassLoader");
105+
Constructor<?> constructor = classLoaderClass.getDeclaredConstructor(
106+
JavaPluginLoader.class,
107+
ClassLoader.class,
108+
PluginDescriptionFile.class,
109+
File.class,
110+
File.class,
111+
ClassLoader.class
112+
);
113+
constructor.setAccessible(true);
114+
115+
final ClassLoader simplixCoreClassLoader = Bukkit
116+
.getPluginManager()
117+
.getPlugin("SimplixCore")
118+
.getClass()
119+
.getClassLoader();
120+
121+
final Method loadClass0 = simplixCoreClassLoader
122+
.getClass()
123+
.getDeclaredMethod(
124+
"loadClass0",
125+
String.class,
126+
boolean.class,
127+
boolean.class,
128+
boolean.class);
129+
130+
ClassLoader parentLoader = new URLClassLoader(new URL[]{
131+
}) {
132+
@Override
133+
public Class<?> loadClass(String name) throws ClassNotFoundException {
134+
try {
135+
return (Class<?>) loadClass0.invoke(
136+
plugin.getPluginLoader(),
137+
name,
138+
false,
139+
true,
140+
false);
141+
} catch (Exception exception) {
142+
throw new ClassNotFoundException(name);
143+
}
119144
}
120-
}
121-
};
122-
123-
final SimplixClassLoader simplixClassLoader = new SimplixClassLoader(
124-
new URL[0],
125-
parentLoader);
126-
127-
pluginClassloader = constructor.newInstance(
128-
plugin.getPluginLoader(),
129-
plugin.getClass().getClassLoader().getParent(),
130-
pluginDescriptionFile,
131-
plugin.getDataFolder(),
132-
file,
133-
simplixClassLoader
134-
);
135-
out = simplixClassLoader;
136-
cachedResult = simplixClassLoader;
145+
};
146+
147+
final SimplixClassLoader simplixClassLoader = new SimplixClassLoader(
148+
new URL[0],
149+
parentLoader);
150+
151+
pluginClassloader = constructor.newInstance(
152+
plugin.getPluginLoader(),
153+
plugin.getClass().getClassLoader().getParent(),
154+
pluginDescriptionFile,
155+
plugin.getDataFolder(),
156+
file,
157+
simplixClassLoader
158+
);
159+
out = simplixClassLoader;
160+
cachedResult = simplixClassLoader;
161+
}
137162
}
138163

139164
Field loadersField = JavaPluginLoader.class.getDeclaredField("loaders");
@@ -182,4 +207,4 @@ private void injectFakeClass(@NonNull File file) {
182207
}
183208
}
184209

185-
}
210+
}

0 commit comments

Comments
 (0)