From f811f65459a3f292ba4b8b76534d2ef68798c1e0 Mon Sep 17 00:00:00 2001 From: AlexProgrammerDE <40795980+AlexProgrammerDE@users.noreply.github.com> Date: Thu, 23 May 2024 08:08:57 +0200 Subject: [PATCH 1/4] Make code more readable before changes --- .../ClientboundStatusResponsePacket.java | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/status/clientbound/ClientboundStatusResponsePacket.java b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/status/clientbound/ClientboundStatusResponsePacket.java index 831c019e8..adb1d3448 100644 --- a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/status/clientbound/ClientboundStatusResponsePacket.java +++ b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/status/clientbound/ClientboundStatusResponsePacket.java @@ -49,6 +49,7 @@ public void serialize(ByteBuf out, MinecraftCodecHelper helper) { public ServerStatusInfo parseInfo() { JsonElement desc = data.get("description"); Component description = DefaultComponentSerializer.get().serializer().fromJson(desc, Component.class); + JsonObject plrs = data.get("players").getAsJsonObject(); List profiles = new ArrayList<>(); if (plrs.has("sample")) { @@ -62,8 +63,10 @@ public ServerStatusInfo parseInfo() { } PlayerInfo players = new PlayerInfo(plrs.get("max").getAsInt(), plrs.get("online").getAsInt(), profiles); + JsonObject ver = data.get("version").getAsJsonObject(); VersionInfo version = new VersionInfo(ver.get("name").getAsString(), ver.get("protocol").getAsInt()); + byte[] icon = null; if (data.has("favicon")) { icon = stringToIcon(data.get("favicon").getAsString()); @@ -83,9 +86,9 @@ public ClientboundStatusResponsePacket withInfo(@NonNull ServerStatusInfo info) private static JsonObject toJson(ServerStatusInfo info) { JsonObject obj = new JsonObject(); - JsonObject ver = new JsonObject(); - ver.addProperty("name", info.getVersionInfo().getVersionName()); - ver.addProperty("protocol", info.getVersionInfo().getProtocolVersion()); + + obj.add("description", new Gson().fromJson(DefaultComponentSerializer.get().serialize(info.getDescription()), JsonElement.class)); + JsonObject plrs = new JsonObject(); plrs.addProperty("max", info.getPlayerInfo().getMaxPlayers()); plrs.addProperty("online", info.getPlayerInfo().getOnlinePlayers()); @@ -100,13 +103,17 @@ private static JsonObject toJson(ServerStatusInfo info) { plrs.add("sample", array); } - - obj.add("description", new Gson().fromJson(DefaultComponentSerializer.get().serialize(info.getDescription()), JsonElement.class)); obj.add("players", plrs); + + JsonObject ver = new JsonObject(); + ver.addProperty("name", info.getVersionInfo().getVersionName()); + ver.addProperty("protocol", info.getVersionInfo().getProtocolVersion()); obj.add("version", ver); + if (info.getIconPng() != null) { obj.addProperty("favicon", iconToString(info.getIconPng())); } + obj.addProperty("enforcesSecureChat", info.isEnforcesSecureChat()); return obj; From 707d64ccc5626e35d3e5d80653a62acfab6202b2 Mon Sep 17 00:00:00 2001 From: AlexProgrammerDE <40795980+AlexProgrammerDE@users.noreply.github.com> Date: Thu, 23 May 2024 08:11:02 +0200 Subject: [PATCH 2/4] Use proper kyori methods --- .../status/clientbound/ClientboundStatusResponsePacket.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/status/clientbound/ClientboundStatusResponsePacket.java b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/status/clientbound/ClientboundStatusResponsePacket.java index adb1d3448..ad4238865 100644 --- a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/status/clientbound/ClientboundStatusResponsePacket.java +++ b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/status/clientbound/ClientboundStatusResponsePacket.java @@ -48,7 +48,7 @@ public void serialize(ByteBuf out, MinecraftCodecHelper helper) { public ServerStatusInfo parseInfo() { JsonElement desc = data.get("description"); - Component description = DefaultComponentSerializer.get().serializer().fromJson(desc, Component.class); + Component description = DefaultComponentSerializer.get().deserializeFromTree(desc); JsonObject plrs = data.get("players").getAsJsonObject(); List profiles = new ArrayList<>(); @@ -87,7 +87,7 @@ public ClientboundStatusResponsePacket withInfo(@NonNull ServerStatusInfo info) private static JsonObject toJson(ServerStatusInfo info) { JsonObject obj = new JsonObject(); - obj.add("description", new Gson().fromJson(DefaultComponentSerializer.get().serialize(info.getDescription()), JsonElement.class)); + obj.add("description", DefaultComponentSerializer.get().serializeToTree(info.getDescription())); JsonObject plrs = new JsonObject(); plrs.addProperty("max", info.getPlayerInfo().getMaxPlayers()); From 46240991963a878e8cc44bb6caae56995908456b Mon Sep 17 00:00:00 2001 From: AlexProgrammerDE <40795980+AlexProgrammerDE@users.noreply.github.com> Date: Thu, 23 May 2024 08:13:24 +0200 Subject: [PATCH 3/4] Make PlayerInfo and VersionInfo optional --- .../data/status/ServerStatusInfo.java | 5 +- .../ClientboundStatusResponsePacket.java | 68 +++++++++++-------- 2 files changed, 42 insertions(+), 31 deletions(-) diff --git a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/data/status/ServerStatusInfo.java b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/data/status/ServerStatusInfo.java index 55e5c1e42..e1c0f813c 100644 --- a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/data/status/ServerStatusInfo.java +++ b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/data/status/ServerStatusInfo.java @@ -6,13 +6,14 @@ import lombok.NonNull; import lombok.Setter; import net.kyori.adventure.text.Component; +import org.jetbrains.annotations.Nullable; @Data @Setter(AccessLevel.NONE) @AllArgsConstructor public class ServerStatusInfo { - private @NonNull VersionInfo versionInfo; - private @NonNull PlayerInfo playerInfo; + private @Nullable VersionInfo versionInfo; + private @Nullable PlayerInfo playerInfo; private @NonNull Component description; private byte[] iconPng; private boolean enforcesSecureChat; diff --git a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/status/clientbound/ClientboundStatusResponsePacket.java b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/status/clientbound/ClientboundStatusResponsePacket.java index ad4238865..59b369102 100644 --- a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/status/clientbound/ClientboundStatusResponsePacket.java +++ b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/status/clientbound/ClientboundStatusResponsePacket.java @@ -50,22 +50,28 @@ public ServerStatusInfo parseInfo() { JsonElement desc = data.get("description"); Component description = DefaultComponentSerializer.get().deserializeFromTree(desc); - JsonObject plrs = data.get("players").getAsJsonObject(); - List profiles = new ArrayList<>(); - if (plrs.has("sample")) { - JsonArray prof = plrs.get("sample").getAsJsonArray(); - if (prof.size() > 0) { - for (int index = 0; index < prof.size(); index++) { - JsonObject o = prof.get(index).getAsJsonObject(); - profiles.add(new GameProfile(o.get("id").getAsString(), o.get("name").getAsString())); + PlayerInfo players = null; + if (data.has("players")) { + JsonObject plrs = data.get("players").getAsJsonObject(); + List profiles = new ArrayList<>(); + if (plrs.has("sample")) { + JsonArray prof = plrs.get("sample").getAsJsonArray(); + if (prof.size() > 0) { + for (int index = 0; index < prof.size(); index++) { + JsonObject o = prof.get(index).getAsJsonObject(); + profiles.add(new GameProfile(o.get("id").getAsString(), o.get("name").getAsString())); + } } } - } - PlayerInfo players = new PlayerInfo(plrs.get("max").getAsInt(), plrs.get("online").getAsInt(), profiles); + players = new PlayerInfo(plrs.get("max").getAsInt(), plrs.get("online").getAsInt(), profiles); + } - JsonObject ver = data.get("version").getAsJsonObject(); - VersionInfo version = new VersionInfo(ver.get("name").getAsString(), ver.get("protocol").getAsInt()); + VersionInfo version = null; + if (data.has("version")) { + JsonObject ver = data.get("version").getAsJsonObject(); + version = new VersionInfo(ver.get("name").getAsString(), ver.get("protocol").getAsInt()); + } byte[] icon = null; if (data.has("favicon")) { @@ -89,26 +95,30 @@ private static JsonObject toJson(ServerStatusInfo info) { obj.add("description", DefaultComponentSerializer.get().serializeToTree(info.getDescription())); - JsonObject plrs = new JsonObject(); - plrs.addProperty("max", info.getPlayerInfo().getMaxPlayers()); - plrs.addProperty("online", info.getPlayerInfo().getOnlinePlayers()); - if (!info.getPlayerInfo().getPlayers().isEmpty()) { - JsonArray array = new JsonArray(); - for (GameProfile profile : info.getPlayerInfo().getPlayers()) { - JsonObject o = new JsonObject(); - o.addProperty("name", profile.getName()); - o.addProperty("id", profile.getIdAsString()); - array.add(o); - } + if (info.getPlayerInfo() != null) { + JsonObject plrs = new JsonObject(); + plrs.addProperty("max", info.getPlayerInfo().getMaxPlayers()); + plrs.addProperty("online", info.getPlayerInfo().getOnlinePlayers()); + if (!info.getPlayerInfo().getPlayers().isEmpty()) { + JsonArray array = new JsonArray(); + for (GameProfile profile : info.getPlayerInfo().getPlayers()) { + JsonObject o = new JsonObject(); + o.addProperty("name", profile.getName()); + o.addProperty("id", profile.getIdAsString()); + array.add(o); + } - plrs.add("sample", array); + plrs.add("sample", array); + } + obj.add("players", plrs); } - obj.add("players", plrs); - JsonObject ver = new JsonObject(); - ver.addProperty("name", info.getVersionInfo().getVersionName()); - ver.addProperty("protocol", info.getVersionInfo().getProtocolVersion()); - obj.add("version", ver); + if (info.getVersionInfo() != null) { + JsonObject ver = new JsonObject(); + ver.addProperty("name", info.getVersionInfo().getVersionName()); + ver.addProperty("protocol", info.getVersionInfo().getProtocolVersion()); + obj.add("version", ver); + } if (info.getIconPng() != null) { obj.addProperty("favicon", iconToString(info.getIconPng())); From 0fefe044a56fdf44f3208f8edeb2038295062703 Mon Sep 17 00:00:00 2001 From: AlexProgrammerDE <40795980+AlexProgrammerDE@users.noreply.github.com> Date: Thu, 23 May 2024 08:16:16 +0200 Subject: [PATCH 4/4] Sort ServerStatusInfo fields in the way they appear in the packet --- .../protocol/example/MinecraftProtocolTest.java | 4 ++-- .../org/geysermc/mcprotocollib/protocol/ServerListener.java | 4 ++-- .../protocol/data/status/ServerStatusInfo.java | 4 ++-- .../status/clientbound/ClientboundStatusResponsePacket.java | 2 +- .../mcprotocollib/protocol/MinecraftProtocolTest.java | 4 ++-- .../clientbound/ClientboundStatusResponsePacketTest.java | 6 +++--- 6 files changed, 12 insertions(+), 12 deletions(-) diff --git a/example/src/main/java/org/geysermc/mcprotocollib/protocol/example/MinecraftProtocolTest.java b/example/src/main/java/org/geysermc/mcprotocollib/protocol/example/MinecraftProtocolTest.java index db88bcae7..277e5e8d3 100644 --- a/example/src/main/java/org/geysermc/mcprotocollib/protocol/example/MinecraftProtocolTest.java +++ b/example/src/main/java/org/geysermc/mcprotocollib/protocol/example/MinecraftProtocolTest.java @@ -61,9 +61,9 @@ public static void main(String[] args) { server.setGlobalFlag(MinecraftConstants.VERIFY_USERS_KEY, VERIFY_USERS); server.setGlobalFlag(MinecraftConstants.SERVER_INFO_BUILDER_KEY, session -> new ServerStatusInfo( - new VersionInfo(MinecraftCodec.CODEC.getMinecraftVersion(), MinecraftCodec.CODEC.getProtocolVersion()), - new PlayerInfo(100, 0, new ArrayList<>()), Component.text("Hello world!"), + new PlayerInfo(100, 0, new ArrayList<>()), + new VersionInfo(MinecraftCodec.CODEC.getMinecraftVersion(), MinecraftCodec.CODEC.getProtocolVersion()), null, false ) diff --git a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/ServerListener.java b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/ServerListener.java index 3d72d0db0..771b6cd65 100644 --- a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/ServerListener.java +++ b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/ServerListener.java @@ -156,9 +156,9 @@ public void packetReceived(Session session, Packet packet) { ServerInfoBuilder builder = session.getFlag(MinecraftConstants.SERVER_INFO_BUILDER_KEY); if (builder == null) { builder = $ -> new ServerStatusInfo( - new VersionInfo(protocol.getCodec().getMinecraftVersion(), protocol.getCodec().getProtocolVersion()), - new PlayerInfo(0, 20, new ArrayList<>()), Component.text("A Minecraft Server"), + new PlayerInfo(0, 20, new ArrayList<>()), + new VersionInfo(protocol.getCodec().getMinecraftVersion(), protocol.getCodec().getProtocolVersion()), null, false ); diff --git a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/data/status/ServerStatusInfo.java b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/data/status/ServerStatusInfo.java index e1c0f813c..73556925f 100644 --- a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/data/status/ServerStatusInfo.java +++ b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/data/status/ServerStatusInfo.java @@ -12,9 +12,9 @@ @Setter(AccessLevel.NONE) @AllArgsConstructor public class ServerStatusInfo { - private @Nullable VersionInfo versionInfo; - private @Nullable PlayerInfo playerInfo; private @NonNull Component description; + private @Nullable PlayerInfo playerInfo; + private @Nullable VersionInfo versionInfo; private byte[] iconPng; private boolean enforcesSecureChat; } diff --git a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/status/clientbound/ClientboundStatusResponsePacket.java b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/status/clientbound/ClientboundStatusResponsePacket.java index 59b369102..5d25a89b1 100644 --- a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/status/clientbound/ClientboundStatusResponsePacket.java +++ b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/status/clientbound/ClientboundStatusResponsePacket.java @@ -83,7 +83,7 @@ public ServerStatusInfo parseInfo() { enforcesSecureChat = data.get("enforcesSecureChat").getAsBoolean(); } - return new ServerStatusInfo(version, players, description, icon, enforcesSecureChat); + return new ServerStatusInfo(description, players, version, icon, enforcesSecureChat); } public ClientboundStatusResponsePacket withInfo(@NonNull ServerStatusInfo info) { diff --git a/protocol/src/test/java/org/geysermc/mcprotocollib/protocol/MinecraftProtocolTest.java b/protocol/src/test/java/org/geysermc/mcprotocollib/protocol/MinecraftProtocolTest.java index 0578034dc..bd6b2e7b0 100644 --- a/protocol/src/test/java/org/geysermc/mcprotocollib/protocol/MinecraftProtocolTest.java +++ b/protocol/src/test/java/org/geysermc/mcprotocollib/protocol/MinecraftProtocolTest.java @@ -33,9 +33,9 @@ public class MinecraftProtocolTest { private static final int PORT = 25562; private static final ServerStatusInfo SERVER_INFO = new ServerStatusInfo( - new VersionInfo(MinecraftCodec.CODEC.getMinecraftVersion(), MinecraftCodec.CODEC.getProtocolVersion()), - new PlayerInfo(100, 0, new ArrayList<>()), Component.text("Hello world!"), + new PlayerInfo(100, 0, new ArrayList<>()), + new VersionInfo(MinecraftCodec.CODEC.getMinecraftVersion(), MinecraftCodec.CODEC.getProtocolVersion()), null, false ); diff --git a/protocol/src/test/java/org/geysermc/mcprotocollib/protocol/packet/status/clientbound/ClientboundStatusResponsePacketTest.java b/protocol/src/test/java/org/geysermc/mcprotocollib/protocol/packet/status/clientbound/ClientboundStatusResponsePacketTest.java index 004d78299..a211cc340 100644 --- a/protocol/src/test/java/org/geysermc/mcprotocollib/protocol/packet/status/clientbound/ClientboundStatusResponsePacketTest.java +++ b/protocol/src/test/java/org/geysermc/mcprotocollib/protocol/packet/status/clientbound/ClientboundStatusResponsePacketTest.java @@ -18,11 +18,11 @@ public class ClientboundStatusResponsePacketTest extends PacketTest { public void setup() { this.setPackets(new ClientboundStatusResponsePacket( new ServerStatusInfo( - new VersionInfo(MinecraftCodec.CODEC.getMinecraftVersion(), MinecraftCodec.CODEC.getProtocolVersion()), + Component.text("Description"), new PlayerInfo(100, 10, new ArrayList<>( - Collections.singleton(new GameProfile(UUID.randomUUID(), "Username")) + Collections.singleton(new GameProfile(UUID.randomUUID(), "Username")) )), - Component.text("Description"), + new VersionInfo(MinecraftCodec.CODEC.getMinecraftVersion(), MinecraftCodec.CODEC.getProtocolVersion()), null, false )