diff --git a/api/src/main/java/com/velocitypowered/api/plugin/PluginContainer.java b/api/src/main/java/com/velocitypowered/api/plugin/PluginContainer.java index b5fe9ab44..c8f03f31e 100644 --- a/api/src/main/java/com/velocitypowered/api/plugin/PluginContainer.java +++ b/api/src/main/java/com/velocitypowered/api/plugin/PluginContainer.java @@ -7,7 +7,7 @@ package com.velocitypowered.api.plugin; -import java.util.Optional; +import org.checkerframework.checker.nullness.qual.Nullable; /** * A wrapper around a plugin loaded by the proxy. @@ -26,7 +26,7 @@ public interface PluginContainer { * * @return the instance if available */ - default Optional instance() { - return Optional.empty(); + default @Nullable Object instance() { + return null; } } diff --git a/config/checkstyle/checkstyle.xml b/config/checkstyle/checkstyle.xml index f419cca1f..716deac59 100644 --- a/config/checkstyle/checkstyle.xml +++ b/config/checkstyle/checkstyle.xml @@ -39,13 +39,9 @@ - + + + diff --git a/proxy/src/main/java/com/velocitypowered/proxy/VelocityServer.java b/proxy/src/main/java/com/velocitypowered/proxy/VelocityServer.java index cf261c4fc..1545fc6fc 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/VelocityServer.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/VelocityServer.java @@ -78,7 +78,6 @@ import java.util.Collection; import java.util.Locale; import java.util.Map; import java.util.Objects; -import java.util.Optional; import java.util.ResourceBundle; import java.util.UUID; import java.util.concurrent.CompletableFuture; @@ -327,10 +326,10 @@ public class VelocityServer implements ProxyServer, ForwardingAudience { // Register the plugin main classes so that we can fire the proxy initialize event for (PluginContainer plugin : pluginManager.plugins()) { - Optional instance = plugin.instance(); - if (instance.isPresent()) { + Object instance = plugin.instance(); + if (instance != null) { try { - eventManager.registerInternally(plugin, instance.get()); + eventManager.registerInternally(plugin, instance); } catch (Exception e) { logger.error("Unable to register plugin listener for {}", MoreObjects.firstNonNull(plugin.description().name(), plugin.description().id()), e); diff --git a/proxy/src/main/java/com/velocitypowered/proxy/command/builtin/ServerCommand.java b/proxy/src/main/java/com/velocitypowered/proxy/command/builtin/ServerCommand.java index 553cd1df6..70f66699a 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/command/builtin/ServerCommand.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/command/builtin/ServerCommand.java @@ -29,7 +29,6 @@ import com.velocitypowered.api.proxy.connection.ServerConnection; import com.velocitypowered.api.proxy.server.RegisteredServer; import com.velocitypowered.api.proxy.server.ServerInfo; import java.util.List; -import java.util.Optional; import java.util.stream.Collectors; import java.util.stream.Stream; import net.kyori.adventure.identity.Identity; @@ -76,13 +75,16 @@ public class ServerCommand implements SimpleCommand { } private void outputServerInformation(Player executor) { - String currentServer = Optional.ofNullable(executor.connectedServer()) - .map(ServerConnection::serverInfo) - .map(ServerInfo::name).orElse(""); + String currentServerName = ""; + ServerConnection connectedTo = executor.connectedServer(); + if (connectedTo != null) { + currentServerName = connectedTo.serverInfo().name(); + } + executor.sendMessage(Identity.nil(), Component.translatable( "velocity.command.server-current-server", NamedTextColor.YELLOW, - Component.text(currentServer))); + Component.text(currentServerName))); List servers = BuiltinCommandUtil.sortedServerList(server); if (servers.size() > MAX_SERVERS_TO_LIST) { @@ -98,7 +100,7 @@ public class ServerCommand implements SimpleCommand { .append(Component.space()); for (int i = 0; i < servers.size(); i++) { RegisteredServer rs = servers.get(i); - serverListBuilder.append(formatServerComponent(currentServer, rs)); + serverListBuilder.append(formatServerComponent(currentServerName, rs)); if (i != servers.size() - 1) { serverListBuilder.append(Component.text(", ", NamedTextColor.GRAY)); } 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 20d6e2667..20c6d98ea 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/plugin/VelocityPluginManager.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/plugin/VelocityPluginManager.java @@ -49,7 +49,6 @@ import java.util.IdentityHashMap; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; -import java.util.Optional; import java.util.Set; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -69,8 +68,10 @@ public class VelocityPluginManager implements PluginManager { private void registerPlugin(PluginContainer plugin) { plugins.put(plugin.description().id(), plugin); - Optional instance = plugin.instance(); - instance.ifPresent(o -> pluginInstances.put(o, plugin)); + Object instance = plugin.instance(); + if (instance != null) { + pluginInstances.put(instance, plugin); + } } /** @@ -198,10 +199,7 @@ public class VelocityPluginManager implements PluginManager { throw new IllegalArgumentException("plugin is not loaded"); } - Optional optInstance = optContainer.instance(); - checkArgument(optInstance.isPresent(), "plugin has no instance"); - - ClassLoader pluginClassloader = optInstance.get().getClass().getClassLoader(); + ClassLoader pluginClassloader = plugin.getClass().getClassLoader(); if (pluginClassloader instanceof PluginClassLoader) { ((PluginClassLoader) pluginClassloader).addPath(path); } else { diff --git a/proxy/src/main/java/com/velocitypowered/proxy/plugin/loader/VelocityPluginContainer.java b/proxy/src/main/java/com/velocitypowered/proxy/plugin/loader/VelocityPluginContainer.java index 6d6e3e10f..483d57960 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/plugin/loader/VelocityPluginContainer.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/plugin/loader/VelocityPluginContainer.java @@ -19,8 +19,8 @@ package com.velocitypowered.proxy.plugin.loader; import com.velocitypowered.api.plugin.PluginContainer; import com.velocitypowered.api.plugin.PluginDescription; -import java.util.Optional; import org.checkerframework.checker.nullness.qual.MonotonicNonNull; +import org.checkerframework.checker.nullness.qual.Nullable; public class VelocityPluginContainer implements PluginContainer { @@ -37,8 +37,8 @@ public class VelocityPluginContainer implements PluginContainer { } @Override - public Optional instance() { - return Optional.ofNullable(instance); + public @Nullable Object instance() { + return instance; } public void setInstance(Object instance) { 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 4773cf44e..4bf3db055 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 @@ -41,10 +41,10 @@ import java.nio.file.Path; import java.security.AccessController; import java.security.PrivilegedAction; import java.util.HashSet; -import java.util.Optional; import java.util.Set; import java.util.jar.JarEntry; import java.util.jar.JarInputStream; +import org.checkerframework.checker.nullness.qual.Nullable; public class JavaPluginLoader implements PluginLoader { @@ -56,18 +56,16 @@ public class JavaPluginLoader implements PluginLoader { @Override public PluginDescription loadPluginDescription(Path source) throws Exception { - Optional serialized = getSerializedPluginInfo(source); - - if (serialized.isEmpty()) { + SerializedPluginDescription serialized = getSerializedPluginInfo(source); + if (serialized == null) { throw new InvalidPluginException("Did not find a valid velocity-plugin.json."); } - SerializedPluginDescription pd = serialized.get(); - if (!SerializedPluginDescription.ID_PATTERN.matcher(pd.getId()).matches()) { - throw new InvalidPluginException("Plugin ID '" + pd.getId() + "' is invalid."); + if (!SerializedPluginDescription.ID_PATTERN.matcher(serialized.getId()).matches()) { + throw new InvalidPluginException("Plugin ID '" + serialized.getId() + "' is invalid."); } - return createCandidateDescription(pd, source); + return createCandidateDescription(serialized, source); } @Override @@ -131,7 +129,7 @@ public class JavaPluginLoader implements PluginLoader { ((VelocityPluginContainer) container).setInstance(instance); } - private Optional getSerializedPluginInfo(Path source) + private @Nullable SerializedPluginDescription getSerializedPluginInfo(Path source) throws Exception { boolean foundBungeeBukkitPluginFile = false; try (JarInputStream in = new JarInputStream( @@ -140,8 +138,8 @@ public class JavaPluginLoader implements PluginLoader { while ((entry = in.getNextJarEntry()) != null) { if (entry.getName().equals("velocity-plugin.json")) { try (Reader pluginInfoReader = new InputStreamReader(in, StandardCharsets.UTF_8)) { - return Optional.of(VelocityServer.GENERAL_GSON.fromJson(pluginInfoReader, - SerializedPluginDescription.class)); + return VelocityServer.GENERAL_GSON.fromJson(pluginInfoReader, + SerializedPluginDescription.class); } } @@ -156,7 +154,7 @@ public class JavaPluginLoader implements PluginLoader { + "plugins."); } - return Optional.empty(); + return null; } } diff --git a/proxy/src/test/java/com/velocitypowered/proxy/testutil/FakePluginManager.java b/proxy/src/test/java/com/velocitypowered/proxy/testutil/FakePluginManager.java index 543357c2e..881b59f55 100644 --- a/proxy/src/test/java/com/velocitypowered/proxy/testutil/FakePluginManager.java +++ b/proxy/src/test/java/com/velocitypowered/proxy/testutil/FakePluginManager.java @@ -23,7 +23,6 @@ import com.velocitypowered.api.plugin.PluginDescription; import com.velocitypowered.api.plugin.PluginManager; import java.nio.file.Path; import java.util.Collection; -import java.util.Optional; import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.Nullable; @@ -89,8 +88,8 @@ public class FakePluginManager implements PluginManager { } @Override - public Optional instance() { - return Optional.of(instance); + public Object instance() { + return instance; } } }