Skip to content

Commit

Permalink
Handle not-set realms properties correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
RaphiMC committed Apr 28, 2024
1 parent 9f05503 commit 0148f14
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import lombok.Value;
import net.raphimc.minecraftauth.util.JsonUtil;

import java.util.UUID;

@Value
public class RealmsWorld {

Expand All @@ -39,15 +41,15 @@ public class RealmsWorld {

public static RealmsWorld fromJson(final JsonObject json) {
return new RealmsWorld(
json.get("id").getAsLong(),
JsonUtil.getStringOr(json, "owner", ""),
json.get("ownerUUID").getAsString(),
json.get("name").getAsString(),
json.get("motd").getAsString(),
json.get("state").getAsString(),
json.get("expired").getAsBoolean(),
json.get("worldType").getAsString(),
json.get("maxPlayers").getAsInt(),
JsonUtil.getLongOr(json, "id", -1L),
JsonUtil.getStringOr(json, "owner", null),
JsonUtil.getStringOr(json, "ownerUUID", new UUID(0L, 0L).toString()),
JsonUtil.getStringOr(json, "name", null),
JsonUtil.getStringOr(json, "motd", null),
JsonUtil.getStringOr(json, "state", "CLOSED"),
JsonUtil.getBooleanOr(json, "expired", false),
JsonUtil.getStringOr(json, "worldType", "NORMAL"),
JsonUtil.getIntOr(json, "maxPlayers", 0),
JsonUtil.getStringOr(json, "compatibility", "COMPATIBLE").equals("COMPATIBLE"),
JsonUtil.getStringOr(json, "activeVersion", ""),
json
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/net/raphimc/minecraftauth/util/JsonUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ public static String getStringOr(final JsonObject obj, final String key, final S
}
}

public static boolean getBooleanOr(final JsonObject obj, final String key, final boolean defaultValue) {
final JsonElement element = obj.get(key);
if (element != null && !element.isJsonNull()) {
return element.getAsBoolean();
} else {
return defaultValue;
}
}

public static int getIntOr(final JsonObject obj, final String key, final int defaultValue) {
final JsonElement element = obj.get(key);
if (element != null && !element.isJsonNull()) {
Expand Down

0 comments on commit 0148f14

Please sign in to comment.