package com.mojang.realmsclient.dto; import com.google.common.collect.ImmutableMap; import com.google.common.collect.Lists; import com.google.common.collect.ImmutableMap.Builder; import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.mojang.logging.LogUtils; import com.mojang.realmsclient.util.JsonUtils; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.UUID; import net.minecraft.client.Minecraft; import net.minecraft.util.GsonHelper; import net.minecraft.util.LenientJsonParser; import net.minecraft.world.item.component.ResolvableProfile; import org.slf4j.Logger; public record RealmsServerPlayerLists(Map> servers) { private static final Logger LOGGER = LogUtils.getLogger(); public static RealmsServerPlayerLists parse(final String json) { Builder> elements = ImmutableMap.builder(); try { JsonObject object = GsonHelper.parse(json); if (GsonHelper.isArrayNode(object, "lists")) { for (JsonElement jsonElement : object.getAsJsonArray("lists")) { JsonObject node = jsonElement.getAsJsonObject(); String playerListString = JsonUtils.getStringOr("playerList", node, null); List players; if (playerListString != null) { JsonElement element = LenientJsonParser.parse(playerListString); if (element.isJsonArray()) { players = parsePlayers(element.getAsJsonArray()); } else { players = Lists.newArrayList(); } } else { players = Lists.newArrayList(); } elements.put(JsonUtils.getLongOr("serverId", node, -1L), players); } } } catch (Exception var10) { LOGGER.error("Could not parse RealmsServerPlayerLists", (Throwable)var10); } return new RealmsServerPlayerLists(elements.build()); } private static List parsePlayers(final JsonArray array) { List profiles = new ArrayList(array.size()); for (JsonElement element : array) { if (element.isJsonObject()) { UUID playerId = JsonUtils.getUuidOr("playerId", element.getAsJsonObject(), null); if (playerId != null && !Minecraft.getInstance().isLocalPlayer(playerId)) { profiles.add(ResolvableProfile.createUnresolved(playerId)); } } } return profiles; } public List getProfileResultsFor(final long serverId) { List profileResults = (List)this.servers.get(serverId); return profileResults != null ? profileResults : List.of(); } }