Skip to content

Commit

Permalink
Make PlayerInfo and VersionInfo optional
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexProgrammerDE committed May 23, 2024
1 parent 707d64c commit 4624099
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<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()));
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()));
}
}
}
}

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")) {
Expand All @@ -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()));
Expand Down

0 comments on commit 4624099

Please sign in to comment.