RealmsAvailability.java

com.mojang.realmsclient.RealmsAvailability

信息

  • 全限定名:com.mojang.realmsclient.RealmsAvailability
  • 类型:public class
  • 包:com.mojang.realmsclient
  • 源码路径:src/main/java/com/mojang/realmsclient/RealmsAvailability.java
  • 起始行号:L22
  • 职责:

    TODO

字段/常量

  • LOGGER

    • 类型: Logger
    • 修饰符: private static final
    • 源码定位: L23
    • 说明:

      TODO

  • future

    • 类型: CompletableFuture<RealmsAvailability.Result>
    • 修饰符: private static
    • 源码定位: L24
    • 说明:

      TODO

内部类/嵌套类型

  • com.mojang.realmsclient.RealmsAvailability.Result

    • 类型: record
    • 修饰符: public
    • 源码定位: L70
    • 说明:

      TODO

  • com.mojang.realmsclient.RealmsAvailability.Type

    • 类型: enum
    • 修饰符: public static
    • 源码定位: L93
    • 说明:

      TODO

构造器

方法

下面的方法块按源码顺序生成。

public static CompletableFuture<RealmsAvailability.Result> get() @ L26

  • 方法名:get
  • 源码定位:L26
  • 返回类型:CompletableFuture<RealmsAvailability.Result>
  • 修饰符:public static

参数:

说明:

TODO

private static boolean shouldRefresh(CompletableFuture<RealmsAvailability.Result> future) @ L34

  • 方法名:shouldRefresh
  • 源码定位:L34
  • 返回类型:boolean
  • 修饰符:private static

参数:

  • future: CompletableFuture<RealmsAvailability.Result>

说明:

TODO

private static CompletableFuture<RealmsAvailability.Result> check() @ L39

  • 方法名:check
  • 源码定位:L39
  • 返回类型:CompletableFuture<RealmsAvailability.Result>
  • 修饰符:private static

参数:

说明:

TODO

代码

@OnlyIn(Dist.CLIENT)
public class RealmsAvailability {
    private static final Logger LOGGER = LogUtils.getLogger();
    private static @Nullable CompletableFuture<RealmsAvailability.Result> future;
 
    public static CompletableFuture<RealmsAvailability.Result> get() {
        if (future == null || shouldRefresh(future)) {
            future = check();
        }
 
        return future;
    }
 
    private static boolean shouldRefresh(CompletableFuture<RealmsAvailability.Result> future) {
        RealmsAvailability.Result result = future.getNow(null);
        return result != null && result.exception() != null;
    }
 
    private static CompletableFuture<RealmsAvailability.Result> 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 client = RealmsClient.getOrCreate();
 
                        try {
                            if (client.clientCompatible() != RealmsClient.CompatibleVersionResponse.COMPATIBLE) {
                                return new RealmsAvailability.Result(RealmsAvailability.Type.INCOMPATIBLE_CLIENT);
                            } else {
                                return !client.hasParentalConsent()
                                    ? new RealmsAvailability.Result(RealmsAvailability.Type.NEEDS_PARENTAL_CONSENT)
                                    : new RealmsAvailability.Result(RealmsAvailability.Type.SUCCESS);
                            }
                        } catch (RealmsServiceException var2) {
                            LOGGER.error("Couldn't connect to realms", (Throwable)var2);
                            return var2.realmsError.errorCode() == 401
                                ? new RealmsAvailability.Result(RealmsAvailability.Type.AUTHENTICATION_ERROR)
                                : new RealmsAvailability.Result(var2);
                        }
                    },
                    Util.ioPool()
                );
        }
    }
 
    @OnlyIn(Dist.CLIENT)
    public record Result(RealmsAvailability.Type type, @Nullable RealmsServiceException exception) {
        public Result(RealmsAvailability.Type type) {
            this(type, null);
        }
 
        public Result(RealmsServiceException exception) {
            this(RealmsAvailability.Type.UNEXPECTED_ERROR, exception);
        }
 
        public @Nullable Screen createErrorScreen(Screen lastScreen) {
            return (Screen)(switch (this.type) {
                case SUCCESS -> null;
                case INCOMPATIBLE_CLIENT -> new RealmsClientOutdatedScreen(lastScreen);
                case NEEDS_PARENTAL_CONSENT -> new RealmsParentalConsentScreen(lastScreen);
                case AUTHENTICATION_ERROR -> new RealmsGenericErrorScreen(
                    Component.translatable("mco.error.invalid.session.title"), Component.translatable("mco.error.invalid.session.message"), lastScreen
                );
                case UNEXPECTED_ERROR -> new RealmsGenericErrorScreen(Objects.requireNonNull(this.exception), lastScreen);
            });
        }
    }
 
    @OnlyIn(Dist.CLIENT)
    public static enum Type {
        SUCCESS,
        INCOMPATIBLE_CLIENT,
        NEEDS_PARENTAL_CONSENT,
        AUTHENTICATION_ERROR,
        UNEXPECTED_ERROR;
    }
}

引用的其他类

  • RealmsClient

    • 引用位置: 方法调用
    • 关联成员: RealmsClient.getOrCreate()
  • RealmsClientOutdatedScreen

    • 引用位置: 构造调用
    • 关联成员: RealmsClientOutdatedScreen()
  • RealmsGenericErrorScreen

    • 引用位置: 构造调用
    • 关联成员: RealmsGenericErrorScreen()
  • RealmsParentalConsentScreen

    • 引用位置: 构造调用
    • 关联成员: RealmsParentalConsentScreen()
  • Minecraft

    • 引用位置: 方法调用
    • 关联成员: Minecraft.getInstance()
  • Component

    • 引用位置: 方法调用
    • 关联成员: Component.translatable()
  • Util

    • 引用位置: 方法调用
    • 关联成员: Util.ioPool()