From a6d4ad695037308a5522276eca52bd33ee651a33 Mon Sep 17 00:00:00 2001 From: Andrew Steinborn Date: Sun, 16 May 2021 00:33:03 -0400 Subject: [PATCH] Some classloader/dependency versioning fix ups --- .../velocitypowered/proxy/VelocityServer.java | 2 +- .../proxy/plugin/PluginClassLoader.java | 18 +++++++++++------- .../proxy/plugin/VelocityPluginManager.java | 2 +- .../plugin/loader/java/JavaPluginLoader.java | 2 +- 4 files changed, 14 insertions(+), 10 deletions(-) diff --git a/proxy/src/main/java/com/velocitypowered/proxy/VelocityServer.java b/proxy/src/main/java/com/velocitypowered/proxy/VelocityServer.java index 6090c11f7..fc22b7a22 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/VelocityServer.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/VelocityServer.java @@ -343,7 +343,7 @@ public class VelocityServer implements ProxyServer, ForwardingAudience { } } - logger.info("Loaded {} plugins", pluginManager.plugins().size()); + logger.info("Loaded {} plugin(s)", pluginManager.plugins().size() - 1); } public Bootstrap createBootstrap(@Nullable EventLoopGroup group, SocketAddress target) { diff --git a/proxy/src/main/java/com/velocitypowered/proxy/plugin/PluginClassLoader.java b/proxy/src/main/java/com/velocitypowered/proxy/plugin/PluginClassLoader.java index 2124c4c27..b5928502a 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/plugin/PluginClassLoader.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/plugin/PluginClassLoader.java @@ -36,8 +36,8 @@ public class PluginClassLoader extends URLClassLoader { private final PluginDescription description; - public PluginClassLoader(URL[] urls, PluginDescription description) { - super(urls); + public PluginClassLoader(URL[] urls, ClassLoader parent, PluginDescription description) { + super(urls, parent); this.description = description; } @@ -60,14 +60,18 @@ public class PluginClassLoader extends URLClassLoader { } @Override - protected Class loadClass(String name, boolean resolve) throws ClassNotFoundException { - return loadClass0(name, resolve, true); + protected Class findClass(String name) throws ClassNotFoundException { + return findClass0(name, true); } - private Class loadClass0(String name, boolean resolve, boolean checkOther) + private Class findClass0(String name, boolean checkOther) throws ClassNotFoundException { + if (name.startsWith("com.velocitypowered")) { + throw new ClassNotFoundException(); + } + try { - return super.loadClass(name, resolve); + return super.findClass(name); } catch (ClassNotFoundException ignored) { // Ignored: we'll try others } @@ -76,7 +80,7 @@ public class PluginClassLoader extends URLClassLoader { for (PluginClassLoader loader : loaders) { if (loader != this) { try { - return loader.loadClass0(name, resolve, false); + return loader.findClass0(name, false); } catch (ClassNotFoundException ignored) { // We're trying others, safe to ignore } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/plugin/VelocityPluginManager.java b/proxy/src/main/java/com/velocitypowered/proxy/plugin/VelocityPluginManager.java index 5a96e9d17..e37369d7c 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/plugin/VelocityPluginManager.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/plugin/VelocityPluginManager.java @@ -136,7 +136,7 @@ public class VelocityPluginManager implements PluginManager { Semver dependencyCandidateVersion = new Semver( dependencyContainer.description().version(), SemverType.NPM); if (!dependencyCandidateVersion.satisfies(requiredRange)) { - if (dependency.optional()) { + if (!dependency.optional()) { logger.error( "Can't load plugin {} due to incompatible dependency {} {} (you have {})", candidate.id(), dependency.id(), requiredRange, diff --git a/proxy/src/main/java/com/velocitypowered/proxy/plugin/loader/java/JavaPluginLoader.java b/proxy/src/main/java/com/velocitypowered/proxy/plugin/loader/java/JavaPluginLoader.java index 7e9fcd435..9fe592a1d 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/plugin/loader/java/JavaPluginLoader.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/plugin/loader/java/JavaPluginLoader.java @@ -92,7 +92,7 @@ public class JavaPluginLoader implements PluginLoader { PluginClassLoader loader = this.classLoaders.computeIfAbsent(pluginJarUri, (uri) -> { PluginClassLoader classLoader = AccessController.doPrivileged( (PrivilegedAction) () -> new PluginClassLoader(new URL[]{pluginJarUrl}, - source)); + JavaPluginLoader.class.getClassLoader(), source)); classLoader.addToClassloaders(); return classLoader; });