Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix optional status fields #821

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,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
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,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
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
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 @NonNull Component description;
private @Nullable PlayerInfo playerInfo;
private @Nullable VersionInfo versionInfo;
private byte[] iconPng;
private boolean enforcesSecureChat;
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,31 @@ 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<GameProfile> 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()));
Component description = DefaultComponentSerializer.get().deserializeFromTree(desc);

PlayerInfo players = null;
if (data.has("players")) {
JsonObject plrs = data.get("players").getAsJsonObject();
List<GameProfile> 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()));
}
}
}

players = new PlayerInfo(plrs.get("max").getAsInt(), plrs.get("online").getAsInt(), profiles);
}

VersionInfo version = null;
if (data.has("version")) {
JsonObject ver = data.get("version").getAsJsonObject();
version = new VersionInfo(ver.get("name").getAsString(), ver.get("protocol").getAsInt());
}

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());
Expand All @@ -74,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) {
Expand All @@ -83,30 +92,38 @@ 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());
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);

obj.add("description", DefaultComponentSerializer.get().serializeToTree(info.getDescription()));

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);
}
obj.add("players", plrs);
}

plrs.add("sample", array);
if (info.getVersionInfo() != null) {
JsonObject ver = new JsonObject();
ver.addProperty("name", info.getVersionInfo().getVersionName());
ver.addProperty("protocol", info.getVersionInfo().getProtocolVersion());
obj.add("version", ver);
}

obj.add("description", new Gson().fromJson(DefaultComponentSerializer.get().serialize(info.getDescription()), JsonElement.class));
obj.add("players", plrs);
obj.add("version", ver);
if (info.getIconPng() != null) {
obj.addProperty("favicon", iconToString(info.getIconPng()));
}

obj.addProperty("enforcesSecureChat", info.isEnforcesSecureChat());

return obj;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,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
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
Loading