Skip to content

Commit

Permalink
Fixed loading of jars not using the plugin's ClassLoader causing issu…
Browse files Browse the repository at this point in the history
…es when these jars try to load classes
  • Loading branch information
OmerBenGera committed Aug 20, 2022
1 parent 38f28ee commit 038e820
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 12 deletions.
@@ -1,6 +1,7 @@
package com.bgsoftware.superiorskyblock;

import com.bgsoftware.common.mappings.MappingsChecker;
import com.bgsoftware.common.remaps.TestRemaps;
import com.bgsoftware.common.updater.Updater;
import com.bgsoftware.superiorskyblock.api.SuperiorSkyblock;
import com.bgsoftware.superiorskyblock.api.SuperiorSkyblockAPI;
Expand Down Expand Up @@ -69,7 +70,6 @@
import com.bgsoftware.superiorskyblock.nms.NMSPlayers;
import com.bgsoftware.superiorskyblock.nms.NMSTags;
import com.bgsoftware.superiorskyblock.nms.NMSWorld;
import com.bgsoftware.common.remaps.TestRemaps;
import com.bgsoftware.superiorskyblock.player.PlayersManagerImpl;
import com.bgsoftware.superiorskyblock.player.container.DefaultPlayersContainer;
import com.bgsoftware.superiorskyblock.service.ServicesHandler;
Expand Down Expand Up @@ -379,6 +379,10 @@ public Updater getUpdater() {
return updater;
}

public ClassLoader getPluginClassLoader() {
return super.getClassLoader();
}

private boolean loadNMSAdapter() {
String version = getServer().getClass().getPackage().getName().split("\\.")[3];
try {
Expand Down Expand Up @@ -460,7 +464,7 @@ private void loadGeneratorFromFile() {
if (generatorsFilesList != null) {
for (File file : generatorsFilesList) {
//noinspection deprecation
Class<?> generatorClass = JarFiles.getClass(file.toURL(), ChunkGenerator.class);
Class<?> generatorClass = JarFiles.getClass(file.toURL(), ChunkGenerator.class, getClassLoader());
if (generatorClass != null) {
for (Constructor<?> constructor : generatorClass.getConstructors()) {
if (constructor.getParameterCount() == 0) {
Expand Down
Expand Up @@ -183,7 +183,7 @@ private void loadCommands() {

try {
//noinspection deprecation
Class<?> commandClass = JarFiles.getClass(file.toURL(), SuperiorCommand.class);
Class<?> commandClass = JarFiles.getClass(file.toURL(), SuperiorCommand.class, plugin.getPluginClassLoader());

if (commandClass == null)
continue;
Expand Down
Expand Up @@ -12,11 +12,6 @@ private JarFiles() {

}

@Nullable
public static Class<?> getClass(URL jar, Class<?> clazz) {
return getClass(jar, clazz, clazz.getClassLoader());
}

@Nullable
public static Class<?> getClass(URL jar, Class<?> clazz, ClassLoader classLoader) {
try (URLClassLoader cl = new URLClassLoader(new URL[]{jar}, classLoader); JarInputStream jis = new JarInputStream(jar.openStream())) {
Expand Down
Expand Up @@ -445,7 +445,8 @@ public Mission<?> loadMission(String missionName, File missionsFolder, Configura

if (mission == null) {
File missionJar = new File(missionsFolder, missionSection.getString("mission-file") + ".jar");
Class<?> missionClass = Objects.requireNonNull(JarFiles.getClass(missionJar.toURL(), Mission.class),
Class<?> missionClass = Objects.requireNonNull(JarFiles.getClass(missionJar.toURL(), Mission.class,
plugin.getPluginClassLoader()),
"The mission file " + missionJar.getName() + " is not valid.");

boolean islandMission = missionSection.getBoolean("island", false);
Expand Down
Expand Up @@ -25,8 +25,8 @@ public class ModuleClassLoader extends URLClassLoader {
private final Manifest manifest;
private final URL url;

public ModuleClassLoader(File file) throws IOException {
super(new URL[]{file.toURI().toURL()}, PluginModule.class.getClassLoader());
public ModuleClassLoader(File file, ClassLoader pluginClassLoader) throws IOException {
super(new URL[]{file.toURI().toURL()}, pluginClassLoader);

this.jar = new JarFile(file);
this.manifest = jar.getManifest();
Expand Down
Expand Up @@ -64,7 +64,7 @@ public PluginModule registerModule(File moduleFile) throws IOException, Reflecti

String moduleName = moduleFile.getName().replace(".jar", "");

ModuleClassLoader moduleClassLoader = new ModuleClassLoader(moduleFile);
ModuleClassLoader moduleClassLoader = new ModuleClassLoader(moduleFile, plugin.getPluginClassLoader());

//noinspection deprecation
Class<?> moduleClass = JarFiles.getClass(moduleFile.toURL(), PluginModule.class, moduleClassLoader);
Expand Down

0 comments on commit 038e820

Please sign in to comment.