| 1 | package net.minecraft.client.gui.screens;
|
| 2 |
|
| 3 | import com.mojang.logging.LogUtils;
|
| 4 | import it.unimi.dsi.fastutil.booleans.BooleanConsumer;
|
| 5 | import java.io.FileNotFoundException;
|
| 6 | import java.io.IOException;
|
| 7 | import java.time.Instant;
|
| 8 | import java.time.ZoneId;
|
| 9 | import java.time.ZonedDateTime;
|
| 10 | import net.fabricmc.api.EnvType;
|
| 11 | import net.fabricmc.api.Environment;
|
| 12 | import net.minecraft.ChatFormatting;
|
| 13 | import net.minecraft.client.Minecraft;
|
| 14 | import net.minecraft.client.gui.components.Button;
|
| 15 | import net.minecraft.client.gui.components.MultiLineTextWidget;
|
| 16 | import net.minecraft.client.gui.components.StringWidget;
|
| 17 | import net.minecraft.client.gui.components.Tooltip;
|
| 18 | import net.minecraft.client.gui.layouts.FrameLayout;
|
| 19 | import net.minecraft.client.gui.layouts.LinearLayout;
|
| 20 | import net.minecraft.client.gui.screens.worldselection.EditWorldScreen;
|
| 21 | import net.minecraft.client.gui.screens.worldselection.WorldSelectionList;
|
| 22 | import net.minecraft.nbt.NbtException;
|
| 23 | import net.minecraft.nbt.ReportedNbtException;
|
| 24 | import net.minecraft.network.chat.CommonComponents;
|
| 25 | import net.minecraft.network.chat.Component;
|
| 26 | import net.minecraft.network.chat.MutableComponent;
|
| 27 | import net.minecraft.util.CommonLinks;
|
| 28 | import net.minecraft.world.level.storage.LevelStorageSource.LevelStorageAccess;
|
| 29 | import org.jspecify.annotations.Nullable;
|
| 30 | import org.slf4j.Logger;
|
| 31 |
|
| 32 | @Environment(EnvType.CLIENT)
|
| 33 | public class RecoverWorldDataScreen extends Screen {
|
| 34 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 35 | private static final int SCREEN_SIDE_MARGIN = 25;
|
| 36 | private static final Component TITLE = Component.translatable("recover_world.title").withStyle(ChatFormatting.BOLD);
|
| 37 | private static final Component BUGTRACKER_BUTTON = Component.translatable("recover_world.bug_tracker");
|
| 38 | private static final Component RESTORE_BUTTON = Component.translatable("recover_world.restore");
|
| 39 | private static final Component NO_FALLBACK_TOOLTIP = Component.translatable("recover_world.no_fallback");
|
| 40 | private static final Component DONE_TITLE = Component.translatable("recover_world.done.title");
|
| 41 | private static final Component DONE_SUCCESS = Component.translatable("recover_world.done.success");
|
| 42 | private static final Component DONE_FAILED = Component.translatable("recover_world.done.failed");
|
| 43 | private static final Component NO_ISSUES = Component.translatable("recover_world.issue.none").withStyle(ChatFormatting.GREEN);
|
| 44 | private static final Component MISSING_FILE = Component.translatable("recover_world.issue.missing_file").withStyle(ChatFormatting.RED);
|
| 45 | private final BooleanConsumer callback;
|
| 46 | private final LinearLayout layout = LinearLayout.vertical().spacing(8);
|
| 47 | private final Component message;
|
| 48 | private final MultiLineTextWidget messageWidget;
|
| 49 | private final MultiLineTextWidget issuesWidget;
|
| 50 | private final LevelStorageAccess storageAccess;
|
| 51 |
|
| 52 | public RecoverWorldDataScreen(final Minecraft minecraft, final BooleanConsumer callback, final LevelStorageAccess storageAccess) {
|
| 53 | super(TITLE);
|
| 54 | this.callback = callback;
|
| 55 | this.message = Component.translatable("recover_world.message", Component.literal(storageAccess.getLevelId()).withStyle(ChatFormatting.GRAY));
|
| 56 | this.messageWidget = new MultiLineTextWidget(this.message, minecraft.font);
|
| 57 | this.storageAccess = storageAccess;
|
| 58 | Exception levelDatIssues = this.collectIssue(storageAccess, false);
|
| 59 | Exception levelDatOldIssues = this.collectIssue(storageAccess, true);
|
| 60 | Component issues = Component.empty()
|
| 61 | .append(this.buildInfo(storageAccess, false, levelDatIssues))
|
| 62 | .append("\n")
|
| 63 | .append(this.buildInfo(storageAccess, true, levelDatOldIssues));
|
| 64 | this.issuesWidget = new MultiLineTextWidget(issues, minecraft.font);
|
| 65 | boolean canRecover = levelDatIssues != null && levelDatOldIssues == null;
|
| 66 | this.layout.defaultCellSetting().alignHorizontallyCenter();
|
| 67 | this.layout.addChild(new StringWidget(this.title, minecraft.font));
|
| 68 | this.layout.addChild(this.messageWidget.setCentered(true));
|
| 69 | this.layout.addChild(this.issuesWidget);
|
| 70 | LinearLayout buttonGrid = LinearLayout.horizontal().spacing(5);
|
| 71 | buttonGrid.addChild(Button.builder(BUGTRACKER_BUTTON, ConfirmLinkScreen.confirmLink(this, CommonLinks.SNAPSHOT_BUGS_FEEDBACK)).size(120, 20).build());
|
| 72 | buttonGrid.addChild(
|
| 73 | Button.builder(RESTORE_BUTTON, button -> this.attemptRestore(minecraft))
|
| 74 | .size(120, 20)
|
| 75 | .tooltip(canRecover ? null : Tooltip.create(NO_FALLBACK_TOOLTIP))
|
| 76 | .build()
|
| 77 | )
|
| 78 | .active = canRecover;
|
| 79 | this.layout.addChild(buttonGrid);
|
| 80 | this.layout.addChild(Button.builder(CommonComponents.GUI_BACK, button -> this.onClose()).size(120, 20).build());
|
| 81 | this.layout.visitWidgets(this::addRenderableWidget);
|
| 82 | }
|
| 83 |
|
| 84 | private void attemptRestore(final Minecraft minecraft) {
|
| 85 | Exception current = this.collectIssue(this.storageAccess, false);
|
| 86 | Exception old = this.collectIssue(this.storageAccess, true);
|
| 87 | if (current != null && old == null) {
|
| 88 | minecraft.setScreenAndShow(new GenericMessageScreen(Component.translatable("recover_world.restoring")));
|
| 89 | EditWorldScreen.makeBackupAndShowToast(this.storageAccess).thenAcceptAsync(var2x -> {
|
| 90 | if (this.storageAccess.restoreLevelDataFromOld()) {
|
| 91 | minecraft.gui.setScreen(new ConfirmScreen(this.callback, DONE_TITLE, DONE_SUCCESS, CommonComponents.GUI_CONTINUE, CommonComponents.GUI_BACK));
|
| 92 | } else {
|
| 93 | minecraft.gui.setScreen(new AlertScreen(() -> this.callback.accept(false), DONE_TITLE, DONE_FAILED));
|
| 94 | }
|
| 95 | }, minecraft);
|
| 96 | } else {
|
| 97 | LOGGER.error(
|
| 98 | "Failed to recover world, files not as expected. level.dat: {}, level.dat_old: {}",
|
| 99 | current != null ? current.getMessage() : "no issues",
|
| 100 | old != null ? old.getMessage() : "no issues"
|
| 101 | );
|
| 102 | minecraft.gui.setScreen(new AlertScreen(() -> this.callback.accept(false), DONE_TITLE, DONE_FAILED));
|
| 103 | }
|
| 104 | }
|
| 105 |
|
| 106 | private Component buildInfo(final LevelStorageAccess access, final boolean fallback, final @Nullable Exception exception) {
|
| 107 | if (fallback && exception instanceof FileNotFoundException) {
|
| 108 | return Component.empty();
|
| 109 | }
|
| 110 |
|
| 111 | MutableComponent component = Component.empty();
|
| 112 | Instant timeStamp = access.getFileModificationTime(fallback);
|
| 113 | MutableComponent time = timeStamp != null
|
| 114 | ? Component.literal(WorldSelectionList.DATE_FORMAT.format(ZonedDateTime.ofInstant(timeStamp, ZoneId.systemDefault())))
|
| 115 | : Component.translatable("recover_world.state_entry.unknown");
|
| 116 | component.append(Component.translatable("recover_world.state_entry", time.withStyle(ChatFormatting.GRAY)));
|
| 117 | if (exception == null) {
|
| 118 | component.append(NO_ISSUES);
|
| 119 | } else if (exception instanceof FileNotFoundException) {
|
| 120 | component.append(MISSING_FILE);
|
| 121 | } else if (exception instanceof ReportedNbtException) {
|
| 122 | component.append(Component.literal(exception.getCause().toString()).withStyle(ChatFormatting.RED));
|
| 123 | } else {
|
| 124 | component.append(Component.literal(exception.toString()).withStyle(ChatFormatting.RED));
|
| 125 | }
|
| 126 |
|
| 127 | return component;
|
| 128 | }
|
| 129 |
|
| 130 | private @Nullable Exception collectIssue(final LevelStorageAccess access, final boolean useFallback) {
|
| 131 | try {
|
| 132 | access.collectIssues(useFallback);
|
| 133 | return null;
|
| 134 | } catch (IOException | NbtException | ReportedNbtException e) {
|
| 135 | return e;
|
| 136 | }
|
| 137 | }
|
| 138 |
|
| 139 | @Override
|
| 140 | protected void init() {
|
| 141 | super.init();
|
| 142 | this.repositionElements();
|
| 143 | }
|
| 144 |
|
| 145 | @Override
|
| 146 | protected void repositionElements() {
|
| 147 | this.issuesWidget.setMaxWidth(this.width - 50);
|
| 148 | this.messageWidget.setMaxWidth(this.width - 50);
|
| 149 | this.layout.arrangeElements();
|
| 150 | FrameLayout.centerInRectangle(this.layout, this.getRectangle());
|
| 151 | }
|
| 152 |
|
| 153 | @Override
|
| 154 | public Component getNarrationMessage() {
|
| 155 | return CommonComponents.joinForNarration(super.getNarrationMessage(), this.message);
|
| 156 | }
|
| 157 |
|
| 158 | @Override
|
| 159 | public void onClose() {
|
| 160 | this.callback.accept(false);
|
| 161 | }
|
| 162 | }
|
| 163 |
|