3
0
Mirror von https://github.com/GeyserMC/Geyser.git synchronisiert 2024-12-27 00:23:03 +01:00

Fix loading multiple extensions (Closes #2826)

Dieser Commit ist enthalten in:
RednedEpic 2022-04-24 14:37:44 -05:00
Ursprung 735697b553
Commit 7c8bf330a9
2 geänderte Dateien mit 14 neuen und 13 gelöschten Zeilen

Datei anzeigen

@ -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;
}
} }

Datei anzeigen

@ -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) {