Skip to content

Commit

Permalink
Fixup protocol auto-detection by removing the second -1 ping
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianMichael committed Jan 27, 2025
1 parent 2e9118c commit 8e6a6b9
Showing 1 changed file with 10 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class ProtocolVersionDetector {
private static final int TIMEOUT = 3000;

public static ProtocolVersion get(final SocketAddress serverAddress, final ProtocolVersion clientVersion) {
MCPingResponse response = MCPing
final MCPingResponse response = MCPing
.pingModern(clientVersion.getOriginalVersion())
.tcpSocketFactory(new SocketChannelSocketFactory())
.address(AddressUtil.toJ16UnixSocketAddress(serverAddress))
Expand All @@ -40,27 +40,19 @@ public static ProtocolVersion get(final SocketAddress serverAddress, final Proto

if (response.version.protocol == clientVersion.getOriginalVersion()) { // If the server is on the same version as the client, we can just connect
return clientVersion;
} else { // Else ping again with protocol id -1 to get the protocol id of the server
response = MCPing
.pingModern(-1)
.tcpSocketFactory(new SocketChannelSocketFactory())
.address(AddressUtil.toJ16UnixSocketAddress(serverAddress))
.noResolve()
.timeout(TIMEOUT, TIMEOUT)
.getSync();
}

if (ProtocolVersion.isRegistered(response.version.protocol)) { // If the protocol is registered, we can use it
return ProtocolVersion.getProtocol(response.version.protocol);
} else {
for (ProtocolVersion protocolVersion : ProtocolVersionList.getProtocolsNewToOld()) {
for (String version : protocolVersion.getIncludedVersions()) {
if (response.version.name.contains(version)) {
return protocolVersion;
}
if (ProtocolVersion.isRegistered(response.version.protocol)) { // If the protocol is registered, we can use it
return ProtocolVersion.getProtocol(response.version.protocol);
} else {
for (ProtocolVersion protocolVersion : ProtocolVersionList.getProtocolsNewToOld()) {
for (String version : protocolVersion.getIncludedVersions()) {
if (response.version.name.contains(version)) {
return protocolVersion;
}
}
throw new RuntimeException("Unable to detect the server version\nServer sent an invalid protocol id: " + response.version.protocol + " (" + response.version.name + "§r)");
}
throw new RuntimeException("Unable to detect the server version\nServer sent an invalid protocol id: " + response.version.protocol + " (" + response.version.name + "§r)");
}
}

Expand Down

0 comments on commit 8e6a6b9

Please sign in to comment.