| 1 | package com.mojang.realmsclient.gui.screens.configuration;
|
| 2 |
|
| 3 | import com.mojang.logging.LogUtils;
|
| 4 | import com.mojang.realmsclient.client.RealmsClient;
|
| 5 | import com.mojang.realmsclient.dto.Backup;
|
| 6 | import com.mojang.realmsclient.dto.RealmsServer;
|
| 7 | import com.mojang.realmsclient.exception.RealmsServiceException;
|
| 8 | import com.mojang.realmsclient.gui.screens.RealmsLongRunningMcoTaskScreen;
|
| 9 | import com.mojang.realmsclient.gui.screens.RealmsPopups;
|
| 10 | import com.mojang.realmsclient.util.RealmsUtil;
|
| 11 | import com.mojang.realmsclient.util.task.DownloadTask;
|
| 12 | import com.mojang.realmsclient.util.task.RestoreTask;
|
| 13 | import java.time.format.DateTimeFormatter;
|
| 14 | import java.time.format.FormatStyle;
|
| 15 | import java.util.ArrayList;
|
| 16 | import java.util.Collections;
|
| 17 | import java.util.List;
|
| 18 | import java.util.Objects;
|
| 19 | import java.util.function.Supplier;
|
| 20 | import net.fabricmc.api.EnvType;
|
| 21 | import net.fabricmc.api.Environment;
|
| 22 | import net.minecraft.client.Minecraft;
|
| 23 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 24 | import net.minecraft.client.gui.components.AbstractWidget;
|
| 25 | import net.minecraft.client.gui.components.Button;
|
| 26 | import net.minecraft.client.gui.components.ContainerObjectSelectionList;
|
| 27 | import net.minecraft.client.gui.components.events.GuiEventListener;
|
| 28 | import net.minecraft.client.gui.layouts.HeaderAndFooterLayout;
|
| 29 | import net.minecraft.client.gui.layouts.LinearLayout;
|
| 30 | import net.minecraft.client.gui.narration.NarratableEntry;
|
| 31 | import net.minecraft.network.chat.CommonComponents;
|
| 32 | import net.minecraft.network.chat.Component;
|
| 33 | import net.minecraft.network.chat.MutableComponent;
|
| 34 | import net.minecraft.realms.RealmsScreen;
|
| 35 | import net.minecraft.util.Util;
|
| 36 | import org.jspecify.annotations.Nullable;
|
| 37 | import org.slf4j.Logger;
|
| 38 |
|
| 39 | @Environment(EnvType.CLIENT)
|
| 40 | public class RealmsBackupScreen extends RealmsScreen {
|
| 41 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 42 | private static final Component TITLE = Component.translatable("mco.configure.world.backup");
|
| 43 | private static final Component RESTORE_TOOLTIP = Component.translatable("mco.backup.button.restore");
|
| 44 | private static final Component HAS_CHANGES_TOOLTIP = Component.translatable("mco.backup.changes.tooltip");
|
| 45 | private static final Component NO_BACKUPS_LABEL = Component.translatable("mco.backup.nobackups");
|
| 46 | private static final Component DOWNLOAD_LATEST = Component.translatable("mco.backup.button.download");
|
| 47 | private static final String UPLOADED_KEY = "uploaded";
|
| 48 | private static final int PADDING = 8;
|
| 49 | public static final DateTimeFormatter SHORT_DATE_FORMAT = Util.localizedDateFormatter(FormatStyle.SHORT);
|
| 50 | private final RealmsConfigureWorldScreen lastScreen;
|
| 51 | private List<Backup> backups = Collections.emptyList();
|
| 52 | private RealmsBackupScreen.@Nullable BackupObjectSelectionList backupList;
|
| 53 | private final HeaderAndFooterLayout layout = new HeaderAndFooterLayout(this);
|
| 54 | private final int slotId;
|
| 55 | private @Nullable Button downloadButton;
|
| 56 | private final RealmsServer serverData;
|
| 57 | private boolean noBackups = false;
|
| 58 |
|
| 59 | public RealmsBackupScreen(final RealmsConfigureWorldScreen lastScreen, final RealmsServer serverData, final int slotId) {
|
| 60 | super(TITLE);
|
| 61 | this.lastScreen = lastScreen;
|
| 62 | this.serverData = serverData;
|
| 63 | this.slotId = slotId;
|
| 64 | }
|
| 65 |
|
| 66 | @Override
|
| 67 | public void init() {
|
| 68 | this.layout.addTitleHeader(TITLE, this.font);
|
| 69 | this.backupList = this.layout.addToContents(new RealmsBackupScreen.BackupObjectSelectionList());
|
| 70 | LinearLayout footer = this.layout.addToFooter(LinearLayout.horizontal().spacing(8));
|
| 71 | this.downloadButton = footer.addChild(Button.builder(DOWNLOAD_LATEST, button -> this.downloadClicked()).build());
|
| 72 | this.downloadButton.active = false;
|
| 73 | footer.addChild(Button.builder(CommonComponents.GUI_BACK, button -> this.onClose()).build());
|
| 74 | this.layout.visitWidgets(x$0 -> this.addRenderableWidget(x$0));
|
| 75 | this.repositionElements();
|
| 76 | this.fetchRealmsBackups();
|
| 77 | }
|
| 78 |
|
| 79 | @Override
|
| 80 | public void extractRenderState(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
|
| 81 | super.extractRenderState(graphics, mouseX, mouseY, a);
|
| 82 | if (this.noBackups && this.backupList != null) {
|
| 83 | graphics.text(
|
| 84 | this.font, NO_BACKUPS_LABEL, this.width / 2 - this.font.width(NO_BACKUPS_LABEL) / 2, this.backupList.getY() + this.backupList.getHeight() / 2 - 9 / 2, -1
|
| 85 | );
|
| 86 | }
|
| 87 | }
|
| 88 |
|
| 89 | @Override
|
| 90 | protected void repositionElements() {
|
| 91 | this.layout.arrangeElements();
|
| 92 | if (this.backupList != null) {
|
| 93 | this.backupList.updateSize(this.width, this.layout);
|
| 94 | }
|
| 95 | }
|
| 96 |
|
| 97 | private void fetchRealmsBackups() {
|
| 98 | (new Thread("Realms-fetch-backups") {
|
| 99 | @Override
|
| 100 | public void run() {
|
| 101 | RealmsClient client = RealmsClient.getOrCreate();
|
| 102 |
|
| 103 | try {
|
| 104 | List<Backup> backups = client.backupsFor(RealmsBackupScreen.this.serverData.id).backups();
|
| 105 | RealmsBackupScreen.this.minecraft.execute(() -> {
|
| 106 | RealmsBackupScreen.this.backups = backups;
|
| 107 | RealmsBackupScreen.this.noBackups = RealmsBackupScreen.this.backups.isEmpty();
|
| 108 | if (!RealmsBackupScreen.this.noBackups && RealmsBackupScreen.this.downloadButton != null) {
|
| 109 | RealmsBackupScreen.this.downloadButton.active = true;
|
| 110 | }
|
| 111 |
|
| 112 | if (RealmsBackupScreen.this.backupList != null) {
|
| 113 | RealmsBackupScreen.this.backupList.replaceEntries(RealmsBackupScreen.this.backups.stream().map(x$0 -> RealmsBackupScreen.this.new Entry(x$0)).toList());
|
| 114 | }
|
| 115 | });
|
| 116 | } catch (RealmsServiceException e) {
|
| 117 | RealmsBackupScreen.LOGGER.error("Couldn't request backups", e);
|
| 118 | }
|
| 119 | }
|
| 120 | }).start();
|
| 121 | }
|
| 122 |
|
| 123 | @Override
|
| 124 | public void onClose() {
|
| 125 | this.minecraft.gui.setScreen(this.lastScreen);
|
| 126 | }
|
| 127 |
|
| 128 | private void downloadClicked() {
|
| 129 | this.minecraft
|
| 130 | .gui
|
| 131 | .setScreen(
|
| 132 | RealmsPopups.infoPopupScreen(
|
| 133 | this,
|
| 134 | Component.translatable("mco.configure.world.restore.download.question.line1"),
|
| 135 | var1 -> this.minecraft
|
| 136 | .gui
|
| 137 | .setScreen(
|
| 138 | new RealmsLongRunningMcoTaskScreen(
|
| 139 | this.lastScreen.getNewScreen(),
|
| 140 | new DownloadTask(
|
| 141 | this.serverData.id,
|
| 142 | this.slotId,
|
| 143 | Objects.requireNonNullElse(this.serverData.name, "")
|
| 144 | + " ("
|
| 145 | + this.serverData.slots.get(this.serverData.activeSlot).options.getSlotName(this.serverData.activeSlot)
|
| 146 | + ")",
|
| 147 | this
|
| 148 | )
|
| 149 | )
|
| 150 | )
|
| 151 | )
|
| 152 | );
|
| 153 | }
|
| 154 |
|
| 155 | @Environment(EnvType.CLIENT)
|
| 156 | private class BackupObjectSelectionList extends ContainerObjectSelectionList<RealmsBackupScreen.Entry> {
|
| 157 | private static final int ITEM_HEIGHT = 36;
|
| 158 |
|
| 159 | public BackupObjectSelectionList() {
|
| 160 | super(
|
| 161 | Minecraft.getInstance(),
|
| 162 | RealmsBackupScreen.this.width,
|
| 163 | RealmsBackupScreen.this.layout.getContentHeight(),
|
| 164 | RealmsBackupScreen.this.layout.getHeaderHeight(),
|
| 165 | 36
|
| 166 | );
|
| 167 | }
|
| 168 |
|
| 169 | @Override
|
| 170 | public int getRowWidth() {
|
| 171 | return 300;
|
| 172 | }
|
| 173 | }
|
| 174 |
|
| 175 | @Environment(EnvType.CLIENT)
|
| 176 | private class Entry extends ContainerObjectSelectionList.Entry<RealmsBackupScreen.Entry> {
|
| 177 | private static final int Y_PADDING = 2;
|
| 178 | private final Backup backup;
|
| 179 | private @Nullable Button restoreButton;
|
| 180 | private @Nullable Button changesButton;
|
| 181 | private final List<AbstractWidget> children = new ArrayList<>();
|
| 182 |
|
| 183 | public Entry(final Backup backup) {
|
| 184 | this.backup = backup;
|
| 185 | this.populateChangeList(backup);
|
| 186 | if (!backup.changeList.isEmpty()) {
|
| 187 | this.changesButton = Button.builder(
|
| 188 | RealmsBackupScreen.HAS_CHANGES_TOOLTIP,
|
| 189 | var2 -> RealmsBackupScreen.this.minecraft.gui.setScreen(new RealmsBackupInfoScreen(RealmsBackupScreen.this, this.backup))
|
| 190 | )
|
| 191 | .width(8 + RealmsBackupScreen.this.font.width(RealmsBackupScreen.HAS_CHANGES_TOOLTIP))
|
| 192 | .createNarration(this::narrationForBackupEntry)
|
| 193 | .build();
|
| 194 | this.children.add(this.changesButton);
|
| 195 | }
|
| 196 |
|
| 197 | if (!RealmsBackupScreen.this.serverData.expired) {
|
| 198 | this.restoreButton = Button.builder(RealmsBackupScreen.RESTORE_TOOLTIP, button -> this.restoreClicked())
|
| 199 | .width(8 + RealmsBackupScreen.this.font.width(RealmsBackupScreen.HAS_CHANGES_TOOLTIP))
|
| 200 | .createNarration(this::narrationForBackupEntry)
|
| 201 | .build();
|
| 202 | this.children.add(this.restoreButton);
|
| 203 | }
|
| 204 | }
|
| 205 |
|
| 206 | private MutableComponent narrationForBackupEntry(final Supplier<MutableComponent> defaultNarrationSupplier) {
|
| 207 | return CommonComponents.joinForNarration(
|
| 208 | Component.translatable("mco.backup.narration", RealmsBackupScreen.SHORT_DATE_FORMAT.format(this.backup.lastModifiedDate())), defaultNarrationSupplier.get()
|
| 209 | );
|
| 210 | }
|
| 211 |
|
| 212 | private void populateChangeList(final Backup backup) {
|
| 213 | int index = RealmsBackupScreen.this.backups.indexOf(backup);
|
| 214 | if (index != RealmsBackupScreen.this.backups.size() - 1) {
|
| 215 | Backup olderBackup = RealmsBackupScreen.this.backups.get(index + 1);
|
| 216 |
|
| 217 | for (String key : backup.metadata.keySet()) {
|
| 218 | if (!key.contains("uploaded") && olderBackup.metadata.containsKey(key)) {
|
| 219 | if (!backup.metadata.get(key).equals(olderBackup.metadata.get(key))) {
|
| 220 | this.addToChangeList(key);
|
| 221 | }
|
| 222 | } else {
|
| 223 | this.addToChangeList(key);
|
| 224 | }
|
| 225 | }
|
| 226 | }
|
| 227 | }
|
| 228 |
|
| 229 | private void addToChangeList(final String key) {
|
| 230 | if (key.contains("uploaded")) {
|
| 231 | String uploadedTime = RealmsBackupScreen.SHORT_DATE_FORMAT.format(this.backup.lastModifiedDate());
|
| 232 | this.backup.changeList.put(key, uploadedTime);
|
| 233 | this.backup.uploadedVersion = true;
|
| 234 | } else {
|
| 235 | this.backup.changeList.put(key, this.backup.metadata.get(key));
|
| 236 | }
|
| 237 | }
|
| 238 |
|
| 239 | private void restoreClicked() {
|
| 240 | Component age = RealmsUtil.convertToAgePresentationFromInstant(this.backup.lastModified);
|
| 241 | String lastModifiedDate = RealmsBackupScreen.SHORT_DATE_FORMAT.format(this.backup.lastModifiedDate());
|
| 242 | Component popupMessage = Component.translatable("mco.configure.world.restore.question.line1", lastModifiedDate, age);
|
| 243 | RealmsBackupScreen.this.minecraft
|
| 244 | .gui
|
| 245 | .setScreen(
|
| 246 | RealmsPopups.warningPopupScreen(
|
| 247 | RealmsBackupScreen.this,
|
| 248 | popupMessage,
|
| 249 | var1x -> {
|
| 250 | RealmsConfigureWorldScreen newScreen = RealmsBackupScreen.this.lastScreen.getNewScreen();
|
| 251 | RealmsBackupScreen.this.minecraft
|
| 252 | .gui
|
| 253 | .setScreen(new RealmsLongRunningMcoTaskScreen(newScreen, new RestoreTask(this.backup, RealmsBackupScreen.this.serverData.id, newScreen)));
|
| 254 | }
|
| 255 | )
|
| 256 | );
|
| 257 | }
|
| 258 |
|
| 259 | @Override
|
| 260 | public List<? extends GuiEventListener> children() {
|
| 261 | return this.children;
|
| 262 | }
|
| 263 |
|
| 264 | @Override
|
| 265 | public List<? extends NarratableEntry> narratables() {
|
| 266 | return this.children;
|
| 267 | }
|
| 268 |
|
| 269 | @Override
|
| 270 | public void extractContent(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final boolean hovered, final float a) {
|
| 271 | int middle = this.getContentYMiddle();
|
| 272 | int firstLineYPos = middle - 9 - 2;
|
| 273 | int secondLineYPos = middle + 2;
|
| 274 | int color = this.backup.uploadedVersion ? -8388737 : -1;
|
| 275 | graphics.text(
|
| 276 | RealmsBackupScreen.this.font,
|
| 277 | Component.translatable("mco.backup.entry", RealmsUtil.convertToAgePresentationFromInstant(this.backup.lastModified)),
|
| 278 | this.getContentX(),
|
| 279 | firstLineYPos,
|
| 280 | color
|
| 281 | );
|
| 282 | graphics.text(
|
| 283 | RealmsBackupScreen.this.font, RealmsBackupScreen.SHORT_DATE_FORMAT.format(this.backup.lastModifiedDate()), this.getContentX(), secondLineYPos, -11776948
|
| 284 | );
|
| 285 | int iconXOffet = 0;
|
| 286 | int iconYPos = this.getContentYMiddle() - 10;
|
| 287 | if (this.restoreButton != null) {
|
| 288 | iconXOffet += this.restoreButton.getWidth() + 8;
|
| 289 | this.restoreButton.setX(this.getContentRight() - iconXOffet);
|
| 290 | this.restoreButton.setY(iconYPos);
|
| 291 | this.restoreButton.extractRenderState(graphics, mouseX, mouseY, a);
|
| 292 | }
|
| 293 |
|
| 294 | if (this.changesButton != null) {
|
| 295 | iconXOffet += this.changesButton.getWidth() + 8;
|
| 296 | this.changesButton.setX(this.getContentRight() - iconXOffet);
|
| 297 | this.changesButton.setY(iconYPos);
|
| 298 | this.changesButton.extractRenderState(graphics, mouseX, mouseY, a);
|
| 299 | }
|
| 300 | }
|
| 301 | }
|
| 302 | }
|
| 303 |
|