RealmsServer .java

com.mojang.realmsclient.dto.RealmsServer (class, 362 lines): members: shouldPlayButtonBeActive, WORLD_CLOSED_COMPONENT, remoteSubscriptionId, expired.

com.mojang.realmsclient.dtoclass362 linescommonextends ValueObject10.5 KB

Implements

API Surface (22)

String getDescription()
void setName(final String name)
void setDescription(final String motd)
RealmsServer parse(final GuardedSerializer gson, final String json)
void finalize(final RealmsServer server)
boolean isCompatible()
boolean needsUpgrade()
boolean needsDowngrade()
boolean shouldPlayButtonBeActive()
int hashCode()
boolean equals(final Object obj)
RealmsServer copy()
Map<Integer, RealmsSlot> cloneSlots(final Map<Integer, RealmsSlot> slots)
boolean isSnapshotRealm()
boolean isMinigameActive()
String getWorldName(final int slotId)
ServerData toServerData(final String ip)
boolean isCompatible()
boolean needsUpgrade()
boolean needsDowngrade()
int compare(final RealmsServer server1, final RealmsServer server2)
Component getDisplayName()

Fields & Constants (28)

LOGGER
NO_VALUE
WORLD_CLOSED_COMPONENT
id
remoteSubscriptionId
name
motd
state
owner
ownerUUID
players
slotList
slots
expired
expiredTrial
daysLeft
worldType
isHardcore
gameMode
activeSlot
minigameName
minigameId
minigameImage
parentRealmId
parentWorldName
activeVersion
compatibility

…and 1 more (see full source).

String literals (32)

"mco.play.button.realm.closed"
"remoteSubscriptionId"
"motd"
"state"
"owner"
"ownerUUID"
"players"
"slots"
"expired"
"expiredTrial"
"daysLeft"
"worldType"
"isHardcore"
"gameMode"
"activeSlot"
"minigameName"
"minigameId"
"minigameImage"
"parentWorldId"
"parentWorldName"
"activeVersion"
"compatibility"
"regionSelectionPreference"
"Could not parse McoServer"

Full Source

RealmsServer.java362 lines · Minecraft 26.3 · verbatim
1
package com.mojang.realmsclient.dto;
2
3
import com.google.common.collect.ComparisonChain;
4
import com.google.common.collect.Lists;
5
import com.google.common.collect.Maps;
6
import com.google.gson.annotations.JsonAdapter;
7
import com.google.gson.annotations.SerializedName;
8
import com.mojang.logging.LogUtils;
9
import com.mojang.util.UUIDTypeAdapter;
10
import java.util.ArrayList;
11
import java.util.Comparator;
12
import java.util.HashMap;
13
import java.util.List;
14
import java.util.Locale;
15
import java.util.Map;
16
import java.util.Objects;
17
import java.util.UUID;
18
import java.util.Map.Entry;
19
import net.fabricmc.api.EnvType;
20
import net.fabricmc.api.Environment;
21
import net.minecraft.client.Minecraft;
22
import net.minecraft.client.multiplayer.ServerData;
23
import net.minecraft.network.chat.Component;
24
import net.minecraft.util.Util;
25
import org.apache.commons.lang3.builder.EqualsBuilder;
26
import org.jspecify.annotations.Nullable;
27
import org.slf4j.Logger;
28
29
@Environment(EnvType.CLIENT)
30
public class RealmsServer extends ValueObject implements ReflectionBasedSerialization {
31
	private static final Logger LOGGER = LogUtils.getLogger();
32
	private static final int NO_VALUE = -1;
33
	public static final Component WORLD_CLOSED_COMPONENT = Component.translatable("mco.play.button.realm.closed");
34
	@SerializedName("id")
35
	public long id = -1L;
36
	@SerializedName("remoteSubscriptionId")
37
	public @Nullable String remoteSubscriptionId;
38
	@SerializedName("name")
39
	public @Nullable String name;
40
	@SerializedName("motd")
41
	public String motd = "";
42
	@SerializedName("state")
43
	public RealmsServer.State state = RealmsServer.State.CLOSED;
44
	@SerializedName("owner")
45
	public @Nullable String owner;
46
	@SerializedName("ownerUUID")
47
	@JsonAdapter(UUIDTypeAdapter.class)
48
	public UUID ownerUUID = Util.NIL_UUID;
49
	@SerializedName("players")
50
	public List<PlayerInfo> players = Lists.newArrayList();
51
	@SerializedName("slots")
52
	private List<RealmsSlot> slotList = createEmptySlots();
53
	@Exclude
54
	public Map<Integer, RealmsSlot> slots = new HashMap<>();
55
	@SerializedName("expired")
56
	public boolean expired;
57
	@SerializedName("expiredTrial")
58
	public boolean expiredTrial = false;
59
	@SerializedName("daysLeft")
60
	public int daysLeft;
61
	@SerializedName("worldType")
62
	public RealmsServer.WorldType worldType = RealmsServer.WorldType.NORMAL;
63
	@SerializedName("isHardcore")
64
	public boolean isHardcore = false;
65
	@SerializedName("gameMode")
66
	public int gameMode = -1;
67
	@SerializedName("activeSlot")
68
	public int activeSlot = -1;
69
	@SerializedName("minigameName")
70
	public @Nullable String minigameName;
71
	@SerializedName("minigameId")
72
	public int minigameId = -1;
73
	@SerializedName("minigameImage")
74
	public @Nullable String minigameImage;
75
	@SerializedName("parentWorldId")
76
	public long parentRealmId = -1L;
77
	@SerializedName("parentWorldName")
78
	public @Nullable String parentWorldName;
79
	@SerializedName("activeVersion")
80
	public String activeVersion = "";
81
	@SerializedName("compatibility")
82
	public RealmsServer.Compatibility compatibility = RealmsServer.Compatibility.UNVERIFIABLE;
83
	@SerializedName("regionSelectionPreference")
84
	public @Nullable RegionSelectionPreferenceDto regionSelectionPreference;
85
86
	public String getDescription() {
87
		return this.motd;
88
	}
89
90
	public @Nullable String getName() {
91
		return this.name;
92
	}
93
94
	public @Nullable String getMinigameName() {
95
		return this.minigameName;
96
	}
97
98
	public void setName(final String name) {
99
		this.name = name;
100
	}
101
102
	public void setDescription(final String motd) {
103
		this.motd = motd;
104
	}
105
106
	public static RealmsServer parse(final GuardedSerializer gson, final String json) {
107
		try {
108
			RealmsServer server = gson.fromJson(json, RealmsServer.class);
109
			if (server == null) {
110
				LOGGER.error("Could not parse McoServer: {}", json);
111
				return new RealmsServer();
112
			} else {
113
				finalize(server);
114
				return server;
115
			}
116
		} catch (Exception e) {
117
			LOGGER.error("Could not parse McoServer", e);
118
			return new RealmsServer();
119
		}
120
	}
121
122
	public static void finalize(final RealmsServer server) {
123
		if (server.players == null) {
124
			server.players = Lists.newArrayList();
125
		}
126
127
		if (server.slotList == null) {
128
			server.slotList = createEmptySlots();
129
		}
130
131
		if (server.slots == null) {
132
			server.slots = new HashMap<>();
133
		}
134
135
		if (server.worldType == null) {
136
			server.worldType = RealmsServer.WorldType.NORMAL;
137
		}
138
139
		if (server.activeVersion == null) {
140
			server.activeVersion = "";
141
		}
142
143
		if (server.compatibility == null) {
144
			server.compatibility = RealmsServer.Compatibility.UNVERIFIABLE;
145
		}
146
147
		if (server.regionSelectionPreference == null) {
148
			server.regionSelectionPreference = RegionSelectionPreferenceDto.DEFAULT;
149
		}
150
151
		sortInvited(server);
152
		finalizeSlots(server);
153
	}
154
155
	private static void sortInvited(final RealmsServer server) {
156
		server.players
157
			.sort(
158
				(o1, o2) -> ComparisonChain.start()
159
					.compareFalseFirst(o2.accepted, o1.accepted)
160
					.compare(o1.name.toLowerCase(Locale.ROOT), o2.name.toLowerCase(Locale.ROOT))
161
					.result()
162
			);
163
	}
164
165
	private static void finalizeSlots(final RealmsServer server) {
166
		server.slotList.forEach(s -> server.slots.put(s.slotId, s));
167
168
		for (int i = 1; i <= 3; i++) {
169
			if (!server.slots.containsKey(i)) {
170
				server.slots.put(i, RealmsSlot.defaults(i));
171
			}
172
		}
173
	}
174
175
	private static List<RealmsSlot> createEmptySlots() {
176
		List<RealmsSlot> slots = new ArrayList<>();
177
		slots.add(RealmsSlot.defaults(1));
178
		slots.add(RealmsSlot.defaults(2));
179
		slots.add(RealmsSlot.defaults(3));
180
		return slots;
181
	}
182
183
	public boolean isCompatible() {
184
		return this.compatibility.isCompatible();
185
	}
186
187
	public boolean needsUpgrade() {
188
		return this.compatibility.needsUpgrade();
189
	}
190
191
	public boolean needsDowngrade() {
192
		return this.compatibility.needsDowngrade();
193
	}
194
195
	public boolean shouldPlayButtonBeActive() {
196
		boolean active = !this.expired && this.state == RealmsServer.State.OPEN;
197
		return active && (this.isCompatible() || this.needsUpgrade() || this.isSelfOwnedServer());
198
	}
199
200
	private boolean isSelfOwnedServer() {
201
		return Minecraft.getInstance().isLocalPlayer(this.ownerUUID);
202
	}
203
204
	@Override
205
	public int hashCode() {
206
		return Objects.hash(this.id, this.name, this.motd, this.state, this.owner, this.expired);
207
	}
208
209
	@Override
210
	public boolean equals(final Object obj) {
211
		if (obj == null) {
212
			return false;
213
		}
214
215
		if (obj == this) {
216
			return true;
217
		}
218
219
		if (obj.getClass() != this.getClass()) {
220
			return false;
221
		}
222
223
		RealmsServer rhs = (RealmsServer)obj;
224
		return new EqualsBuilder()
225
			.append(this.id, rhs.id)
226
			.append(this.name, rhs.name)
227
			.append(this.motd, rhs.motd)
228
			.append(this.state, rhs.state)
229
			.append(this.owner, rhs.owner)
230
			.append(this.expired, rhs.expired)
231
			.append(this.worldType, this.worldType)
232
			.isEquals();
233
	}
234
235
	public RealmsServer copy() {
236
		RealmsServer server = new RealmsServer();
237
		server.id = this.id;
238
		server.remoteSubscriptionId = this.remoteSubscriptionId;
239
		server.name = this.name;
240
		server.motd = this.motd;
241
		server.state = this.state;
242
		server.owner = this.owner;
243
		server.players = this.players;
244
		server.slotList = this.slotList.stream().map(RealmsSlot::copy).toList();
245
		server.slots = this.cloneSlots(this.slots);
246
		server.expired = this.expired;
247
		server.expiredTrial = this.expiredTrial;
248
		server.daysLeft = this.daysLeft;
249
		server.worldType = this.worldType;
250
		server.isHardcore = this.isHardcore;
251
		server.gameMode = this.gameMode;
252
		server.ownerUUID = this.ownerUUID;
253
		server.minigameName = this.minigameName;
254
		server.activeSlot = this.activeSlot;
255
		server.minigameId = this.minigameId;
256
		server.minigameImage = this.minigameImage;
257
		server.parentWorldName = this.parentWorldName;
258
		server.parentRealmId = this.parentRealmId;
259
		server.activeVersion = this.activeVersion;
260
		server.compatibility = this.compatibility;
261
		server.regionSelectionPreference = this.regionSelectionPreference != null ? this.regionSelectionPreference.copy() : null;
262
		return server;
263
	}
264
265
	public Map<Integer, RealmsSlot> cloneSlots(final Map<Integer, RealmsSlot> slots) {
266
		Map<Integer, RealmsSlot> newSlots = Maps.newHashMap();
267
268
		for (Entry<Integer, RealmsSlot> entry : slots.entrySet()) {
269
			newSlots.put(entry.getKey(), new RealmsSlot(entry.getKey(), entry.getValue().options.copy(), entry.getValue().settings));
270
		}
271
272
		return newSlots;
273
	}
274
275
	public boolean isSnapshotRealm() {
276
		return this.parentRealmId != -1L;
277
	}
278
279
	public boolean isMinigameActive() {
280
		return this.worldType == RealmsServer.WorldType.MINIGAME;
281
	}
282
283
	public String getWorldName(final int slotId) {
284
		return this.name == null ? this.slots.get(slotId).options.getSlotName(slotId) : this.name + " (" + this.slots.get(slotId).options.getSlotName(slotId) + ")";
285
	}
286
287
	public ServerData toServerData(final String ip) {
288
		return new ServerData(Objects.requireNonNullElse(this.name, "unknown server"), ip, ServerData.Type.REALM);
289
	}
290
291
	@Environment(EnvType.CLIENT)
292
	public enum Compatibility {
293
		UNVERIFIABLE,
294
		INCOMPATIBLE,
295
		RELEASE_TYPE_INCOMPATIBLE,
296
		NEEDS_DOWNGRADE,
297
		NEEDS_UPGRADE,
298
		COMPATIBLE;
299
300
		public boolean isCompatible() {
301
			return this == COMPATIBLE;
302
		}
303
304
		public boolean needsUpgrade() {
305
			return this == NEEDS_UPGRADE;
306
		}
307
308
		public boolean needsDowngrade() {
309
			return this == NEEDS_DOWNGRADE;
310
		}
311
	}
312
313
	@Environment(EnvType.CLIENT)
314
	public static class McoServerComparator implements Comparator<RealmsServer> {
315
		private final String refOwner;
316
317
		public McoServerComparator(final String owner) {
318
			this.refOwner = owner;
319
		}
320
321
		public int compare(final RealmsServer server1, final RealmsServer server2) {
322
			return ComparisonChain.start()
323
				.compareTrueFirst(server1.isSnapshotRealm(), server2.isSnapshotRealm())
324
				.compareTrueFirst(server1.state == RealmsServer.State.UNINITIALIZED, server2.state == RealmsServer.State.UNINITIALIZED)
325
				.compareTrueFirst(server1.expiredTrial, server2.expiredTrial)
326
				.compareTrueFirst(Objects.equals(server1.owner, this.refOwner), Objects.equals(server2.owner, this.refOwner))
327
				.compareFalseFirst(server1.expired, server2.expired)
328
				.compareTrueFirst(server1.state == RealmsServer.State.OPEN, server2.state == RealmsServer.State.OPEN)
329
				.compare(server1.id, server2.id)
330
				.result();
331
		}
332
	}
333
334
	@Environment(EnvType.CLIENT)
335
	public enum State {
336
		CLOSED,
337
		OPEN,
338
		UNINITIALIZED;
339
	}
340
341
	@Environment(EnvType.CLIENT)
342
	public enum WorldType {
343
		NORMAL("normal"),
344
		MINIGAME("minigame"),
345
		ADVENTUREMAP("adventureMap"),
346
		EXPERIENCE("experience"),
347
		INSPIRATION("inspiration"),
348
		UNKNOWN("unknown");
349
350
		private static final String TRANSLATION_PREFIX = "mco.backup.entry.worldType.";
351
		private final Component displayName;
352
353
		WorldType(final String translationKey) {
354
			this.displayName = Component.translatable("mco.backup.entry.worldType." + translationKey);
355
		}
356
357
		public Component getDisplayName() {
358
			return this.displayName;
359
		}
360
	}
361
}
362

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