From ce6b061b7968f3ccbca5fc03fc29d825a8538cb4 Mon Sep 17 00:00:00 2001 From: Andrew Steinborn Date: Fri, 7 Dec 2018 17:24:41 -0500 Subject: [PATCH] Make Velocity's server list ping behavior more conformant. This fixes pinging with 1.7.x and with certain ping libraries that send the -1 protocol version. --- .../client/HandshakeSessionHandler.java | 2 +- .../client/StatusSessionHandler.java | 5 ++--- .../proxy/protocol/StateRegistry.java | 20 ++++++++++++------- .../proxy/protocol/packet/Handshake.java | 7 ++++--- 4 files changed, 20 insertions(+), 14 deletions(-) diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/HandshakeSessionHandler.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/HandshakeSessionHandler.java index 9a04833e2..ca47e08e3 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/HandshakeSessionHandler.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/HandshakeSessionHandler.java @@ -179,7 +179,7 @@ public class HandshakeSessionHandler implements MinecraftSessionHandler { @Override public ProtocolVersion getProtocolVersion() { - return ProtocolVersion.UNKNOWN; + return ProtocolVersion.LEGACY; } } } 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 3787ed7f0..ca0d75f36 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 @@ -38,9 +38,8 @@ public class StatusSessionHandler implements MinecraftSessionHandler { public boolean handle(StatusRequest packet) { VelocityConfiguration configuration = server.getConfiguration(); - ProtocolVersion shownVersion = ProtocolVersion.isSupported(connection.getProtocolVersion()) ? connection - .getProtocolVersion() : - ProtocolVersion.MAXIMUM_VERSION; + ProtocolVersion shownVersion = ProtocolVersion.isSupported(connection.getProtocolVersion()) + ? connection.getProtocolVersion() : ProtocolVersion.MAXIMUM_VERSION; ServerPing initialPing = new ServerPing( new ServerPing.Version(shownVersion.getProtocol(), "Velocity " + ProtocolVersion.SUPPORTED_VERSION_STRING), 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 1c225f192..fa7d7033d 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/StateRegistry.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/StateRegistry.java @@ -200,14 +200,18 @@ public enum StateRegistry { public static class PacketRegistry { - private static final EnumMap> LINKED_PROTOCOL_VERSIONS = new EnumMap(ProtocolVersion.class); + private static final Map> LINKED_PROTOCOL_VERSIONS + = new EnumMap<>(ProtocolVersion.class); static { - LINKED_PROTOCOL_VERSIONS.put(MINECRAFT_1_9, ImmutableList.of(MINECRAFT_1_9_1, MINECRAFT_1_9_2, MINECRAFT_1_9_4)); - LINKED_PROTOCOL_VERSIONS.put(MINECRAFT_1_9_4, ImmutableList.of(MINECRAFT_1_10, MINECRAFT_1_11, MINECRAFT_1_11_1)); + LINKED_PROTOCOL_VERSIONS.put(MINECRAFT_1_9, ImmutableList.of(MINECRAFT_1_9_1, MINECRAFT_1_9_2, + MINECRAFT_1_9_4)); + LINKED_PROTOCOL_VERSIONS.put(MINECRAFT_1_9_4, ImmutableList.of(MINECRAFT_1_10, MINECRAFT_1_11, + MINECRAFT_1_11_1)); LINKED_PROTOCOL_VERSIONS.put(MINECRAFT_1_12, ImmutableList.of(MINECRAFT_1_12_1)); LINKED_PROTOCOL_VERSIONS.put(MINECRAFT_1_12_1, ImmutableList.of(MINECRAFT_1_12_2)); - LINKED_PROTOCOL_VERSIONS.put(MINECRAFT_1_13, ImmutableList.of(MINECRAFT_1_13_1, MINECRAFT_1_13_2)); + LINKED_PROTOCOL_VERSIONS.put(MINECRAFT_1_13, ImmutableList.of(MINECRAFT_1_13_1, + MINECRAFT_1_13_2)); } private final Direction direction; @@ -217,9 +221,11 @@ public enum StateRegistry { PacketRegistry(Direction direction) { this.direction = direction; - EnumMap mutableVersions = new EnumMap(ProtocolVersion.class); + Map mutableVersions = new EnumMap<>(ProtocolVersion.class); for (ProtocolVersion version : ProtocolVersion.values()) { - mutableVersions.put(version, new ProtocolRegistry(version)); + if (!version.isLegacy() && !version.isUnknown()) { + mutableVersions.put(version, new ProtocolRegistry(version)); + } } this.versions = Collections.unmodifiableMap(mutableVersions); @@ -252,7 +258,7 @@ public enum StateRegistry { } registry.packetClassToId.put(clazz, mapping.id); - ImmutableList linked = LINKED_PROTOCOL_VERSIONS.get(mapping.protocolVersion); + List linked = LINKED_PROTOCOL_VERSIONS.get(mapping.protocolVersion); if (linked != null) { links: for (int i = 0; i < linked.size(); i++) { diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/Handshake.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/Handshake.java index 515bf27b2..cf64b7b93 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/Handshake.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/Handshake.java @@ -56,15 +56,16 @@ public class Handshake implements MinecraftPacket { } @Override - public void decode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion protocolVersion) { - this.protocolVersion = ProtocolVersion.getProtocolVersion(ProtocolUtils.readVarInt(buf)); + public void decode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion ignored) { + int realProtocolVersion = ProtocolUtils.readVarInt(buf); + this.protocolVersion = ProtocolVersion.getProtocolVersion(realProtocolVersion); this.serverAddress = ProtocolUtils.readString(buf); this.port = buf.readUnsignedShort(); this.nextStatus = ProtocolUtils.readVarInt(buf); } @Override - public void encode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion protocolVersion) { + public void encode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion ignored) { ProtocolUtils.writeVarInt(buf, this.protocolVersion.getProtocol()); ProtocolUtils.writeString(buf, this.serverAddress); buf.writeShort(this.port);