| 1 | package com.mojang.realmsclient.gui.screens.configuration;
|
| 2 |
|
| 3 | import com.mojang.realmsclient.RealmsMainScreen;
|
| 4 | import com.mojang.realmsclient.dto.RealmsServer;
|
| 5 | import com.mojang.realmsclient.dto.Subscription;
|
| 6 | import com.mojang.realmsclient.gui.screens.RealmsPopups;
|
| 7 | import com.mojang.realmsclient.util.RealmsUtil;
|
| 8 | import java.time.Instant;
|
| 9 | import java.time.ZoneId;
|
| 10 | import java.time.ZonedDateTime;
|
| 11 | import java.time.format.FormatStyle;
|
| 12 | import net.fabricmc.api.EnvType;
|
| 13 | import net.fabricmc.api.Environment;
|
| 14 | import net.minecraft.ChatFormatting;
|
| 15 | import net.minecraft.client.Minecraft;
|
| 16 | import net.minecraft.client.gui.Font;
|
| 17 | import net.minecraft.client.gui.components.Button;
|
| 18 | import net.minecraft.client.gui.components.FocusableTextWidget;
|
| 19 | import net.minecraft.client.gui.components.StringWidget;
|
| 20 | import net.minecraft.client.gui.components.tabs.GridLayoutTab;
|
| 21 | import net.minecraft.client.gui.layouts.GridLayout;
|
| 22 | import net.minecraft.client.gui.layouts.LayoutSettings;
|
| 23 | import net.minecraft.client.gui.layouts.SpacerElement;
|
| 24 | import net.minecraft.client.gui.screens.ConfirmLinkScreen;
|
| 25 | import net.minecraft.network.chat.CommonComponents;
|
| 26 | import net.minecraft.network.chat.Component;
|
| 27 | import net.minecraft.util.CommonLinks;
|
| 28 | import net.minecraft.util.Util;
|
| 29 | import net.minecraft.util.CommonLinks.ExtensionReference;
|
| 30 |
|
| 31 | @Environment(EnvType.CLIENT)
|
| 32 | public class RealmsSubscriptionTab extends GridLayoutTab implements RealmsConfigurationTab {
|
| 33 | private static final int DEFAULT_COMPONENT_WIDTH = 200;
|
| 34 | private static final int EXTRA_SPACING = 2;
|
| 35 | private static final int DEFAULT_SPACING = 6;
|
| 36 | public static final Component TITLE = Component.translatable("mco.configure.world.subscription.tab");
|
| 37 | private static final Component SUBSCRIPTION_START_LABEL = Component.translatable("mco.configure.world.subscription.start");
|
| 38 | private static final Component TIME_LEFT_LABEL = Component.translatable("mco.configure.world.subscription.timeleft");
|
| 39 | private static final Component DAYS_LEFT_LABEL = Component.translatable("mco.configure.world.subscription.recurring.daysleft");
|
| 40 | private static final Component SUBSCRIPTION_EXPIRED_TEXT = Component.translatable("mco.configure.world.subscription.expired").withStyle(ChatFormatting.GRAY);
|
| 41 | private static final Component SUBSCRIPTION_LESS_THAN_A_DAY_TEXT = Component.translatable("mco.configure.world.subscription.less_than_a_day")
|
| 42 | .withStyle(ChatFormatting.GRAY);
|
| 43 | private static final Component RECURRING_INFO = Component.translatable("mco.configure.world.subscription.recurring.info");
|
| 44 | private final RealmsConfigureWorldScreen configurationScreen;
|
| 45 | private final Minecraft minecraft;
|
| 46 | private final Button deleteButton;
|
| 47 | private final FocusableTextWidget subscriptionInfo;
|
| 48 | private final Component startDate;
|
| 49 | private final Component daysLeft;
|
| 50 | private RealmsServer serverData;
|
| 51 |
|
| 52 | public RealmsSubscriptionTab(
|
| 53 | final RealmsConfigureWorldScreen configurationScreen, final Minecraft minecraft, final RealmsServer serverData, final Subscription subscription
|
| 54 | ) {
|
| 55 | super(TITLE);
|
| 56 | this.configurationScreen = configurationScreen;
|
| 57 | this.minecraft = minecraft;
|
| 58 | this.serverData = serverData;
|
| 59 | this.startDate = localPresentation(subscription.startDate());
|
| 60 | this.daysLeft = this.daysLeftPresentation(subscription.daysLeft());
|
| 61 | GridLayout.RowHelper helper = this.layout.rowSpacing(6).createRowHelper(1);
|
| 62 | Font font = configurationScreen.getFont();
|
| 63 | helper.addChild(new StringWidget(200, 9, SUBSCRIPTION_START_LABEL, font));
|
| 64 | helper.addChild(new StringWidget(200, 9, this.startDate, font));
|
| 65 | helper.addChild(SpacerElement.height(2));
|
| 66 | Component daysLeftLabel = subscription.type() == Subscription.SubscriptionType.RECURRING ? DAYS_LEFT_LABEL : TIME_LEFT_LABEL;
|
| 67 | helper.addChild(new StringWidget(200, 9, daysLeftLabel, font));
|
| 68 | helper.addChild(new StringWidget(200, 9, this.daysLeft, font));
|
| 69 | helper.addChild(SpacerElement.height(2));
|
| 70 | helper.addChild(
|
| 71 | Button.builder(
|
| 72 | Component.translatable("mco.configure.world.subscription.extend"),
|
| 73 | button -> ConfirmLinkScreen.confirmLinkNow(
|
| 74 | configurationScreen, CommonLinks.extendRealms(serverData.remoteSubscriptionId, minecraft.getUser().getProfileId(), ExtensionReference.NONE)
|
| 75 | )
|
| 76 | )
|
| 77 | .bounds(0, 0, 200, 20)
|
| 78 | .build()
|
| 79 | );
|
| 80 | helper.addChild(SpacerElement.height(2));
|
| 81 | this.deleteButton = helper.addChild(
|
| 82 | Button.builder(
|
| 83 | Component.translatable("mco.configure.world.delete.button"),
|
| 84 | var3 -> minecraft.gui
|
| 85 | .setScreen(
|
| 86 | RealmsPopups.warningPopupScreen(configurationScreen, Component.translatable("mco.configure.world.delete.question.line1"), popup -> this.deleteRealm())
|
| 87 | )
|
| 88 | )
|
| 89 | .bounds(0, 0, 200, 20)
|
| 90 | .build()
|
| 91 | );
|
| 92 | helper.addChild(SpacerElement.height(2));
|
| 93 | this.subscriptionInfo = helper.addChild(
|
| 94 | FocusableTextWidget.builder(Component.empty(), font).maxWidth(200).build(), LayoutSettings.defaults().alignHorizontallyCenter()
|
| 95 | );
|
| 96 | this.subscriptionInfo.setCentered(false);
|
| 97 | this.updateData(serverData);
|
| 98 | }
|
| 99 |
|
| 100 | private void deleteRealm() {
|
| 101 | RealmsUtil.runAsync(
|
| 102 | client -> client.deleteRealm(this.serverData.id),
|
| 103 | RealmsUtil.openScreenAndLogOnFailure(this.configurationScreen::createErrorScreen, "Couldn't delete world")
|
| 104 | )
|
| 105 | .thenRunAsync(() -> this.minecraft.gui.setScreen(this.configurationScreen.getLastScreen()), this.minecraft);
|
| 106 | this.minecraft.gui.setScreen(this.configurationScreen);
|
| 107 | }
|
| 108 |
|
| 109 | private static Component localPresentation(final Instant time) {
|
| 110 | String formattedDate = ZonedDateTime.ofInstant(time, ZoneId.systemDefault()).format(Util.localizedDateFormatter(FormatStyle.MEDIUM));
|
| 111 | return Component.literal(formattedDate).withStyle(ChatFormatting.GRAY);
|
| 112 | }
|
| 113 |
|
| 114 | private Component daysLeftPresentation(final int daysLeft) {
|
| 115 | if (daysLeft < 0 && this.serverData.expired) {
|
| 116 | return SUBSCRIPTION_EXPIRED_TEXT;
|
| 117 | } else if (daysLeft <= 1) {
|
| 118 | return SUBSCRIPTION_LESS_THAN_A_DAY_TEXT;
|
| 119 | } else {
|
| 120 | int months = daysLeft / 30;
|
| 121 | int days = daysLeft % 30;
|
| 122 | boolean showMonths = months > 0;
|
| 123 | boolean showDays = days > 0;
|
| 124 | if (showMonths && showDays) {
|
| 125 | return Component.translatable("mco.configure.world.subscription.remaining.months.days", months, days).withStyle(ChatFormatting.GRAY);
|
| 126 | } else if (showMonths) {
|
| 127 | return Component.translatable("mco.configure.world.subscription.remaining.months", months).withStyle(ChatFormatting.GRAY);
|
| 128 | } else {
|
| 129 | return showDays ? Component.translatable("mco.configure.world.subscription.remaining.days", days).withStyle(ChatFormatting.GRAY) : Component.empty();
|
| 130 | }
|
| 131 | }
|
| 132 | }
|
| 133 |
|
| 134 | @Override
|
| 135 | public void updateData(final RealmsServer serverData) {
|
| 136 | this.serverData = serverData;
|
| 137 | boolean snapshotWorld = RealmsMainScreen.isSnapshot() && serverData.parentWorldName != null;
|
| 138 | this.deleteButton.active = serverData.expired;
|
| 139 | if (snapshotWorld) {
|
| 140 | this.subscriptionInfo.setMessage(Component.translatable("mco.snapshot.subscription.info", serverData.parentWorldName));
|
| 141 | } else {
|
| 142 | this.subscriptionInfo.setMessage(RECURRING_INFO);
|
| 143 | }
|
| 144 |
|
| 145 | this.layout.arrangeElements();
|
| 146 | }
|
| 147 |
|
| 148 | @Override
|
| 149 | public Component getTabExtraNarration() {
|
| 150 | return CommonComponents.joinLines(TITLE, SUBSCRIPTION_START_LABEL, this.startDate, TIME_LEFT_LABEL, this.daysLeft);
|
| 151 | }
|
| 152 | }
|
| 153 |
|