Skip to content

Commit

Permalink
Added more indicative errors when modules or missions fail to load
Browse files Browse the repository at this point in the history
  • Loading branch information
OmerBenGera committed Dec 17, 2022
1 parent 06b12b4 commit cc36496
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 20 deletions.
Expand Up @@ -480,7 +480,7 @@ private void loadGeneratorFromFile() {
if (generatorsFilesList != null) {
for (File file : generatorsFilesList) {
//noinspection deprecation
Class<?> generatorClass = JarFiles.getClass(file.toURL(), ChunkGenerator.class, getClassLoader());
Class<?> generatorClass = JarFiles.getClass(file.toURL(), ChunkGenerator.class, getClassLoader()).getRight();
if (generatorClass != null) {
for (Constructor<?> constructor : generatorClass.getConstructors()) {
if (constructor.getParameterCount() == 0) {
Expand Down
Expand Up @@ -184,7 +184,7 @@ private void loadCommands() {

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

if (commandClass == null)
continue;
Expand Down
90 changes: 90 additions & 0 deletions src/main/java/com/bgsoftware/superiorskyblock/core/Either.java
@@ -0,0 +1,90 @@
package com.bgsoftware.superiorskyblock.core;

import javax.annotation.Nullable;
import java.util.function.Consumer;

public abstract class Either<R, L> {

public static <R, L> Either<R, L> right(R value) {
return new Right<>(value);
}

public static <R, L> Either<R, L> left(L value) {
return new Left<>(value);
}

public abstract Either<R, L> ifRight(Consumer<R> consumer);

public abstract Either<R, L> ifLeft(Consumer<L> consumer);

@Nullable
public abstract R getRight();

@Nullable
public abstract L getLeft();

private static class Right<R, L> extends Either<R, L> {

private final R value;

Right(R value) {
this.value = value;
}

@Override
public Either<R, L> ifRight(Consumer<R> consumer) {
consumer.accept(this.value);
return this;
}

@Override
public Either<R, L> ifLeft(Consumer<L> consumer) {
// Do nothing
return this;
}

@Override
public R getRight() {
return this.value;
}

@Override
public L getLeft() {
return null;
}

}

private static class Left<R, L> extends Either<R, L> {

private final L value;

Left(L value) {
this.value = value;
}

@Override
public Either<R, L> ifRight(Consumer<R> consumer) {
// Do nothing
return this;
}

@Override
public Either<R, L> ifLeft(Consumer<L> consumer) {
consumer.accept(this.value);
return this;
}

@Override
public R getRight() {
return null;
}

@Override
public L getLeft() {
return this.value;
}

}

}
@@ -1,6 +1,7 @@
package com.bgsoftware.superiorskyblock.core.io;

import javax.annotation.Nullable;
import com.bgsoftware.superiorskyblock.core.Either;

import java.net.URL;
import java.net.URLClassLoader;
import java.util.jar.JarEntry;
Expand All @@ -12,8 +13,7 @@ private JarFiles() {

}

@Nullable
public static Class<?> getClass(URL jar, Class<?> clazz, ClassLoader classLoader) {
public static Either<Class<?>, Throwable> getClass(URL jar, Class<?> clazz, ClassLoader classLoader) {
try (URLClassLoader cl = new URLClassLoader(new URL[]{jar}, classLoader); JarInputStream jis = new JarInputStream(jar.openStream())) {
JarEntry jarEntry;
while ((jarEntry = jis.getNextJarEntry()) != null) {
Expand All @@ -29,13 +29,14 @@ public static Class<?> getClass(URL jar, Class<?> clazz, ClassLoader classLoader
Class<?> c = cl.loadClass(clazzName);

if (clazz.isAssignableFrom(c)) {
return c;
return Either.right(c);
}
}
} catch (Throwable ignored) {
} catch (Throwable error) {
return Either.left(error);
}

return null;
return Either.right(null);
}

}
Expand Up @@ -7,6 +7,7 @@
import com.bgsoftware.superiorskyblock.api.missions.Mission;
import com.bgsoftware.superiorskyblock.api.missions.MissionCategory;
import com.bgsoftware.superiorskyblock.api.wrappers.SuperiorPlayer;
import com.bgsoftware.superiorskyblock.core.Either;
import com.bgsoftware.superiorskyblock.core.Manager;
import com.bgsoftware.superiorskyblock.core.events.EventResult;
import com.bgsoftware.superiorskyblock.core.events.EventsBus;
Expand Down Expand Up @@ -442,9 +443,17 @@ 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,
plugin.getPluginClassLoader()),
"The mission file " + missionJar.getName() + " is not valid.");

Either<Class<?>, Throwable> missionClassLookup = JarFiles.getClass(missionJar.toURL(), Mission.class,
plugin.getPluginClassLoader());

if (missionClassLookup.getLeft() != null)
throw new RuntimeException("An unexpected error occurred while reading " + missionJar.getName() + ".", missionClassLookup.getLeft());

Class<?> missionClass = missionClassLookup.getRight();

if (missionClass == null)
throw new RuntimeException("The mission file " + missionJar.getName() + " is not valid.");

boolean islandMission = missionSection.getBoolean("island", false);
List<String> requiredMissions = missionSection.getStringList("required-missions");
Expand Down
Expand Up @@ -5,6 +5,7 @@
import com.bgsoftware.superiorskyblock.api.handlers.ModulesManager;
import com.bgsoftware.superiorskyblock.api.modules.ModuleLoadTime;
import com.bgsoftware.superiorskyblock.api.modules.PluginModule;
import com.bgsoftware.superiorskyblock.core.Either;
import com.bgsoftware.superiorskyblock.core.Manager;
import com.bgsoftware.superiorskyblock.core.io.JarFiles;
import com.bgsoftware.superiorskyblock.core.logging.Log;
Expand Down Expand Up @@ -55,21 +56,20 @@ public void registerModule(PluginModule pluginModule) {

@Override
public PluginModule registerModule(File moduleFile) throws IOException, ReflectiveOperationException {
if (!moduleFile.exists())
throw new IllegalArgumentException("The given file does not exist.");
Preconditions.checkArgument(moduleFile.exists(), "The file " + moduleFile.getName() + " does not exist.");
Preconditions.checkArgument(moduleFile.getName().endsWith(".jar"), "The file " + moduleFile.getName() + " is not a valid jar file.");

if (!moduleFile.getName().endsWith(".jar"))
throw new IllegalArgumentException("The given file is not a valid jar file.");
ModuleClassLoader moduleClassLoader = new ModuleClassLoader(moduleFile, plugin.getPluginClassLoader());

String moduleName = moduleFile.getName().replace(".jar", "");
Either<Class<?>, Throwable> moduleClassLookup = JarFiles.getClass(moduleFile.toURL(), PluginModule.class, moduleClassLoader);

ModuleClassLoader moduleClassLoader = new ModuleClassLoader(moduleFile, plugin.getPluginClassLoader());
if (moduleClassLookup.getLeft() != null)
throw new RuntimeException("An error occurred while reading " + moduleFile.getName(), moduleClassLookup.getLeft());

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

if (moduleClass == null)
throw new IllegalArgumentException("The file " + moduleName + " is not a valid module.");
throw new RuntimeException("The module file " + moduleFile.getName() + " is not valid.");

PluginModule pluginModule = createInstance(moduleClass);
pluginModule.initModuleLoader(moduleFile, moduleClassLoader);
Expand Down

0 comments on commit cc36496

Please sign in to comment.