From ca794e288ca7ab73f1211943730f5a80d5cd4227 Mon Sep 17 00:00:00 2001 From: Rainnny7 Date: Wed, 10 Apr 2024 05:59:03 -0400 Subject: [PATCH] Fix Bedrock servers not always pinging --- .../impl/bedrock/BedrockPacketUnconnectedPong.java | 14 +++++++++++++- .../mc/model/server/BedrockMinecraftServer.java | 11 +---------- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/main/java/me/braydon/mc/common/packet/impl/bedrock/BedrockPacketUnconnectedPong.java b/src/main/java/me/braydon/mc/common/packet/impl/bedrock/BedrockPacketUnconnectedPong.java index a36ca79..4ce86df 100644 --- a/src/main/java/me/braydon/mc/common/packet/impl/bedrock/BedrockPacketUnconnectedPong.java +++ b/src/main/java/me/braydon/mc/common/packet/impl/bedrock/BedrockPacketUnconnectedPong.java @@ -26,6 +26,7 @@ package me.braydon.mc.common.packet.impl.bedrock; import lombok.Getter; import lombok.NonNull; import me.braydon.mc.common.packet.MinecraftBedrockPacket; +import me.braydon.mc.model.server.BedrockMinecraftServer; import java.io.IOException; import java.net.DatagramPacket; @@ -67,7 +68,18 @@ public final class BedrockPacketUnconnectedPong implements MinecraftBedrockPacke ByteBuffer buffer = ByteBuffer.wrap(receivePacket.getData()).order(ByteOrder.LITTLE_ENDIAN); byte id = buffer.get(); // The received packet id if (id == ID) { - response = new String(buffer.array(), StandardCharsets.UTF_8).substring(34).trim(); + String response = new String(buffer.array(), StandardCharsets.UTF_8).trim(); // Extract the response + + // Trim the length of the response (short) from the + // start of the string, which begins with the edition name + for (BedrockMinecraftServer.Edition edition : BedrockMinecraftServer.Edition.values()) { + int startIndex = response.indexOf(edition.name()); + if (startIndex != -1) { + response = response.substring(startIndex); + break; + } + } + this.response = response; } } } \ No newline at end of file diff --git a/src/main/java/me/braydon/mc/model/server/BedrockMinecraftServer.java b/src/main/java/me/braydon/mc/model/server/BedrockMinecraftServer.java index f1c3dd7..0444660 100644 --- a/src/main/java/me/braydon/mc/model/server/BedrockMinecraftServer.java +++ b/src/main/java/me/braydon/mc/model/server/BedrockMinecraftServer.java @@ -75,16 +75,7 @@ public final class BedrockMinecraftServer extends MinecraftServer { @NonNull public static BedrockMinecraftServer create(@NonNull String hostname, String ip, int port, @NonNull String token) { String[] split = token.split(";"); // Split the token - - Edition edition; - try { - edition = Edition.valueOf(split[0]); - } catch (IllegalArgumentException ex) { - // Funky fix, but sometimes the edition can contain an extra - // portion of the length of the edition, so we need to remove it - edition = Edition.valueOf(split[0].substring(1)); - } - + Edition edition = Edition.valueOf(split[0]); Version version = new Version(Integer.parseInt(split[2]), split[3]); Players players = new Players(Integer.parseInt(split[4]), Integer.parseInt(split[5]), null); MOTD motd = MOTD.create(split[1] + "\n" + split[7]);