diff --git a/proxy/src/main/java/com/velocitypowered/proxy/VelocityServer.java b/proxy/src/main/java/com/velocitypowered/proxy/VelocityServer.java index 721541bae..e7c23283b 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/VelocityServer.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/VelocityServer.java @@ -104,18 +104,11 @@ public class VelocityServer implements ProxyServer { } public KeyPair getServerKeyPair() { - if (serverKeyPair == null) { - throw new AssertionError(); - } - return serverKeyPair; + return ensureInitialized(serverKeyPair); } public VelocityConfiguration getConfiguration() { - VelocityConfiguration cfg = this.configuration; - if (cfg == null) { - throw new IllegalStateException("Configuration not initialized!"); - } - return cfg; + return ensureInitialized(this.configuration); } @Override @@ -250,6 +243,12 @@ public class VelocityServer implements ProxyServer { return shutdown; } + /** + * Reloads the proxy's configuration. + * + * @return {@code true} if successful, {@code false} if we can't read the configuration + * @throws IOException if we can't read {@code velocity.toml} + */ public boolean reloadConfiguration() throws IOException { Path configPath = Paths.get("velocity.toml"); VelocityConfiguration newConfiguration = VelocityConfiguration.read(configPath); @@ -331,6 +330,11 @@ public class VelocityServer implements ProxyServer { return true; } + /** + * Shuts down the proxy. + * + * @param explicitExit whether the user explicitly shut down the proxy + */ public void shutdown(boolean explicitExit) { if (eventManager == null || pluginManager == null || cm == null || scheduler == null) { throw new AssertionError(); @@ -365,19 +369,25 @@ public class VelocityServer implements ProxyServer { } public NettyHttpClient getHttpClient() { - if (httpClient == null) { - throw new IllegalStateException("HTTP client not initialized"); - } - return httpClient; + return ensureInitialized(httpClient); } public Ratelimiter getIpAttemptLimiter() { - if (ipAttemptLimiter == null) { - throw new IllegalStateException("Ratelimiter not initialized"); - } - return ipAttemptLimiter; + return ensureInitialized(ipAttemptLimiter); } + private static T ensureInitialized(T o) { + if (o == null) { + throw new IllegalStateException("The proxy isn't fully initialized."); + } + return o; + } + + /** + * Attempts to register the {@code connection} with the proxy. + * @param connection the connection to register + * @return {@code true} if we registered the connection, {@code false} if not + */ public boolean registerConnection(ConnectedPlayer connection) { String lowerName = connection.getUsername().toLowerCase(Locale.US); if (connectionsByName.putIfAbsent(lowerName, connection) != null) { diff --git a/proxy/src/main/java/com/velocitypowered/proxy/config/VelocityConfiguration.java b/proxy/src/main/java/com/velocitypowered/proxy/config/VelocityConfiguration.java index f6ff81ecd..558d5a955 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/config/VelocityConfiguration.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/config/VelocityConfiguration.java @@ -266,6 +266,11 @@ public class VelocityConfiguration extends AnnotatedConfig implements ProxyConfi return motd; } + /** + * Returns the proxy's MOTD. + * + * @return the MOTD + */ public Component getMotdComponent() { if (motdAsComponent == null) { if (motd.startsWith("{")) { diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/BackendPlaySessionHandler.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/BackendPlaySessionHandler.java index 215587e54..4ac2cfc8a 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/BackendPlaySessionHandler.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/BackendPlaySessionHandler.java @@ -95,7 +95,8 @@ public class BackendPlaySessionHandler implements MinecraftSessionHandler { } if (PluginMessageUtil.isMcBrand(packet)) { - PluginMessage rewritten = PluginMessageUtil.rewriteMinecraftBrand(packet, server.getVersion()); + PluginMessage rewritten = PluginMessageUtil.rewriteMinecraftBrand(packet, + server.getVersion()); serverConn.getPlayer().getConnection().write(rewritten); return true; } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/VelocityServerConnection.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/VelocityServerConnection.java index 87fb29436..d2b973a46 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/VelocityServerConnection.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/VelocityServerConnection.java @@ -212,6 +212,9 @@ public class VelocityServerConnection implements MinecraftConnectionAssociation, return true; } + /** + * Indicates that we have completed the plugin process. + */ public void completeJoin() { if (!hasCompletedJoin) { hasCompletedJoin = true; diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ClientPlaySessionHandler.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ClientPlaySessionHandler.java index 8317a1575..393c367b6 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ClientPlaySessionHandler.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ClientPlaySessionHandler.java @@ -296,6 +296,11 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler { } } + /** + * Handles the {@code JoinGame} packet. This function is responsible for handling the client-side + * switching servers in Velocity. + * @param joinGame the join game packet + */ public void handleBackendJoinGame(JoinGame joinGame) { VelocityServerConnection serverConn = player.getConnectedServer(); if (serverConn == null) { @@ -387,6 +392,11 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler { return knownChannels; } + /** + * Handles additional tab complete for 1.12 and lower clients. + * + * @param response the tab complete response from the backend + */ public void handleTabCompleteResponse(TabCompleteResponse response) { if (outstandingTabComplete != null) { if (!outstandingTabComplete.isAssumeCommand() diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ConnectedPlayer.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ConnectedPlayer.java index 8e73c05ec..281448d02 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ConnectedPlayer.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ConnectedPlayer.java @@ -296,6 +296,11 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player { connectionInFlight = null; } + /** + * Handles unexpected disconnects. + * @param server the server we disconnected from + * @param throwable the exception + */ public void handleConnectionException(RegisteredServer server, Throwable throwable) { if (throwable == null) { throw new NullPointerException("throwable"); @@ -321,6 +326,11 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player { handleConnectionException(server, null, TextComponent.of(userMessage, TextColor.RED)); } + /** + * Handles unexpected disconnects. + * @param server the server we disconnected from + * @param disconnect the disconnect packet + */ public void handleConnectionException(RegisteredServer server, Disconnect disconnect) { Component disconnectReason = ComponentSerializers.JSON.deserialize(disconnect.getReason()); String plainTextReason = PASS_THRU_TRANSLATE.serialize(disconnectReason); @@ -382,6 +392,11 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player { } } + /** + * Finds another server to attempt to log into, if we were unexpectedly disconnected from the + * server. + * @return the next server to try + */ public Optional getNextServerToTry() { if (serversToTry == null) { String virtualHostStr = getVirtualHost().map(InetSocketAddress::getHostString).orElse(""); @@ -402,6 +417,11 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player { return server.getServer(toTryName); } + /** + * Sets the player's new connected server and clears the in-flight connection. + * + * @param serverConnection the new server connection + */ public void setConnectedServer(@Nullable VelocityServerConnection serverConnection) { this.connectedServer = serverConnection; this.tryIndex = 0; // reset since we got connected to a server diff --git a/proxy/src/main/java/com/velocitypowered/proxy/console/VelocityConsole.java b/proxy/src/main/java/com/velocitypowered/proxy/console/VelocityConsole.java index 1768830a5..cb620eaaf 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/console/VelocityConsole.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/console/VelocityConsole.java @@ -1,13 +1,12 @@ package com.velocitypowered.proxy.console; -import static com.velocitypowered.api.permission.PermissionFunction.*; +import static com.velocitypowered.api.permission.PermissionFunction.ALWAYS_TRUE; import com.velocitypowered.api.event.permission.PermissionsSetupEvent; import com.velocitypowered.api.permission.PermissionFunction; import com.velocitypowered.api.permission.Tristate; import com.velocitypowered.api.proxy.ConsoleCommandSource; import com.velocitypowered.proxy.VelocityServer; -import java.io.PrintWriter; import java.util.List; import net.kyori.text.Component; import net.kyori.text.TextComponent; @@ -44,11 +43,17 @@ public final class VelocityConsole extends SimpleTerminalConsole implements Cons return this.permissionFunction.getPermissionValue(permission); } + /** + * Sets up {@code System.out} and {@code System.err} to redirect to log4j. + */ public void setupStreams() { System.setOut(IoBuilder.forLogger(logger).setLevel(Level.INFO).buildPrintStream()); System.setErr(IoBuilder.forLogger(logger).setLevel(Level.ERROR).buildPrintStream()); } + /** + * Sets up permissions for the console. + */ public void setupPermissions() { PermissionsSetupEvent event = new PermissionsSetupEvent(this, s -> ALWAYS_TRUE); // we can safely block here, this is before any listeners fire diff --git a/proxy/src/main/java/com/velocitypowered/proxy/network/ConnectionManager.java b/proxy/src/main/java/com/velocitypowered/proxy/network/ConnectionManager.java index 110dd94fd..e6c72fb10 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/network/ConnectionManager.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/network/ConnectionManager.java @@ -36,6 +36,11 @@ public final class ConnectionManager { private final DnsAddressResolverGroup resolverGroup; + /** + * Initalizes the {@code ConnectionManager}. + * + * @param server a reference to the Velocity server + */ public ConnectionManager(VelocityServer server) { this.server = server; this.transportType = TransportType.bestType(); @@ -52,6 +57,11 @@ public final class ConnectionManager { Natives.compress.getLoadedVariant(), Natives.cipher.getLoadedVariant()); } + /** + * Binds a Minecraft listener to the specified {@code address}. + * + * @param address the address to bind to + */ public void bind(final InetSocketAddress address) { final ServerBootstrap bootstrap = new ServerBootstrap() .channel(this.transportType.serverSocketChannelClass) @@ -73,6 +83,12 @@ public final class ConnectionManager { }); } + /** + * Binds a GS4 listener to the specified {@code hostname} and {@code port}. + * + * @param hostname the hostname to bind to + * @param port the port to bind to + */ public void queryBind(final String hostname, final int port) { InetSocketAddress address = new InetSocketAddress(hostname, port); final Bootstrap bootstrap = new Bootstrap() @@ -92,6 +108,11 @@ public final class ConnectionManager { }); } + /** + * Creates a TCP {@link Bootstrap} using Velocity's event loops. + * + * @return a new {@link Bootstrap} + */ public Bootstrap createWorker() { return new Bootstrap() .channel(this.transportType.socketChannelClass) @@ -102,6 +123,11 @@ public final class ConnectionManager { .resolver(this.resolverGroup); } + /** + * Closes the specified {@code oldBind} endpoint. + * + * @param oldBind the endpoint to close + */ public void close(InetSocketAddress oldBind) { Channel serverChannel = endpoints.remove(oldBind); Preconditions.checkState(serverChannel != null, "Endpoint %s not registered", oldBind); @@ -109,6 +135,9 @@ public final class ConnectionManager { serverChannel.close().syncUninterruptibly(); } + /** + * Closes all endpoints. + */ public void shutdown() { for (final Channel endpoint : this.endpoints.values()) { try { diff --git a/proxy/src/main/java/com/velocitypowered/proxy/network/http/NettyHttpClient.java b/proxy/src/main/java/com/velocitypowered/proxy/network/http/NettyHttpClient.java index af012a817..4b667ae86 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/network/http/NettyHttpClient.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/network/http/NettyHttpClient.java @@ -27,6 +27,11 @@ public class NettyHttpClient { private final ChannelPoolMap poolMap; private final String userAgent; + /** + * Initializes the HTTP client. + * + * @param server the Velocity server + */ public NettyHttpClient(VelocityServer server) { this.userAgent = server.getVersion().getName() + "/" + server.getVersion().getVersion(); Bootstrap bootstrap = server.initializeGenericBootstrap(); diff --git a/proxy/src/main/java/com/velocitypowered/proxy/plugin/VelocityEventManager.java b/proxy/src/main/java/com/velocitypowered/proxy/plugin/VelocityEventManager.java index 68f103222..024d80a13 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/plugin/VelocityEventManager.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/plugin/VelocityEventManager.java @@ -44,6 +44,11 @@ public class VelocityEventManager implements EventManager { private final ExecutorService service; private final PluginManager pluginManager; + /** + * Initializes the Velocity event manager. + * + * @param pluginManager a reference to the Velocity plugin manager + */ public VelocityEventManager(PluginManager pluginManager) { // Expose the event executors to the plugins - required in order for the generated ASM classes // to work. 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 d41a8e72d..f23dfa66e 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/plugin/VelocityPluginManager.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/plugin/VelocityPluginManager.java @@ -46,6 +46,11 @@ public class VelocityPluginManager implements PluginManager { } } + /** + * Loads all plugins from the specified {@code directory}. + * @param directory the directory to load from + * @throws IOException if we could not open the directory + */ public void loadPlugins(Path directory) throws IOException { checkNotNull(directory, "directory"); checkArgument(directory.toFile().isDirectory(), "provided path isn't a directory"); diff --git a/proxy/src/main/java/com/velocitypowered/proxy/plugin/loader/VelocityPluginDescription.java b/proxy/src/main/java/com/velocitypowered/proxy/plugin/loader/VelocityPluginDescription.java index ac29f69f1..41f0d9151 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/plugin/loader/VelocityPluginDescription.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/plugin/loader/VelocityPluginDescription.java @@ -25,6 +25,17 @@ public class VelocityPluginDescription implements PluginDescription { private final Map dependencies; private final Path source; + /** + * Creates a new plugin description. + * @param id the ID + * @param name the name of the plugin + * @param version the plugin version + * @param description a description of the plugin + * @param url the website for the plugin + * @param authors the authors of this plugin + * @param dependencies the dependencies for this plugin + * @param source the original source for the plugin + */ public VelocityPluginDescription(String id, @Nullable String name, @Nullable String version, @Nullable String description, @Nullable String url, @Nullable List authors, Collection dependencies, Path source) { diff --git a/proxy/src/main/java/com/velocitypowered/proxy/plugin/util/PluginDependencyUtils.java b/proxy/src/main/java/com/velocitypowered/proxy/plugin/util/PluginDependencyUtils.java index 9bc2f2684..8bf4d1ff8 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/plugin/util/PluginDependencyUtils.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/plugin/util/PluginDependencyUtils.java @@ -35,7 +35,8 @@ public class PluginDependencyUtils { .allowsSelfLoops(false) .expectedNodeCount(candidates.size()) .build(); - Map candidateMap = Maps.uniqueIndex(candidates, PluginDescription::getId); + Map candidateMap = Maps.uniqueIndex(candidates, + PluginDescription::getId); for (PluginDescription description : candidates) { graph.addNode(description); diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/MinecraftDecoder.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/MinecraftDecoder.java index 90532d0ec..a6f616941 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/MinecraftDecoder.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/MinecraftDecoder.java @@ -17,6 +17,11 @@ public class MinecraftDecoder extends MessageToMessageDecoder { private StateRegistry state; private StateRegistry.PacketRegistry.ProtocolRegistry registry; + /** + * Creates a new {@code MinecraftDecoder} decoding packets from the specified {@code direction}. + * + * @param direction the direction from which we decode from + */ public MinecraftDecoder(ProtocolUtils.Direction direction) { this.direction = Preconditions.checkNotNull(direction, "direction"); this.registry = direction diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/MinecraftEncoder.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/MinecraftEncoder.java index d0caaac40..889232de5 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/MinecraftEncoder.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/MinecraftEncoder.java @@ -15,6 +15,11 @@ public class MinecraftEncoder extends MessageToByteEncoder { private StateRegistry state; private StateRegistry.PacketRegistry.ProtocolRegistry registry; + /** + * Creates a new {@code MinecraftEncoder} encoding packets for the specified {@code direction}. + * + * @param direction the direction to encode to + */ public MinecraftEncoder(ProtocolUtils.Direction direction) { this.direction = Preconditions.checkNotNull(direction, "direction"); this.registry = direction diff --git a/proxy/src/main/java/com/velocitypowered/proxy/scheduler/VelocityScheduler.java b/proxy/src/main/java/com/velocitypowered/proxy/scheduler/VelocityScheduler.java index 0d4ebe25e..0bd4bcd3d 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/scheduler/VelocityScheduler.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/scheduler/VelocityScheduler.java @@ -1,5 +1,8 @@ package com.velocitypowered.proxy.scheduler; +import static com.google.common.base.Preconditions.checkArgument; +import static com.google.common.base.Preconditions.checkNotNull; + import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableList; import com.google.common.collect.Multimap; @@ -29,6 +32,11 @@ public class VelocityScheduler implements Scheduler { private final Multimap tasksByPlugin = Multimaps.synchronizedMultimap( Multimaps.newSetMultimap(new IdentityHashMap<>(), HashSet::new)); + /** + * Initalizes the scheduler. + * + * @param pluginManager the Velocity plugin manager + */ public VelocityScheduler(PluginManager pluginManager) { this.pluginManager = pluginManager; this.taskService = Executors.newCachedThreadPool(new ThreadFactoryBuilder().setDaemon(true) @@ -40,13 +48,17 @@ public class VelocityScheduler implements Scheduler { @Override public TaskBuilder buildTask(Object plugin, Runnable runnable) { - Preconditions.checkNotNull(plugin, "plugin"); - Preconditions.checkNotNull(runnable, "runnable"); - Preconditions - .checkArgument(pluginManager.fromInstance(plugin).isPresent(), "plugin is not registered"); + checkNotNull(plugin, "plugin"); + checkNotNull(runnable, "runnable"); + checkArgument(pluginManager.fromInstance(plugin).isPresent(), "plugin is not registered"); return new TaskBuilderImpl(plugin, runnable); } + /** + * Shuts down the Velocity scheduler. + * @return {@code true} if all tasks finished, {@code false} otherwise + * @throws InterruptedException if the current thread was interrupted + */ public boolean shutdown() throws InterruptedException { Collection terminating; synchronized (tasksByPlugin) { @@ -121,7 +133,7 @@ public class VelocityScheduler implements Scheduler { this.repeat = repeat; } - public void schedule() { + void schedule() { if (repeat == 0) { this.future = timerExecutionService.schedule(this, delay, TimeUnit.MILLISECONDS); } else {