RealmsAvailability .java

com.mojang.realmsclient.RealmsAvailability (class, 99 lines): members: future, Result, Type, get, LOGGER. strings: "mco.error.invalid.session.message".

com.mojang.realmsclientclass99 linescommon3.7 KB

API Surface (2)

CompletableFuture<RealmsAvailability.Result> get()
record Result(RealmsAvailability.Type type, @Nullable RealmsServiceException exception)

Fields & Constants (2)

LOGGER
future

String literals (3)

"Couldn't connect to realms"
"mco.error.invalid.session.title"
"mco.error.invalid.session.message"

Full Source

RealmsAvailability.java99 lines · Minecraft 26.3 · verbatim
1
package com.mojang.realmsclient;
2
3
import com.mojang.logging.LogUtils;
4
import com.mojang.realmsclient.client.RealmsClient;
5
import com.mojang.realmsclient.exception.RealmsServiceException;
6
import com.mojang.realmsclient.gui.screens.RealmsClientOutdatedScreen;
7
import com.mojang.realmsclient.gui.screens.RealmsGenericErrorScreen;
8
import com.mojang.realmsclient.gui.screens.RealmsParentalConsentScreen;
9
import java.util.Objects;
10
import java.util.concurrent.CompletableFuture;
11
import net.fabricmc.api.EnvType;
12
import net.fabricmc.api.Environment;
13
import net.minecraft.SharedConstants;
14
import net.minecraft.client.Minecraft;
15
import net.minecraft.client.gui.screens.Screen;
16
import net.minecraft.network.chat.Component;
17
import net.minecraft.util.Util;
18
import org.jspecify.annotations.Nullable;
19
import org.slf4j.Logger;
20
21
@Environment(EnvType.CLIENT)
22
public class RealmsAvailability {
23
	private static final Logger LOGGER = LogUtils.getLogger();
24
	private static @Nullable CompletableFuture<RealmsAvailability.Result> future;
25
26
	public static CompletableFuture<RealmsAvailability.Result> get() {
27
		if (future == null || shouldRefresh(future)) {
28
			future = check();
29
		}
30
31
		return future;
32
	}
33
34
	private static boolean shouldRefresh(final CompletableFuture<RealmsAvailability.Result> future) {
35
		RealmsAvailability.Result result = future.getNow(null);
36
		return result != null && result.exception() != null;
37
	}
38
39
	private static CompletableFuture<RealmsAvailability.Result> check() {
40
		if (Minecraft.getInstance().isOfflineDeveloperMode()) {
41
			return CompletableFuture.completedFuture(new RealmsAvailability.Result(RealmsAvailability.Type.AUTHENTICATION_ERROR));
42
		} else {
43
			return SharedConstants.DEBUG_BYPASS_REALMS_VERSION_CHECK
44
				? CompletableFuture.completedFuture(new RealmsAvailability.Result(RealmsAvailability.Type.SUCCESS))
45
				: CompletableFuture.supplyAsync(
46
					() -> {
47
						RealmsClient client = RealmsClient.getOrCreate();
48
49
						try {
50
							if (client.clientCompatible() != RealmsClient.CompatibleVersionResponse.COMPATIBLE) {
51
								return new RealmsAvailability.Result(RealmsAvailability.Type.INCOMPATIBLE_CLIENT);
52
							} else {
53
								return !client.hasParentalConsent()
54
									? new RealmsAvailability.Result(RealmsAvailability.Type.NEEDS_PARENTAL_CONSENT)
55
									: new RealmsAvailability.Result(RealmsAvailability.Type.SUCCESS);
56
							}
57
						} catch (RealmsServiceException e) {
58
							LOGGER.error("Couldn't connect to realms", e);
59
							return e.realmsError.errorCode() == 401 ? new RealmsAvailability.Result(RealmsAvailability.Type.AUTHENTICATION_ERROR) : new RealmsAvailability.Result(e);
60
						}
61
					},
62
					Util.nonCriticalIoPool()
63
				);
64
		}
65
	}
66
67
	@Environment(EnvType.CLIENT)
68
	public record Result(RealmsAvailability.Type type, @Nullable RealmsServiceException exception) {
69
		public Result(final RealmsAvailability.Type type) {
70
			this(type, null);
71
		}
72
73
		public Result(final RealmsServiceException exception) {
74
			this(RealmsAvailability.Type.UNEXPECTED_ERROR, exception);
75
		}
76
77
		public @Nullable Screen createErrorScreen(final Screen lastScreen) {
78
			return switch (this.type) {
79
				case SUCCESS -> null;
80
				case INCOMPATIBLE_CLIENT -> new RealmsClientOutdatedScreen(lastScreen);
81
				case NEEDS_PARENTAL_CONSENT -> new RealmsParentalConsentScreen(lastScreen);
82
				case AUTHENTICATION_ERROR -> new RealmsGenericErrorScreen(
83
					Component.translatable("mco.error.invalid.session.title"), Component.translatable("mco.error.invalid.session.message"), lastScreen
84
				);
85
				case UNEXPECTED_ERROR -> new RealmsGenericErrorScreen(Objects.requireNonNull(this.exception), lastScreen);
86
			};
87
		}
88
	}
89
90
	@Environment(EnvType.CLIENT)
91
	public enum Type {
92
		SUCCESS,
93
		INCOMPATIBLE_CLIENT,
94
		NEEDS_PARENTAL_CONSENT,
95
		AUTHENTICATION_ERROR,
96
		UNEXPECTED_ERROR;
97
	}
98
}
99

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