Fix Bedrock servers not always pinging

This commit is contained in:
Braydon 2024-04-10 05:59:03 -04:00
parent 657c7196dd
commit ca794e288c
2 changed files with 14 additions and 11 deletions

@ -26,6 +26,7 @@ package me.braydon.mc.common.packet.impl.bedrock;
import lombok.Getter; import lombok.Getter;
import lombok.NonNull; import lombok.NonNull;
import me.braydon.mc.common.packet.MinecraftBedrockPacket; import me.braydon.mc.common.packet.MinecraftBedrockPacket;
import me.braydon.mc.model.server.BedrockMinecraftServer;
import java.io.IOException; import java.io.IOException;
import java.net.DatagramPacket; import java.net.DatagramPacket;
@ -67,7 +68,18 @@ public final class BedrockPacketUnconnectedPong implements MinecraftBedrockPacke
ByteBuffer buffer = ByteBuffer.wrap(receivePacket.getData()).order(ByteOrder.LITTLE_ENDIAN); ByteBuffer buffer = ByteBuffer.wrap(receivePacket.getData()).order(ByteOrder.LITTLE_ENDIAN);
byte id = buffer.get(); // The received packet id byte id = buffer.get(); // The received packet id
if (id == 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;
} }
} }
} }

@ -75,16 +75,7 @@ public final class BedrockMinecraftServer extends MinecraftServer {
@NonNull @NonNull
public static BedrockMinecraftServer create(@NonNull String hostname, String ip, int port, @NonNull String token) { public static BedrockMinecraftServer create(@NonNull String hostname, String ip, int port, @NonNull String token) {
String[] split = token.split(";"); // Split the token String[] split = token.split(";"); // Split the token
Edition edition = Edition.valueOf(split[0]);
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));
}
Version version = new Version(Integer.parseInt(split[2]), split[3]); Version version = new Version(Integer.parseInt(split[2]), split[3]);
Players players = new Players(Integer.parseInt(split[4]), Integer.parseInt(split[5]), null); Players players = new Players(Integer.parseInt(split[4]), Integer.parseInt(split[5]), null);
MOTD motd = MOTD.create(split[1] + "\n" + split[7]); MOTD motd = MOTD.create(split[1] + "\n" + split[7]);