From 89bcc9a100228bd8b5bfbfb21dd793c9e3bed963 Mon Sep 17 00:00:00 2001 From: Andrew Steinborn Date: Sat, 29 Dec 2018 17:22:36 -0500 Subject: [PATCH] Grab most of the low-hanging fruit. --- .../api/proxy/server/QueryResponseTest.java | 2 +- .../api/proxy/server/ServerPingTest.java | 2 +- gradle/checkstyle.gradle | 4 ++ proxy/build.gradle | 4 ++ .../proxy/command/VelocityCommandManager.java | 6 +++ .../proxy/config/AnnotatedConfig.java | 15 +++++-- .../proxy/config/VelocityConfiguration.java | 6 +++ .../backend/BackendPlaySessionHandler.java | 2 +- .../backend/VelocityServerConnection.java | 14 ++++++ .../client/ClientPlaySessionHandler.java | 3 +- .../client/InitialInboundConnection.java | 2 +- .../client/StatusSessionHandler.java | 2 +- .../legacy/LegacyForgeConnectionType.java | 3 +- .../connection/util/ConnectionTypeImpl.java | 2 +- .../proxy/console/VelocityConsole.java | 20 ++++----- .../ServerChannelInitializerHolder.java | 5 +++ .../proxy/plugin/VelocityPluginManager.java | 2 +- .../loader/{ => java}/JavaPluginLoader.java | 5 ++- .../java/JavaVelocityPluginDescription.java | 6 +-- .../loader/java/VelocityPluginModule.java | 4 +- .../proxy/protocol/ProtocolUtils.java | 43 +++++++++++++++---- .../proxy/protocol/StateRegistry.java | 24 +++++++++-- .../proxy/tablist/VelocityTabList.java | 11 ++++- 23 files changed, 143 insertions(+), 44 deletions(-) rename proxy/src/main/java/com/velocitypowered/proxy/plugin/loader/{ => java}/JavaPluginLoader.java (95%) diff --git a/api/src/test/java/com/velocitypowered/api/proxy/server/QueryResponseTest.java b/api/src/test/java/com/velocitypowered/api/proxy/server/QueryResponseTest.java index ffeeccb20..c7222a06f 100644 --- a/api/src/test/java/com/velocitypowered/api/proxy/server/QueryResponseTest.java +++ b/api/src/test/java/com/velocitypowered/api/proxy/server/QueryResponseTest.java @@ -1,6 +1,6 @@ package com.velocitypowered.api.proxy.server; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertEquals; import com.google.common.collect.ImmutableList; import com.velocitypowered.api.proxy.server.QueryResponse.PluginInformation; diff --git a/api/src/test/java/com/velocitypowered/api/proxy/server/ServerPingTest.java b/api/src/test/java/com/velocitypowered/api/proxy/server/ServerPingTest.java index 1c1ac49d8..76caaa3a7 100644 --- a/api/src/test/java/com/velocitypowered/api/proxy/server/ServerPingTest.java +++ b/api/src/test/java/com/velocitypowered/api/proxy/server/ServerPingTest.java @@ -1,6 +1,6 @@ package com.velocitypowered.api.proxy.server; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertEquals; import com.google.common.collect.ImmutableList; import com.velocitypowered.api.proxy.server.ServerPing.Players; diff --git a/gradle/checkstyle.gradle b/gradle/checkstyle.gradle index 7ff1c3b2d..3a193db55 100644 --- a/gradle/checkstyle.gradle +++ b/gradle/checkstyle.gradle @@ -1,4 +1,8 @@ checkstyle { toolVersion '8.14' configFile new File(project.rootDir, ['config', 'checkstyle', 'checkstyle.xml'].join(File.separator)) + + // The build should immediately fail if we have errors. + maxErrors = 0 + maxWarnings = 0 } \ No newline at end of file diff --git a/proxy/build.gradle b/proxy/build.gradle index 9d0621095..c2331e025 100644 --- a/proxy/build.gradle +++ b/proxy/build.gradle @@ -20,6 +20,10 @@ jar { } } +tasks.withType(Checkstyle) { + exclude('**/com/velocitypowered/proxy/protocol/packet/*.java') +} + dependencies { // Note: we depend on the API twice, first the main sourceset, and then the annotation processor. compile project(':velocity-api') diff --git a/proxy/src/main/java/com/velocitypowered/proxy/command/VelocityCommandManager.java b/proxy/src/main/java/com/velocitypowered/proxy/command/VelocityCommandManager.java index 5593352b0..c24f47dd6 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/command/VelocityCommandManager.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/command/VelocityCommandManager.java @@ -123,6 +123,12 @@ public class VelocityCommandManager implements CommandManager { } } + /** + * Determines if the {@code source} has permission to run the {@code cmdLine}. + * @param source the source to check against + * @param cmdLine the command to run + * @return {@code true} if the command can be run, otherwise {@code false} + */ public boolean hasPermission(CommandSource source, String cmdLine) { Preconditions.checkNotNull(source, "source"); Preconditions.checkNotNull(cmdLine, "cmdLine"); diff --git a/proxy/src/main/java/com/velocitypowered/proxy/config/AnnotatedConfig.java b/proxy/src/main/java/com/velocitypowered/proxy/config/AnnotatedConfig.java index 5ea3546a8..6d55bb1d4 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/config/AnnotatedConfig.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/config/AnnotatedConfig.java @@ -36,7 +36,10 @@ public abstract class AnnotatedConfig { @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.FIELD, ElementType.TYPE}) public @interface Table { - + /** + * The table's name. + * @return the table's name + */ String value(); } @@ -46,7 +49,10 @@ public abstract class AnnotatedConfig { @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.FIELD, ElementType.TYPE}) public @interface Comment { - + /** + * The comments to include with this key. Each entry is considered a line. + * @return the comments + */ String[] value(); } @@ -56,7 +62,10 @@ public abstract class AnnotatedConfig { @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.FIELD, ElementType.TYPE}) public @interface ConfigKey { - + /** + * The name of this field in the configuration. + * @return the field's name + */ String value(); } 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 83e4b8fed..f6ff81ecd 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/config/VelocityConfiguration.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/config/VelocityConfiguration.java @@ -356,6 +356,12 @@ public class VelocityConfiguration extends AnnotatedConfig implements ProxyConfi .toString(); } + /** + * Reads the Velocity configuration from {@code path}. + * @param path the path to read from + * @return the deserialized Velocity configuration + * @throws IOException if we could not read from the {@code path}. + */ public static VelocityConfiguration read(Path path) throws IOException { Toml toml; if (!path.toFile().exists()) { 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 d61b0126c..215587e54 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 @@ -5,8 +5,8 @@ import com.mojang.brigadier.builder.LiteralArgumentBuilder; import com.mojang.brigadier.builder.RequiredArgumentBuilder; import com.mojang.brigadier.tree.LiteralCommandNode; import com.velocitypowered.api.event.connection.PluginMessageEvent; -import com.velocitypowered.api.proxy.messages.ChannelIdentifier; import com.velocitypowered.api.network.ProtocolVersion; +import com.velocitypowered.api.proxy.messages.ChannelIdentifier; import com.velocitypowered.proxy.VelocityServer; import com.velocitypowered.proxy.connection.MinecraftConnection; import com.velocitypowered.proxy.connection.MinecraftSessionHandler; 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 cd81ca962..87fb29436 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 @@ -53,6 +53,12 @@ public class VelocityServerConnection implements MinecraftConnectionAssociation, private long lastPingId; private long lastPingSent; + /** + * Initializes a new server connection. + * @param registeredServer the server to connect to + * @param proxyPlayer the player connecting to the server + * @param server the Velocity proxy instance + */ public VelocityServerConnection(VelocityRegisteredServer registeredServer, ConnectedPlayer proxyPlayer, VelocityServer server) { this.registeredServer = registeredServer; @@ -60,6 +66,11 @@ public class VelocityServerConnection implements MinecraftConnectionAssociation, this.server = server; } + /** + * Connects to the server. + * @return a {@link com.velocitypowered.api.proxy.ConnectionRequestBuilder.Result} representing + * whether or not the connect succeeded + */ public CompletableFuture connect() { CompletableFuture result = new CompletableFuture<>(); server.initializeGenericBootstrap() @@ -167,6 +178,9 @@ public class VelocityServerConnection implements MinecraftConnectionAssociation, return proxyPlayer; } + /** + * Disconnects from the server. + */ public void disconnect() { if (connection != null) { gracefulDisconnect = 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 cb8cd4618..8317a1575 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 @@ -188,7 +188,8 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler { MinecraftConnection backendConn = serverConn != null ? serverConn.getConnection() : null; if (serverConn != null && backendConn != null) { if (backendConn.getState() != StateRegistry.PLAY) { - logger.warn("Plugin message was sent while the backend was in PLAY. Channel: {}. Packet discarded."); + logger.warn("A plugin message was received while the backend server was not " + + "ready. Channel: {}. Packet discarded.", packet.getChannel()); } else if (PluginMessageUtil.isRegister(packet)) { List actuallyRegistered = new ArrayList<>(); List channels = PluginMessageUtil.getChannels(packet); diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/InitialInboundConnection.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/InitialInboundConnection.java index c280e9579..fe6b53e3c 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/InitialInboundConnection.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/InitialInboundConnection.java @@ -1,7 +1,7 @@ package com.velocitypowered.proxy.connection.client; -import com.velocitypowered.api.proxy.InboundConnection; import com.velocitypowered.api.network.ProtocolVersion; +import com.velocitypowered.api.proxy.InboundConnection; import com.velocitypowered.proxy.connection.MinecraftConnection; import com.velocitypowered.proxy.connection.MinecraftConnectionAssociation; import com.velocitypowered.proxy.protocol.packet.Disconnect; diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/StatusSessionHandler.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/StatusSessionHandler.java index ca0d75f36..8caaf16ae 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/StatusSessionHandler.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/StatusSessionHandler.java @@ -2,10 +2,10 @@ package com.velocitypowered.proxy.connection.client; import com.google.common.collect.ImmutableList; import com.velocitypowered.api.event.proxy.ProxyPingEvent; +import com.velocitypowered.api.network.ProtocolVersion; import com.velocitypowered.api.proxy.InboundConnection; import com.velocitypowered.api.proxy.server.ServerPing; import com.velocitypowered.api.util.ModInfo; -import com.velocitypowered.api.network.ProtocolVersion; import com.velocitypowered.proxy.VelocityServer; import com.velocitypowered.proxy.config.VelocityConfiguration; import com.velocitypowered.proxy.connection.MinecraftConnection; diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/forge/legacy/LegacyForgeConnectionType.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/forge/legacy/LegacyForgeConnectionType.java index fad5e953f..0b0ded3d3 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/forge/legacy/LegacyForgeConnectionType.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/forge/legacy/LegacyForgeConnectionType.java @@ -14,7 +14,8 @@ public class LegacyForgeConnectionType extends ConnectionTypeImpl { new GameProfile.Property("forgeClient", "true", ""); public LegacyForgeConnectionType() { - super(LegacyForgeHandshakeClientPhase.NOT_STARTED, LegacyForgeHandshakeBackendPhase.NOT_STARTED); + super(LegacyForgeHandshakeClientPhase.NOT_STARTED, + LegacyForgeHandshakeBackendPhase.NOT_STARTED); } @Override diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/util/ConnectionTypeImpl.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/util/ConnectionTypeImpl.java index cba5efac7..123f1c640 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/util/ConnectionTypeImpl.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/util/ConnectionTypeImpl.java @@ -7,7 +7,7 @@ import com.velocitypowered.proxy.connection.backend.BackendConnectionPhase; import com.velocitypowered.proxy.connection.client.ClientConnectionPhase; /** - * Indicates the type of connection that has been made + * Indicates the type of connection that has been made. */ public class ConnectionTypeImpl implements ConnectionType { 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 49f0348cf..1768830a5 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/console/VelocityConsole.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/console/VelocityConsole.java @@ -1,5 +1,7 @@ package com.velocitypowered.proxy.console; +import static com.velocitypowered.api.permission.PermissionFunction.*; + import com.velocitypowered.api.event.permission.PermissionsSetupEvent; import com.velocitypowered.api.permission.PermissionFunction; import com.velocitypowered.api.permission.Tristate; @@ -26,7 +28,7 @@ public final class VelocityConsole extends SimpleTerminalConsole implements Cons private static final Logger logger = LogManager.getLogger(VelocityConsole.class); private final VelocityServer server; - private PermissionFunction permissionFunction = PermissionFunction.ALWAYS_TRUE; + private PermissionFunction permissionFunction = ALWAYS_TRUE; public VelocityConsole(VelocityServer server) { this.server = server; @@ -43,20 +45,14 @@ public final class VelocityConsole extends SimpleTerminalConsole implements Cons } public void setupStreams() { - System.setOut(IoBuilder.forLogger(logger) - .setLevel(Level.INFO) - .buildPrintStream()); - System.setErr(IoBuilder.forLogger(logger) - .setLevel(Level.ERROR) - .buildPrintStream()); + System.setOut(IoBuilder.forLogger(logger).setLevel(Level.INFO).buildPrintStream()); + System.setErr(IoBuilder.forLogger(logger).setLevel(Level.ERROR).buildPrintStream()); } public void setupPermissions() { - PermissionsSetupEvent event = new PermissionsSetupEvent(this, - s -> PermissionFunction.ALWAYS_TRUE); - this.server.getEventManager().fire(event) - .join(); // this is called on startup, we can safely #join - this.permissionFunction = event.createFunction(this); + PermissionsSetupEvent event = new PermissionsSetupEvent(this, s -> ALWAYS_TRUE); + // we can safely block here, this is before any listeners fire + this.permissionFunction = this.server.getEventManager().fire(event).join().createFunction(this); } @Override diff --git a/proxy/src/main/java/com/velocitypowered/proxy/network/ServerChannelInitializerHolder.java b/proxy/src/main/java/com/velocitypowered/proxy/network/ServerChannelInitializerHolder.java index 96c154eba..f84e56e72 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/network/ServerChannelInitializerHolder.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/network/ServerChannelInitializerHolder.java @@ -20,6 +20,11 @@ public class ServerChannelInitializerHolder implements Supplier initializer) { LOGGER.warn("The server channel initializer has been replaced by {}", 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 02f3391d2..d41a8e72d 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/plugin/VelocityPluginManager.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/plugin/VelocityPluginManager.java @@ -8,7 +8,7 @@ import com.velocitypowered.api.plugin.PluginDescription; import com.velocitypowered.api.plugin.PluginManager; import com.velocitypowered.api.plugin.meta.PluginDependency; import com.velocitypowered.proxy.VelocityServer; -import com.velocitypowered.proxy.plugin.loader.JavaPluginLoader; +import com.velocitypowered.proxy.plugin.loader.java.JavaPluginLoader; import com.velocitypowered.proxy.plugin.util.PluginDependencyUtils; import java.io.IOException; import java.nio.file.DirectoryStream; diff --git a/proxy/src/main/java/com/velocitypowered/proxy/plugin/loader/JavaPluginLoader.java b/proxy/src/main/java/com/velocitypowered/proxy/plugin/loader/java/JavaPluginLoader.java similarity index 95% rename from proxy/src/main/java/com/velocitypowered/proxy/plugin/loader/JavaPluginLoader.java rename to proxy/src/main/java/com/velocitypowered/proxy/plugin/loader/java/JavaPluginLoader.java index 7f29712fe..9493a984c 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/plugin/loader/JavaPluginLoader.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/plugin/loader/java/JavaPluginLoader.java @@ -1,4 +1,4 @@ -package com.velocitypowered.proxy.plugin.loader; +package com.velocitypowered.proxy.plugin.loader.java; import com.google.inject.Guice; import com.google.inject.Injector; @@ -10,6 +10,9 @@ import com.velocitypowered.api.plugin.meta.PluginDependency; import com.velocitypowered.api.proxy.ProxyServer; import com.velocitypowered.proxy.VelocityServer; import com.velocitypowered.proxy.plugin.PluginClassLoader; +import com.velocitypowered.proxy.plugin.loader.PluginLoader; +import com.velocitypowered.proxy.plugin.loader.VelocityPluginContainer; +import com.velocitypowered.proxy.plugin.loader.VelocityPluginDescription; import com.velocitypowered.proxy.plugin.loader.java.JavaVelocityPluginDescription; import com.velocitypowered.proxy.plugin.loader.java.VelocityPluginModule; import java.io.BufferedInputStream; diff --git a/proxy/src/main/java/com/velocitypowered/proxy/plugin/loader/java/JavaVelocityPluginDescription.java b/proxy/src/main/java/com/velocitypowered/proxy/plugin/loader/java/JavaVelocityPluginDescription.java index 2948ee2d8..3a775991e 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/plugin/loader/java/JavaVelocityPluginDescription.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/plugin/loader/java/JavaVelocityPluginDescription.java @@ -9,11 +9,11 @@ import java.util.Collection; import java.util.List; import org.checkerframework.checker.nullness.qual.Nullable; -public class JavaVelocityPluginDescription extends VelocityPluginDescription { +class JavaVelocityPluginDescription extends VelocityPluginDescription { private final Class mainClass; - public JavaVelocityPluginDescription(String id, @Nullable String name, @Nullable String version, + JavaVelocityPluginDescription(String id, @Nullable String name, @Nullable String version, @Nullable String description, @Nullable String url, @Nullable List authors, Collection dependencies, Path source, Class mainClass) { @@ -21,7 +21,7 @@ public class JavaVelocityPluginDescription extends VelocityPluginDescription { this.mainClass = checkNotNull(mainClass); } - public Class getMainClass() { + Class getMainClass() { return mainClass; } } 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 d35c9382a..0f5a61cf1 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 @@ -12,13 +12,13 @@ import java.nio.file.Path; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class VelocityPluginModule implements Module { +class VelocityPluginModule implements Module { private final ProxyServer server; private final JavaVelocityPluginDescription description; private final Path basePluginPath; - public VelocityPluginModule(ProxyServer server, JavaVelocityPluginDescription description, + VelocityPluginModule(ProxyServer server, JavaVelocityPluginDescription description, Path basePluginPath) { this.server = server; this.description = description; diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/ProtocolUtils.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/ProtocolUtils.java index dbc99e8d3..85bce8394 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/ProtocolUtils.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/ProtocolUtils.java @@ -83,6 +83,11 @@ public enum ProtocolUtils { return str; } + /** + * Writes the specified {@code str} to the {@code buf} with a VarInt prefix. + * @param buf the buffer to write to + * @param str the string to write + */ public static void writeString(ByteBuf buf, String str) { int size = ByteBufUtil.utf8Bytes(str); writeVarInt(buf, size); @@ -93,6 +98,13 @@ public enum ProtocolUtils { return readByteArray(buf, DEFAULT_MAX_STRING_SIZE); } + /** + * Reads a VarInt length-prefixed byte array from the {@code buf}, making sure to not go over + * {@code cap} size. + * @param buf the buffer to read from + * @param cap the maximum size of the string, in UTF-8 character length + * @return the byte array + */ public static byte[] readByteArray(ByteBuf buf, int cap) { int length = readVarInt(buf); checkArgument(length <= cap, "Bad string size (got %s, maximum is %s)", length, cap); @@ -109,6 +121,11 @@ public enum ProtocolUtils { buf.writeBytes(array); } + /** + * Reads an VarInt-prefixed array of VarInt integers from the {@code buf}. + * @param buf the buffer to read from + * @return an array of integers + */ public static int[] readIntegerArray(ByteBuf buf) { int len = readVarInt(buf); int[] array = new int[len]; @@ -118,13 +135,11 @@ public enum ProtocolUtils { return array; } - public static void writeIntegerArray(ByteBuf buf, int[] array) { - writeVarInt(buf, array.length); - for (int i : array) { - writeVarInt(buf, i); - } - } - + /** + * Reads an UUID from the {@code buf}. + * @param buf the buffer to read from + * @return the UUID from the buffer + */ public static UUID readUuid(ByteBuf buf) { long msb = buf.readLong(); long lsb = buf.readLong(); @@ -136,6 +151,11 @@ public enum ProtocolUtils { buf.writeLong(uuid.getLeastSignificantBits()); } + /** + * Writes a list of {@link com.velocitypowered.api.util.GameProfile.Property} to the buffer. + * @param buf the buffer to write to + * @param properties the properties to serialize + */ public static void writeProperties(ByteBuf buf, List properties) { writeVarInt(buf, properties.size()); for (GameProfile.Property property : properties) { @@ -151,6 +171,11 @@ public enum ProtocolUtils { } } + /** + * Reads a list of {@link com.velocitypowered.api.util.GameProfile.Property} from the buffer. + * @param buf the buffer to read from + * @return the read properties + */ public static List readProperties(ByteBuf buf) { List properties = new ArrayList<>(); int size = readVarInt(buf); @@ -172,9 +197,9 @@ public enum ProtocolUtils { CLIENTBOUND; public StateRegistry.PacketRegistry.ProtocolRegistry getProtocolRegistry(StateRegistry state, - ProtocolVersion protocolVersion) { + ProtocolVersion version) { return (this == SERVERBOUND ? state.serverbound : state.clientbound) - .getProtocolRegistry(protocolVersion); + .getProtocolRegistry(version); } } } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/StateRegistry.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/StateRegistry.java index 387884666..4ad06e24d 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/StateRegistry.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/StateRegistry.java @@ -50,7 +50,12 @@ import io.netty.util.collection.IntObjectMap; import it.unimi.dsi.fastutil.objects.Object2IntMap; import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap; -import java.util.*; +import java.util.Collection; +import java.util.Collections; +import java.util.EnumMap; +import java.util.EnumSet; +import java.util.Map; +import java.util.Objects; import java.util.function.Supplier; import org.checkerframework.checker.nullness.qual.Nullable; @@ -245,7 +250,7 @@ public enum StateRegistry { this.versions = Collections.unmodifiableMap(mutableVersions); } - public ProtocolRegistry getProtocolRegistry(final ProtocolVersion version) { + ProtocolRegistry getProtocolRegistry(final ProtocolVersion version) { ProtocolRegistry registry = versions.get(version); if (registry == null) { if (fallback) { @@ -256,7 +261,7 @@ public enum StateRegistry { return registry; } - public

void register(Class

clazz, Supplier

packetSupplier, +

void register(Class

clazz, Supplier

packetSupplier, PacketMapping... mappings) { if (mappings.length == 0) { throw new IllegalArgumentException("At least one mapping must be provided."); @@ -301,6 +306,11 @@ public enum StateRegistry { this.packetClassToId.defaultReturnValue(Integer.MIN_VALUE); } + /** + * Attempts to create a packet from the specified {@code id}. + * @param id the packet ID + * @return the packet instance, or {@code null} if the ID is not registered + */ public @Nullable MinecraftPacket createPacket(final int id) { final Supplier supplier = this.packetIdToSupplier.get(id); if (supplier == null) { @@ -309,6 +319,12 @@ public enum StateRegistry { return supplier.get(); } + /** + * Attempts to look up the packet ID for an {@code packet}. + * @param packet the packet to look up + * @return the packet ID + * @throws IllegalArgumentException if the packet ID is not found + */ public int getPacketId(final MinecraftPacket packet) { final int id = this.packetClassToId.getInt(packet.getClass()); if (id == Integer.MIN_VALUE) { @@ -328,7 +344,7 @@ public enum StateRegistry { private final ProtocolVersion protocolVersion; private final boolean encodeOnly; - public PacketMapping(int id, ProtocolVersion protocolVersion, boolean packetDecoding) { + PacketMapping(int id, ProtocolVersion protocolVersion, boolean packetDecoding) { this.id = id; this.protocolVersion = protocolVersion; this.encodeOnly = packetDecoding; diff --git a/proxy/src/main/java/com/velocitypowered/proxy/tablist/VelocityTabList.java b/proxy/src/main/java/com/velocitypowered/proxy/tablist/VelocityTabList.java index edb449590..5a0d5db2a 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/tablist/VelocityTabList.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/tablist/VelocityTabList.java @@ -65,7 +65,12 @@ public class VelocityTabList implements TabList { return Optional.ofNullable(entry); } - public void clearAll() { // Note: this method is called upon server switch + /** + * Clears all entries from the tab list. Note that the entries are written with + * {@link MinecraftConnection#delayedWrite(Object)}, so make sure to do an explicit + * {@link MinecraftConnection#flush()}. + */ + public void clearAll() { List items = new ArrayList<>(); for (TabListEntry value : entries.values()) { items.add(PlayerListItem.Item.from(value)); @@ -85,6 +90,10 @@ public class VelocityTabList implements TabList { return new VelocityTabListEntry(this, profile, displayName, latency, gameMode); } + /** + * Processes a tab list entry packet from the backend. + * @param packet the packet to process + */ public void processBackendPacket(PlayerListItem packet) { // Packets are already forwarded on, so no need to do that here for (PlayerListItem.Item item : packet.getItems()) {