File size: 8,698 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package com.mojang.realmsclient.gui.screens;

import com.mojang.logging.LogUtils;
import com.mojang.realmsclient.RealmsMainScreen;
import com.mojang.realmsclient.client.RealmsClient;
import com.mojang.realmsclient.dto.RealmsServer;
import com.mojang.realmsclient.dto.Subscription;
import com.mojang.realmsclient.exception.RealmsServiceException;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.TimeZone;
import javax.annotation.Nullable;
import net.minecraft.client.GameNarrator;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.components.FittingMultiLineTextWidget;
import net.minecraft.client.gui.components.PopupScreen;
import net.minecraft.client.gui.screens.ConfirmLinkScreen;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.CommonComponents;
import net.minecraft.network.chat.Component;
import net.minecraft.realms.RealmsScreen;
import net.minecraft.util.CommonLinks;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import org.slf4j.Logger;

@OnlyIn(Dist.CLIENT)
public class RealmsSubscriptionInfoScreen extends RealmsScreen {
    static final Logger LOGGER = LogUtils.getLogger();
    private static final Component SUBSCRIPTION_TITLE = Component.translatable("mco.configure.world.subscription.title");
    private static final Component SUBSCRIPTION_START_LABEL = Component.translatable("mco.configure.world.subscription.start");
    private static final Component TIME_LEFT_LABEL = Component.translatable("mco.configure.world.subscription.timeleft");
    private static final Component DAYS_LEFT_LABEL = Component.translatable("mco.configure.world.subscription.recurring.daysleft");
    private static final Component SUBSCRIPTION_EXPIRED_TEXT = Component.translatable("mco.configure.world.subscription.expired");
    private static final Component SUBSCRIPTION_LESS_THAN_A_DAY_TEXT = Component.translatable("mco.configure.world.subscription.less_than_a_day");
    private static final Component UNKNOWN = Component.translatable("mco.configure.world.subscription.unknown");
    private static final Component RECURRING_INFO = Component.translatable("mco.configure.world.subscription.recurring.info");
    private final Screen lastScreen;
    final RealmsServer serverData;
    final Screen mainScreen;
    private Component daysLeft = UNKNOWN;
    private Component startDate = UNKNOWN;
    @Nullable
    private Subscription.SubscriptionType type;

    public RealmsSubscriptionInfoScreen(Screen p_89979_, RealmsServer p_89980_, Screen p_89981_) {
        super(GameNarrator.NO_TITLE);
        this.lastScreen = p_89979_;
        this.serverData = p_89980_;
        this.mainScreen = p_89981_;
    }

    @Override
    public void init() {
        this.getSubscription(this.serverData.id);
        this.addRenderableWidget(
            Button.builder(
                    Component.translatable("mco.configure.world.subscription.extend"),
                    p_296090_ -> ConfirmLinkScreen.confirmLinkNow(this, CommonLinks.extendRealms(this.serverData.remoteSubscriptionId, this.minecraft.getUser().getProfileId()))
                )
                .bounds(this.width / 2 - 100, row(6), 200, 20)
                .build()
        );
        if (this.serverData.expired) {
            this.addRenderableWidget(
                Button.builder(
                        Component.translatable("mco.configure.world.delete.button"),
                        p_340731_ -> this.minecraft
                                .setScreen(
                                    RealmsPopups.warningPopupScreen(this, Component.translatable("mco.configure.world.delete.question.line1"), p_340730_ -> this.deleteRealm())
                                )
                    )
                    .bounds(this.width / 2 - 100, row(10), 200, 20)
                    .build()
            );
        } else if (RealmsMainScreen.isSnapshot() && this.serverData.parentWorldName != null) {
            this.addRenderableWidget(
                new FittingMultiLineTextWidget(
                    this.width / 2 - 100,
                    row(8),
                    200,
                    46,
                    Component.translatable("mco.snapshot.subscription.info", this.serverData.parentWorldName),
                    this.font
                )
            );
        } else {
            this.addRenderableWidget(new FittingMultiLineTextWidget(this.width / 2 - 100, row(8), 200, 46, RECURRING_INFO, this.font));
        }

        this.addRenderableWidget(
            Button.builder(CommonComponents.GUI_BACK, p_280760_ -> this.onClose()).bounds(this.width / 2 - 100, row(12), 200, 20).build()
        );
    }

    @Override
    public Component getNarrationMessage() {
        return CommonComponents.joinLines(SUBSCRIPTION_TITLE, SUBSCRIPTION_START_LABEL, this.startDate, TIME_LEFT_LABEL, this.daysLeft);
    }

    private void deleteRealm() {
        (new Thread("Realms-delete-realm") {
                @Override
                public void run() {
                    try {
                        RealmsClient realmsclient = RealmsClient.create();
                        realmsclient.deleteRealm(RealmsSubscriptionInfoScreen.this.serverData.id);
                    } catch (RealmsServiceException realmsserviceexception) {
                        RealmsSubscriptionInfoScreen.LOGGER.error("Couldn't delete world", (Throwable)realmsserviceexception);
                    }

                    RealmsSubscriptionInfoScreen.this.minecraft
                        .execute(() -> RealmsSubscriptionInfoScreen.this.minecraft.setScreen(RealmsSubscriptionInfoScreen.this.mainScreen));
                }
            })
            .start();
        this.minecraft.setScreen(this);
    }

    private void getSubscription(long p_89990_) {
        RealmsClient realmsclient = RealmsClient.create();

        try {
            Subscription subscription = realmsclient.subscriptionFor(p_89990_);
            this.daysLeft = this.daysLeftPresentation(subscription.daysLeft);
            this.startDate = localPresentation(subscription.startDate);
            this.type = subscription.type;
        } catch (RealmsServiceException realmsserviceexception) {
            LOGGER.error("Couldn't get subscription", (Throwable)realmsserviceexception);
            this.minecraft.setScreen(new RealmsGenericErrorScreen(realmsserviceexception, this.lastScreen));
        }
    }

    private static Component localPresentation(long p_182539_) {
        Calendar calendar = new GregorianCalendar(TimeZone.getDefault());
        calendar.setTimeInMillis(p_182539_);
        return Component.literal(DateFormat.getDateTimeInstance().format(calendar.getTime()));
    }

    @Override
    public void onClose() {
        this.minecraft.setScreen(this.lastScreen);
    }

    @Override
    public void render(GuiGraphics p_282831_, int p_281266_, int p_281799_, float p_282330_) {
        super.render(p_282831_, p_281266_, p_281799_, p_282330_);
        int i = this.width / 2 - 100;
        p_282831_.drawCenteredString(this.font, SUBSCRIPTION_TITLE, this.width / 2, 17, -1);
        p_282831_.drawString(this.font, SUBSCRIPTION_START_LABEL, i, row(0), -6250336);
        p_282831_.drawString(this.font, this.startDate, i, row(1), -1);
        if (this.type == Subscription.SubscriptionType.NORMAL) {
            p_282831_.drawString(this.font, TIME_LEFT_LABEL, i, row(3), -6250336);
        } else if (this.type == Subscription.SubscriptionType.RECURRING) {
            p_282831_.drawString(this.font, DAYS_LEFT_LABEL, i, row(3), -6250336);
        }

        p_282831_.drawString(this.font, this.daysLeft, i, row(4), -1);
    }

    private Component daysLeftPresentation(int p_89984_) {
        if (p_89984_ < 0 && this.serverData.expired) {
            return SUBSCRIPTION_EXPIRED_TEXT;
        } else if (p_89984_ <= 1) {
            return SUBSCRIPTION_LESS_THAN_A_DAY_TEXT;
        } else {
            int i = p_89984_ / 30;
            int j = p_89984_ % 30;
            boolean flag = i > 0;
            boolean flag1 = j > 0;
            if (flag && flag1) {
                return Component.translatable("mco.configure.world.subscription.remaining.months.days", i, j);
            } else if (flag) {
                return Component.translatable("mco.configure.world.subscription.remaining.months", i);
            } else {
                return flag1 ? Component.translatable("mco.configure.world.subscription.remaining.days", j) : Component.empty();
            }
        }
    }
}