From 50b463b439ff9c634b0e0c3e9bc0b8197dae5ac4 Mon Sep 17 00:00:00 2001 From: Gravita <12893402+gravit0@users.noreply.github.com> Date: Fri, 21 Apr 2023 16:14:33 +0700 Subject: [PATCH] [FEATURE] Support custom protocol --- .../java/pro/gravit/launcher/client/ServerPinger.java | 8 +++++--- .../java/pro/gravit/launcher/profiles/ClientProfile.java | 1 + 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/Launcher/src/main/java/pro/gravit/launcher/client/ServerPinger.java b/Launcher/src/main/java/pro/gravit/launcher/client/ServerPinger.java index 7de97b99..07d9a1d9 100644 --- a/Launcher/src/main/java/pro/gravit/launcher/client/ServerPinger.java +++ b/Launcher/src/main/java/pro/gravit/launcher/client/ServerPinger.java @@ -30,6 +30,7 @@ public final class ServerPinger { private static final int PACKET_LENGTH = 65535; // Instance private final InetSocketAddress address; + private final int protocol; private final ClientProfile.Version version; // Cache private final Object cacheLock = new Object(); @@ -47,6 +48,7 @@ public ServerPinger(ClientProfile.ServerProfile profile, ClientProfile.Version v } this.address = profile.toSocketAddress(); this.version = Objects.requireNonNull(version, "version"); + this.protocol = profile.protocol; } private static String readUTF16String(HInput input) throws IOException { @@ -65,7 +67,7 @@ private Result doPing() throws IOException { socket.connect(IOHelper.resolve(address), IOHelper.SOCKET_TIMEOUT); try (HInput input = new HInput(socket.getInputStream()); HOutput output = new HOutput(socket.getOutputStream())) { - return version.compareTo(ClientProfileVersions.MINECRAFT_1_7_2) >= 0 ? modernPing(input, output) : legacyPing(input, output, version.compareTo(ClientProfileVersions.MINECRAFT_1_6_4) >= 0); + return version.compareTo(ClientProfileVersions.MINECRAFT_1_7_2) >= 0 ? modernPing(input, output, protocol) : legacyPing(input, output, version.compareTo(ClientProfileVersions.MINECRAFT_1_6_4) >= 0); } } } @@ -121,13 +123,13 @@ private Result legacyPing(HInput input, HOutput output, boolean mc16) throws IOE return new Result(onlinePlayers, maxPlayers, response); } - private Result modernPing(HInput input, HOutput output) throws IOException { + private Result modernPing(HInput input, HOutput output, int protocol) throws IOException { // Prepare handshake packet byte[] handshakePacket; try (ByteArrayOutputStream packetArray = IOHelper.newByteArrayOutput()) { try (HOutput packetOutput = new HOutput(packetArray)) { packetOutput.writeVarInt(0x0); // Handshake packet ID - packetOutput.writeVarInt(0x4); // Protocol version + packetOutput.writeVarInt(protocol > 0 ? protocol : 0x4); // Protocol version packetOutput.writeString(address.getHostString(), 0); // Server address packetOutput.writeShort((short) address.getPort()); // Server port packetOutput.writeVarInt(0x1); // Next state - status diff --git a/LauncherAPI/src/main/java/pro/gravit/launcher/profiles/ClientProfile.java b/LauncherAPI/src/main/java/pro/gravit/launcher/profiles/ClientProfile.java index a5475723..b0265d08 100644 --- a/LauncherAPI/src/main/java/pro/gravit/launcher/profiles/ClientProfile.java +++ b/LauncherAPI/src/main/java/pro/gravit/launcher/profiles/ClientProfile.java @@ -544,6 +544,7 @@ public static class ServerProfile { public String serverAddress; public int serverPort; public boolean isDefault = true; + public int protocol = -1; public boolean socketPing = true; public ServerProfile() {