File size: 4,406 Bytes
d46f4a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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 javax.annotation.Nullable;
import net.minecraft.Util;
import net.minecraft.client.Minecraft;
import net.minecraft.client.User;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.Component;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import org.slf4j.Logger;

@OnlyIn(Dist.CLIENT)
public class RealmsAvailability {
    private static final Logger LOGGER = LogUtils.getLogger();
    @Nullable
    private static 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> p_299038_) {
        RealmsAvailability.Result realmsavailability$result = p_299038_.getNow(null);
        return realmsavailability$result != null && realmsavailability$result.exception() != null;
    }

    private static CompletableFuture<RealmsAvailability.Result> check() {
        User user = Minecraft.getInstance().getUser();
        return user.getType() != User.Type.MSA
            ? CompletableFuture.completedFuture(new RealmsAvailability.Result(RealmsAvailability.Type.AUTHENTICATION_ERROR))
            : CompletableFuture.supplyAsync(
                () -> {
                    RealmsClient realmsclient = RealmsClient.create();

                    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", (Throwable)realmsserviceexception);
                        return realmsserviceexception.realmsError.errorCode() == 401
                            ? new RealmsAvailability.Result(RealmsAvailability.Type.AUTHENTICATION_ERROR)
                            : new RealmsAvailability.Result(realmsserviceexception);
                    }
                },
                Util.ioPool()
            );
    }

    @OnlyIn(Dist.CLIENT)
    public static record Result(RealmsAvailability.Type type, @Nullable RealmsServiceException exception) {
        public Result(RealmsAvailability.Type p_298726_) {
            this(p_298726_, null);
        }

        public Result(RealmsServiceException p_297806_) {
            this(RealmsAvailability.Type.UNEXPECTED_ERROR, p_297806_);
        }

        @Nullable
        public Screen createErrorScreen(Screen p_299167_) {
            return (Screen)(switch (this.type) {
                case SUCCESS -> null;
                case INCOMPATIBLE_CLIENT -> new RealmsClientOutdatedScreen(p_299167_);
                case NEEDS_PARENTAL_CONSENT -> new RealmsParentalConsentScreen(p_299167_);
                case AUTHENTICATION_ERROR -> new RealmsGenericErrorScreen(
                Component.translatable("mco.error.invalid.session.title"), Component.translatable("mco.error.invalid.session.message"), p_299167_
            );
                case UNEXPECTED_ERROR -> new RealmsGenericErrorScreen(Objects.requireNonNull(this.exception), p_299167_);
            });
        }
    }

    @OnlyIn(Dist.CLIENT)
    public static enum Type {
        SUCCESS,
        INCOMPATIBLE_CLIENT,
        NEEDS_PARENTAL_CONSENT,
        AUTHENTICATION_ERROR,
        UNEXPECTED_ERROR;
    }
}