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 { public class GeyserExtensionClassLoader extends URLClassLoader {
private final GeyserExtensionLoader loader; private final GeyserExtensionLoader loader;
private final Map<String, Class<?>> classes = new HashMap<>(); 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); super(new URL[] { path.toUri().toURL() }, parent);
this.loader = loader; this.loader = loader;
}
public Extension load(ExtensionDescription description) throws InvalidExtensionException {
try { try {
Class<?> jarClass; Class<?> jarClass;
try { try {
@ -57,10 +58,10 @@ public class GeyserExtensionClassLoader extends URLClassLoader {
try { try {
extensionClass = jarClass.asSubclass(Extension.class); extensionClass = jarClass.asSubclass(Extension.class);
} catch (ClassCastException ex) { } 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) { } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException ex) {
throw new InvalidExtensionException("No public constructor", ex); throw new InvalidExtensionException("No public constructor", ex);
} catch (InstantiationException ex) { } catch (InstantiationException ex) {
@ -77,6 +78,7 @@ public class GeyserExtensionClassLoader extends URLClassLoader {
if (name.startsWith("org.geysermc.geyser.") || name.startsWith("net.minecraft.")) { if (name.startsWith("org.geysermc.geyser.") || name.startsWith("net.minecraft.")) {
throw new ClassNotFoundException(name); throw new ClassNotFoundException(name);
} }
Class<?> result = this.classes.get(name); Class<?> result = this.classes.get(name);
if (result == null) { if (result == null) {
if (checkGlobal) { if (checkGlobal) {
@ -94,8 +96,4 @@ public class GeyserExtensionClassLoader extends URLClassLoader {
} }
return result; return result;
} }
public Extension extension() {
return this.extension;
}
} }

View file

@ -78,13 +78,15 @@ public class GeyserExtensionLoader extends ExtensionLoader {
final GeyserExtensionClassLoader loader; final GeyserExtensionClassLoader loader;
try { try {
loader = new GeyserExtensionClassLoader(this, getClass().getClassLoader(), description, path); loader = new GeyserExtensionClassLoader(this, getClass().getClassLoader(), path);
} catch (Throwable e) { } catch (Throwable e) {
throw new InvalidExtensionException(e); throw new InvalidExtensionException(e);
} }
this.classLoaders.put(description.name(), loader); 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) { 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); Class<?> clazz = this.classes.get(name);
try { try {
for (GeyserExtensionClassLoader loader : this.classLoaders.values()) { for (GeyserExtensionClassLoader loader : this.classLoaders.values()) {
try { if (clazz != null) {
clazz = loader.findClass(name,false); continue;
} catch(NullPointerException ignored) {
} }
clazz = loader.findClass(name, false);
} }
return clazz; return clazz;
} catch (NullPointerException s) { } catch (NullPointerException s) {