minecraft-source/src/com/mojang/realmsclient/dto/RealmsServerPlayerList.java

60 lines
2.0 KiB
Java
Raw Normal View History

2020-07-22 06:23:34 +01:00
package com.mojang.realmsclient.dto;
import org.apache.logging.log4j.LogManager;
import java.util.Iterator;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
2020-07-22 06:25:47 +01:00
import com.google.common.collect.Lists;
2020-07-22 06:23:34 +01:00
import com.mojang.realmsclient.util.JsonUtils;
import com.google.gson.JsonObject;
import java.util.List;
import com.google.gson.JsonParser;
import org.apache.logging.log4j.Logger;
public class RealmsServerPlayerList extends ValueObject {
private static final Logger LOGGER;
2020-07-22 06:32:50 +01:00
private static final JsonParser JSON_PARSER;
2020-07-22 06:23:34 +01:00
public long serverId;
public List<String> players;
public static RealmsServerPlayerList parse(final JsonObject jsonObject) {
2020-07-22 06:32:50 +01:00
final RealmsServerPlayerList dit2 = new RealmsServerPlayerList();
2020-07-22 06:23:34 +01:00
try {
2020-07-22 06:32:50 +01:00
dit2.serverId = JsonUtils.getLongOr("serverId", jsonObject, -1L);
2020-07-22 06:23:34 +01:00
final String string3 = JsonUtils.getStringOr("playerList", jsonObject, null);
if (string3 != null) {
2020-07-22 06:32:50 +01:00
final JsonElement jsonElement4 = RealmsServerPlayerList.JSON_PARSER.parse(string3);
2020-07-22 06:23:34 +01:00
if (jsonElement4.isJsonArray()) {
2020-07-22 06:32:50 +01:00
dit2.players = parsePlayers(jsonElement4.getAsJsonArray());
2020-07-22 06:23:34 +01:00
}
else {
2020-07-22 06:32:50 +01:00
dit2.players = Lists.newArrayList();
2020-07-22 06:23:34 +01:00
}
}
else {
2020-07-22 06:32:50 +01:00
dit2.players = Lists.newArrayList();
2020-07-22 06:23:34 +01:00
}
}
catch (Exception exception3) {
RealmsServerPlayerList.LOGGER.error("Could not parse RealmsServerPlayerList: " + exception3.getMessage());
}
2020-07-22 06:32:50 +01:00
return dit2;
2020-07-22 06:23:34 +01:00
}
private static List<String> parsePlayers(final JsonArray jsonArray) {
2020-07-22 06:25:47 +01:00
final List<String> list2 = Lists.newArrayList();
2020-07-22 06:23:34 +01:00
for (final JsonElement jsonElement4 : jsonArray) {
try {
2020-07-22 06:25:47 +01:00
list2.add(jsonElement4.getAsString());
2020-07-22 06:23:34 +01:00
}
catch (Exception ex) {}
}
2020-07-22 06:25:47 +01:00
return list2;
2020-07-22 06:23:34 +01:00
}
static {
LOGGER = LogManager.getLogger();
2020-07-22 06:32:50 +01:00
JSON_PARSER = new JsonParser();
2020-07-22 06:23:34 +01:00
}
}