Fix loading multiple extensions (Closes #2826)

This commit is contained in:
RednedEpic 2022-04-24 14:37:44 -05:00
parent 735697b553
commit 7c8bf330a9
2 changed files with 14 additions and 13 deletions

View file

@ -39,12 +39,13 @@ import java.util.Map;
public class GeyserExtensionClassLoader extends URLClassLoader {
private final GeyserExtensionLoader loader;
private final Map<String, Class<?>> classes = new HashMap<>();
private final Extension extension;
public GeyserExtensionClassLoader(GeyserExtensionLoader loader, ClassLoader parent, ExtensionDescription description, Path path) throws InvalidExtensionException, MalformedURLException {
public GeyserExtensionClassLoader(GeyserExtensionLoader loader, ClassLoader parent, Path path) throws MalformedURLException {
super(new URL[] { path.toUri().toURL() }, parent);
this.loader = loader;
}
public Extension load(ExtensionDescription description) throws InvalidExtensionException {
try {
Class<?> jarClass;
try {
@ -57,10 +58,10 @@ public class GeyserExtensionClassLoader extends URLClassLoader {
try {
extensionClass = jarClass.asSubclass(Extension.class);
} catch (ClassCastException ex) {
throw new InvalidExtensionException("Main class " + description.main() + " should extends GeyserExtension, but extends " + jarClass.getSuperclass().getSimpleName(), ex);
throw new InvalidExtensionException("Main class " + description.main() + " should implement Extension, but extends " + jarClass.getSuperclass().getSimpleName(), ex);
}
this.extension = extensionClass.getConstructor().newInstance();
return extensionClass.getConstructor().newInstance();
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException ex) {
throw new InvalidExtensionException("No public constructor", ex);
} catch (InstantiationException ex) {
@ -77,6 +78,7 @@ public class GeyserExtensionClassLoader extends URLClassLoader {
if (name.startsWith("org.geysermc.geyser.") || name.startsWith("net.minecraft.")) {
throw new ClassNotFoundException(name);
}
Class<?> result = this.classes.get(name);
if (result == null) {
if (checkGlobal) {
@ -94,8 +96,4 @@ public class GeyserExtensionClassLoader extends URLClassLoader {
}
return result;
}
public Extension extension() {
return this.extension;
}
}

View file

@ -78,13 +78,15 @@ public class GeyserExtensionLoader extends ExtensionLoader {
final GeyserExtensionClassLoader loader;
try {
loader = new GeyserExtensionClassLoader(this, getClass().getClassLoader(), description, path);
loader = new GeyserExtensionClassLoader(this, getClass().getClassLoader(), path);
} catch (Throwable e) {
throw new InvalidExtensionException(e);
}
this.classLoaders.put(description.name(), loader);
return this.setup(loader.extension(), description, dataFolder, new GeyserExtensionEventBus(GeyserImpl.getInstance().eventBus(), loader.extension()));
final Extension extension = loader.load(description);
return this.setup(extension, description, dataFolder, new GeyserExtensionEventBus(GeyserImpl.getInstance().eventBus(), extension));
}
private GeyserExtensionContainer setup(Extension extension, GeyserExtensionDescription description, Path dataFolder, ExtensionEventBus eventBus) {
@ -110,10 +112,11 @@ public class GeyserExtensionLoader extends ExtensionLoader {
Class<?> clazz = this.classes.get(name);
try {
for (GeyserExtensionClassLoader loader : this.classLoaders.values()) {
try {
clazz = loader.findClass(name,false);
} catch(NullPointerException ignored) {
if (clazz != null) {
continue;
}
clazz = loader.findClass(name, false);
}
return clazz;
} catch (NullPointerException s) {