InviteCodeList .java

com.mojang.realmsclient.dto.InviteCodeList (record, 50 lines): members: inviteCodes, getInviteCodesArray, parse, LOGGER. strings: "result".

com.mojang.realmsclient.dtorecord50 linescommon1.4 KB

API Surface (2)

record InviteCodeList(List<InviteCode> inviteCodes)
InviteCodeList parse(final String json)

Fields & Constants (1)

LOGGER

String literals (2)

"Could not parse InviteCodeList"
"result"

Code Graph: Dependencies (2)

Edges (calls / imports / inherits / references) resolved by graphify from the AST. Every chip opens the class source.

Full Source

InviteCodeList.java50 lines · Minecraft 26.3 · verbatim
1
package com.mojang.realmsclient.dto;
2
3
import com.google.gson.JsonArray;
4
import com.google.gson.JsonElement;
5
import com.google.gson.JsonObject;
6
import com.mojang.logging.LogUtils;
7
import java.util.ArrayList;
8
import java.util.List;
9
import net.fabricmc.api.EnvType;
10
import net.fabricmc.api.Environment;
11
import net.minecraft.util.LenientJsonParser;
12
import org.slf4j.Logger;
13
14
@Environment(EnvType.CLIENT)
15
public record InviteCodeList(List<InviteCode> inviteCodes) {
16
	private static final Logger LOGGER = LogUtils.getLogger();
17
18
	public static InviteCodeList parse(final String json) {
19
		List<InviteCode> codes = new ArrayList<>();
20
21
		try {
22
			JsonElement root = LenientJsonParser.parse(json);
23
24
			for (JsonElement element : getInviteCodesArray(root)) {
25
				if (!element.isJsonObject()) {
26
					LOGGER.error("Invite code list contained a non-object entry: {}", element);
27
				} else {
28
					InviteCode code = InviteCode.parse(element.getAsJsonObject());
29
					if (code != null) {
30
						codes.add(code);
31
					}
32
				}
33
			}
34
		} catch (Exception e) {
35
			LOGGER.error("Could not parse InviteCodeList", e);
36
		}
37
38
		return new InviteCodeList(List.copyOf(codes));
39
	}
40
41
	private static JsonArray getInviteCodesArray(final JsonElement root) {
42
		if (root.isJsonArray()) {
43
			return root.getAsJsonArray();
44
		}
45
46
		JsonObject object = root.getAsJsonObject();
47
		return object.has("result") && object.get("result").isJsonArray() ? object.getAsJsonArray("result") : new JsonArray();
48
	}
49
}
50

Decompiled Minecraft 26.3 source of InviteCodeList (com/mojang/realmsclient/dto/InviteCodeList.java) with Mojang official mappings, stored verbatim on the site.