| 1 | package com.mojang.realmsclient.dto;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableMap;
|
| 4 | import com.google.common.collect.Lists;
|
| 5 | import com.google.common.collect.ImmutableMap.Builder;
|
| 6 | import com.google.gson.JsonArray;
|
| 7 | import com.google.gson.JsonElement;
|
| 8 | import com.google.gson.JsonObject;
|
| 9 | import com.mojang.logging.LogUtils;
|
| 10 | import com.mojang.realmsclient.util.JsonUtils;
|
| 11 | import java.util.ArrayList;
|
| 12 | import java.util.List;
|
| 13 | import java.util.Map;
|
| 14 | import java.util.UUID;
|
| 15 | import net.fabricmc.api.EnvType;
|
| 16 | import net.fabricmc.api.Environment;
|
| 17 | import net.minecraft.client.Minecraft;
|
| 18 | import net.minecraft.util.GsonHelper;
|
| 19 | import net.minecraft.util.LenientJsonParser;
|
| 20 | import net.minecraft.world.item.component.ResolvableProfile;
|
| 21 | import org.slf4j.Logger;
|
| 22 |
|
| 23 | @Environment(EnvType.CLIENT)
|
| 24 | public record RealmsServerPlayerLists(Map<Long, List<ResolvableProfile>> servers) {
|
| 25 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 26 |
|
| 27 | public static RealmsServerPlayerLists parse(final String json) {
|
| 28 | Builder<Long, List<ResolvableProfile>> elements = ImmutableMap.builder();
|
| 29 |
|
| 30 | try {
|
| 31 | JsonObject object = GsonHelper.parse(json);
|
| 32 | if (GsonHelper.isArrayNode(object, "lists")) {
|
| 33 | for (JsonElement jsonElement : object.getAsJsonArray("lists")) {
|
| 34 | JsonObject node = jsonElement.getAsJsonObject();
|
| 35 | String playerListString = JsonUtils.getStringOr("playerList", node, null);
|
| 36 | List<ResolvableProfile> players;
|
| 37 | if (playerListString != null) {
|
| 38 | JsonElement element = LenientJsonParser.parse(playerListString);
|
| 39 | if (element.isJsonArray()) {
|
| 40 | players = parsePlayers(element.getAsJsonArray());
|
| 41 | } else {
|
| 42 | players = Lists.newArrayList();
|
| 43 | }
|
| 44 | } else {
|
| 45 | players = Lists.newArrayList();
|
| 46 | }
|
| 47 |
|
| 48 | elements.put(JsonUtils.getLongOr("serverId", node, -1L), players);
|
| 49 | }
|
| 50 | }
|
| 51 | } catch (Exception e) {
|
| 52 | LOGGER.error("Could not parse RealmsServerPlayerLists", e);
|
| 53 | }
|
| 54 |
|
| 55 | return new RealmsServerPlayerLists(elements.build());
|
| 56 | }
|
| 57 |
|
| 58 | private static List<ResolvableProfile> parsePlayers(final JsonArray array) {
|
| 59 | List<ResolvableProfile> profiles = new ArrayList<>(array.size());
|
| 60 |
|
| 61 | for (JsonElement element : array) {
|
| 62 | if (element.isJsonObject()) {
|
| 63 | UUID playerId = JsonUtils.getUuidOr("playerId", element.getAsJsonObject(), null);
|
| 64 | if (playerId != null && !Minecraft.getInstance().isLocalPlayer(playerId)) {
|
| 65 | profiles.add(ResolvableProfile.createUnresolved(playerId));
|
| 66 | }
|
| 67 | }
|
| 68 | }
|
| 69 |
|
| 70 | return profiles;
|
| 71 | }
|
| 72 |
|
| 73 | public List<ResolvableProfile> getProfileResultsFor(final long serverId) {
|
| 74 | List<ResolvableProfile> profileResults = this.servers.get(serverId);
|
| 75 | return profileResults != null ? profileResults : List.of();
|
| 76 | }
|
| 77 | }
|
| 78 |
|