From 897ff819150691c82ea70b11dd18759e08594d54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20St=C3=A4ding?= Date: Thu, 4 Jan 2024 17:04:25 +0100 Subject: [PATCH] Clean up JavaPluginLoader and VelocityPluginModule (#1140) --- .../plugin/loader/java/JavaPluginLoader.java | 16 +++++++++------- .../plugin/loader/java/VelocityPluginModule.java | 7 ++----- 2 files changed, 11 insertions(+), 12 deletions(-) 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 5674794ef..d783f476e 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 @@ -63,7 +63,7 @@ public class JavaPluginLoader implements PluginLoader { public PluginDescription loadCandidate(Path source) throws Exception { Optional serialized = getSerializedPluginInfo(source); - if (!serialized.isPresent()) { + if (serialized.isEmpty()) { throw new InvalidPluginException("Did not find a valid velocity-plugin.json."); } @@ -81,19 +81,21 @@ public class JavaPluginLoader implements PluginLoader { throw new IllegalArgumentException("Description provided isn't of the Java plugin loader"); } - URL pluginJarUrl = candidate.getSource().get().toUri().toURL(); + URL pluginJarUrl = candidate.getSource().orElseThrow( + () -> new InvalidPluginException("Description provided does not have a source path") + ).toUri().toURL(); PluginClassLoader loader = AccessController.doPrivileged( (PrivilegedAction) () -> new PluginClassLoader(new URL[]{pluginJarUrl})); loader.addToClassloaders(); JavaVelocityPluginDescriptionCandidate candidateInst = (JavaVelocityPluginDescriptionCandidate) candidate; - Class mainClass = loader.loadClass(candidateInst.getMainClass()); + Class mainClass = loader.loadClass(candidateInst.getMainClass()); return createDescription(candidateInst, mainClass); } @Override - public Module createModule(PluginContainer container) throws Exception { + public Module createModule(PluginContainer container) { PluginDescription description = container.getDescription(); if (!(description instanceof JavaVelocityPluginDescription)) { throw new IllegalArgumentException("Description provided isn't of the Java plugin loader"); @@ -102,11 +104,11 @@ public class JavaPluginLoader implements PluginLoader { JavaVelocityPluginDescription javaDescription = (JavaVelocityPluginDescription) description; Optional source = javaDescription.getSource(); - if (!source.isPresent()) { + if (source.isEmpty()) { throw new IllegalArgumentException("No path in plugin description"); } - return new VelocityPluginModule(server, javaDescription, container, baseDirectory); + return new VelocityPluginModule(javaDescription, container, baseDirectory); } @Override @@ -184,7 +186,7 @@ public class JavaPluginLoader implements PluginLoader { private VelocityPluginDescription createDescription( JavaVelocityPluginDescriptionCandidate description, - Class mainClass) { + Class mainClass) { return new JavaVelocityPluginDescription( description.getId(), description.getName().orElse(null), diff --git a/proxy/src/main/java/com/velocitypowered/proxy/plugin/loader/java/VelocityPluginModule.java b/proxy/src/main/java/com/velocitypowered/proxy/plugin/loader/java/VelocityPluginModule.java index a6331dd48..6ca2a9d30 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/plugin/loader/java/VelocityPluginModule.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/plugin/loader/java/VelocityPluginModule.java @@ -23,7 +23,6 @@ import com.google.inject.Scopes; import com.velocitypowered.api.plugin.PluginContainer; import com.velocitypowered.api.plugin.PluginDescription; import com.velocitypowered.api.plugin.annotation.DataDirectory; -import com.velocitypowered.api.proxy.ProxyServer; import java.nio.file.Path; import java.util.concurrent.ExecutorService; import net.kyori.adventure.text.logger.slf4j.ComponentLogger; @@ -32,14 +31,12 @@ import org.slf4j.LoggerFactory; class VelocityPluginModule implements Module { - private final ProxyServer server; private final JavaVelocityPluginDescription description; private final PluginContainer pluginContainer; private final Path basePluginPath; - VelocityPluginModule(ProxyServer server, JavaVelocityPluginDescription description, - PluginContainer pluginContainer, Path basePluginPath) { - this.server = server; + VelocityPluginModule(JavaVelocityPluginDescription description, PluginContainer pluginContainer, + Path basePluginPath) { this.description = description; this.pluginContainer = pluginContainer; this.basePluginPath = basePluginPath;