package com.mojang.realmsclient; import com.mojang.logging.LogUtils; import com.mojang.realmsclient.client.RealmsClient; import com.mojang.realmsclient.exception.RealmsServiceException; import com.mojang.realmsclient.gui.screens.RealmsClientOutdatedScreen; import com.mojang.realmsclient.gui.screens.RealmsGenericErrorScreen; import com.mojang.realmsclient.gui.screens.RealmsParentalConsentScreen; import java.util.Objects; import java.util.concurrent.CompletableFuture; import net.minecraft.SharedConstants; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.screens.Screen; import net.minecraft.network.chat.Component; import net.minecraft.util.Util; import org.jspecify.annotations.Nullable; import org.slf4j.Logger; public class RealmsAvailability { private static final Logger LOGGER = LogUtils.getLogger(); @Nullable private static CompletableFuture future; public static CompletableFuture get() { if (future == null || shouldRefresh(future)) { future = check(); } return future; } private static boolean shouldRefresh(CompletableFuture completableFuture) { RealmsAvailability.Result result = (RealmsAvailability.Result)completableFuture.getNow(null); return result != null && result.exception() != null; } private static CompletableFuture check() { if (Minecraft.getInstance().isOfflineDeveloperMode()) { return CompletableFuture.completedFuture(new RealmsAvailability.Result(RealmsAvailability.Type.AUTHENTICATION_ERROR)); } else { return SharedConstants.DEBUG_BYPASS_REALMS_VERSION_CHECK ? CompletableFuture.completedFuture(new RealmsAvailability.Result(RealmsAvailability.Type.SUCCESS)) : CompletableFuture.supplyAsync( () -> { RealmsClient realmsClient = RealmsClient.getOrCreate(); try { if (realmsClient.clientCompatible() != RealmsClient.CompatibleVersionResponse.COMPATIBLE) { return new RealmsAvailability.Result(RealmsAvailability.Type.INCOMPATIBLE_CLIENT); } else { return !realmsClient.hasParentalConsent() ? new RealmsAvailability.Result(RealmsAvailability.Type.NEEDS_PARENTAL_CONSENT) : new RealmsAvailability.Result(RealmsAvailability.Type.SUCCESS); } } catch (RealmsServiceException realmsServiceException) { LOGGER.error("Couldn't connect to realms", realmsServiceException); return realmsServiceException.realmsError.errorCode() == 401 ? new RealmsAvailability.Result(RealmsAvailability.Type.AUTHENTICATION_ERROR) : new RealmsAvailability.Result(realmsServiceException); } }, Util.ioPool() ); } } public static record Result(RealmsAvailability.Type type, @Nullable RealmsServiceException exception) { public Result(RealmsAvailability.Type type) { this(typex, null); } public Result(RealmsServiceException realmsServiceException) { this(RealmsAvailability.Type.UNEXPECTED_ERROR, realmsServiceException); } @Nullable public Screen createErrorScreen(Screen screen) { return (Screen)(switch (this.type) { case SUCCESS -> null; case INCOMPATIBLE_CLIENT -> new RealmsClientOutdatedScreen(screen); case NEEDS_PARENTAL_CONSENT -> new RealmsParentalConsentScreen(screen); case AUTHENTICATION_ERROR -> new RealmsGenericErrorScreen( Component.translatable("mco.error.invalid.session.title"), Component.translatable("mco.error.invalid.session.message"), screen ); case UNEXPECTED_ERROR -> new RealmsGenericErrorScreen(Objects.requireNonNull(this.exception), screen); }); } } public static enum Type { SUCCESS, INCOMPATIBLE_CLIENT, NEEDS_PARENTAL_CONSENT, AUTHENTICATION_ERROR, UNEXPECTED_ERROR; } }