RecoverWorldDataScreen.java

net.minecraft.client.gui.screens.RecoverWorldDataScreen

信息

  • 全限定名:net.minecraft.client.gui.screens.RecoverWorldDataScreen
  • 类型:public class
  • 包:net.minecraft.client.gui.screens
  • 源码路径:src/main/java/net/minecraft/client/gui/screens/RecoverWorldDataScreen.java
  • 起始行号:L33
  • 继承:Screen
  • 职责:

    TODO

字段/常量

  • LOGGER

    • 类型: Logger
    • 修饰符: private static final
    • 源码定位: L34
    • 说明:

      TODO

  • SCREEN_SIDE_MARGIN

    • 类型: int
    • 修饰符: private static final
    • 源码定位: L35
    • 说明:

      TODO

  • TITLE

    • 类型: Component
    • 修饰符: private static final
    • 源码定位: L36
    • 说明:

      TODO

  • BUGTRACKER_BUTTON

    • 类型: Component
    • 修饰符: private static final
    • 源码定位: L37
    • 说明:

      TODO

  • RESTORE_BUTTON

    • 类型: Component
    • 修饰符: private static final
    • 源码定位: L38
    • 说明:

      TODO

  • NO_FALLBACK_TOOLTIP

    • 类型: Component
    • 修饰符: private static final
    • 源码定位: L39
    • 说明:

      TODO

  • DONE_TITLE

    • 类型: Component
    • 修饰符: private static final
    • 源码定位: L40
    • 说明:

      TODO

  • DONE_SUCCESS

    • 类型: Component
    • 修饰符: private static final
    • 源码定位: L41
    • 说明:

      TODO

  • DONE_FAILED

    • 类型: Component
    • 修饰符: private static final
    • 源码定位: L42
    • 说明:

      TODO

  • NO_ISSUES

    • 类型: Component
    • 修饰符: private static final
    • 源码定位: L43
    • 说明:

      TODO

  • MISSING_FILE

    • 类型: Component
    • 修饰符: private static final
    • 源码定位: L44
    • 说明:

      TODO

  • callback

    • 类型: BooleanConsumer
    • 修饰符: private final
    • 源码定位: L45
    • 说明:

      TODO

  • layout

    • 类型: LinearLayout
    • 修饰符: private final
    • 源码定位: L46
    • 说明:

      TODO

  • message

    • 类型: Component
    • 修饰符: private final
    • 源码定位: L47
    • 说明:

      TODO

  • messageWidget

    • 类型: MultiLineTextWidget
    • 修饰符: private final
    • 源码定位: L48
    • 说明:

      TODO

  • issuesWidget

    • 类型: MultiLineTextWidget
    • 修饰符: private final
    • 源码定位: L49
    • 说明:

      TODO

  • storageAccess

    • 类型: LevelStorageSource.LevelStorageAccess
    • 修饰符: private final
    • 源码定位: L50
    • 说明:

      TODO

内部类/嵌套类型

构造器

public RecoverWorldDataScreen(Minecraft minecraft, BooleanConsumer callback, LevelStorageSource.LevelStorageAccess storageAccess) @ L52

  • 构造器名:RecoverWorldDataScreen
  • 源码定位:L52
  • 修饰符:public

参数:

  • minecraft: Minecraft
  • callback: BooleanConsumer
  • storageAccess: LevelStorageSource.LevelStorageAccess

说明:

TODO

方法

下面的方法块按源码顺序生成。

private void attemptRestore(Minecraft minecraft) @ L84

  • 方法名:attemptRestore
  • 源码定位:L84
  • 返回类型:void
  • 修饰符:private

参数:

  • minecraft: Minecraft

说明:

TODO

private Component buildInfo(LevelStorageSource.LevelStorageAccess access, boolean fallback, Exception exception) @ L106

  • 方法名:buildInfo
  • 源码定位:L106
  • 返回类型:Component
  • 修饰符:private

参数:

  • access: LevelStorageSource.LevelStorageAccess
  • fallback: boolean
  • exception: Exception

说明:

TODO

private Exception collectIssue(LevelStorageSource.LevelStorageAccess access, boolean useFallback) @ L130

  • 方法名:collectIssue
  • 源码定位:L130
  • 返回类型:Exception
  • 修饰符:private

参数:

  • access: LevelStorageSource.LevelStorageAccess
  • useFallback: boolean

说明:

TODO

protected void init() @ L139

  • 方法名:init
  • 源码定位:L139
  • 返回类型:void
  • 修饰符:protected

参数:

说明:

TODO

protected void repositionElements() @ L145

  • 方法名:repositionElements
  • 源码定位:L145
  • 返回类型:void
  • 修饰符:protected

参数:

说明:

TODO

public Component getNarrationMessage() @ L153

  • 方法名:getNarrationMessage
  • 源码定位:L153
  • 返回类型:Component
  • 修饰符:public

参数:

说明:

TODO

public void onClose() @ L158

  • 方法名:onClose
  • 源码定位:L158
  • 返回类型:void
  • 修饰符:public

参数:

说明:

TODO

代码

@OnlyIn(Dist.CLIENT)
public class RecoverWorldDataScreen extends Screen {
    private static final Logger LOGGER = LogUtils.getLogger();
    private static final int SCREEN_SIDE_MARGIN = 25;
    private static final Component TITLE = Component.translatable("recover_world.title").withStyle(ChatFormatting.BOLD);
    private static final Component BUGTRACKER_BUTTON = Component.translatable("recover_world.bug_tracker");
    private static final Component RESTORE_BUTTON = Component.translatable("recover_world.restore");
    private static final Component NO_FALLBACK_TOOLTIP = Component.translatable("recover_world.no_fallback");
    private static final Component DONE_TITLE = Component.translatable("recover_world.done.title");
    private static final Component DONE_SUCCESS = Component.translatable("recover_world.done.success");
    private static final Component DONE_FAILED = Component.translatable("recover_world.done.failed");
    private static final Component NO_ISSUES = Component.translatable("recover_world.issue.none").withStyle(ChatFormatting.GREEN);
    private static final Component MISSING_FILE = Component.translatable("recover_world.issue.missing_file").withStyle(ChatFormatting.RED);
    private final BooleanConsumer callback;
    private final LinearLayout layout = LinearLayout.vertical().spacing(8);
    private final Component message;
    private final MultiLineTextWidget messageWidget;
    private final MultiLineTextWidget issuesWidget;
    private final LevelStorageSource.LevelStorageAccess storageAccess;
 
    public RecoverWorldDataScreen(Minecraft minecraft, BooleanConsumer callback, LevelStorageSource.LevelStorageAccess storageAccess) {
        super(TITLE);
        this.callback = callback;
        this.message = Component.translatable("recover_world.message", Component.literal(storageAccess.getLevelId()).withStyle(ChatFormatting.GRAY));
        this.messageWidget = new MultiLineTextWidget(this.message, minecraft.font);
        this.storageAccess = storageAccess;
        Exception levelDatIssues = this.collectIssue(storageAccess, false);
        Exception levelDatOldIssues = this.collectIssue(storageAccess, true);
        Component issues = Component.empty()
            .append(this.buildInfo(storageAccess, false, levelDatIssues))
            .append("\n")
            .append(this.buildInfo(storageAccess, true, levelDatOldIssues));
        this.issuesWidget = new MultiLineTextWidget(issues, minecraft.font);
        boolean canRecover = levelDatIssues != null && levelDatOldIssues == null;
        this.layout.defaultCellSetting().alignHorizontallyCenter();
        this.layout.addChild(new StringWidget(this.title, minecraft.font));
        this.layout.addChild(this.messageWidget.setCentered(true));
        this.layout.addChild(this.issuesWidget);
        LinearLayout buttonGrid = LinearLayout.horizontal().spacing(5);
        buttonGrid.addChild(Button.builder(BUGTRACKER_BUTTON, ConfirmLinkScreen.confirmLink(this, CommonLinks.SNAPSHOT_BUGS_FEEDBACK)).size(120, 20).build());
        buttonGrid.addChild(
                Button.builder(RESTORE_BUTTON, button -> this.attemptRestore(minecraft))
                    .size(120, 20)
                    .tooltip(canRecover ? null : Tooltip.create(NO_FALLBACK_TOOLTIP))
                    .build()
            )
            .active = canRecover;
        this.layout.addChild(buttonGrid);
        this.layout.addChild(Button.builder(CommonComponents.GUI_BACK, button -> this.onClose()).size(120, 20).build());
        this.layout.visitWidgets(this::addRenderableWidget);
    }
 
    private void attemptRestore(Minecraft minecraft) {
        Exception current = this.collectIssue(this.storageAccess, false);
        Exception old = this.collectIssue(this.storageAccess, true);
        if (current != null && old == null) {
            minecraft.setScreenAndShow(new GenericMessageScreen(Component.translatable("recover_world.restoring")));
            EditWorldScreen.makeBackupAndShowToast(this.storageAccess).thenAcceptAsync(var2x -> {
                if (this.storageAccess.restoreLevelDataFromOld()) {
                    minecraft.setScreen(new ConfirmScreen(this.callback, DONE_TITLE, DONE_SUCCESS, CommonComponents.GUI_CONTINUE, CommonComponents.GUI_BACK));
                } else {
                    minecraft.setScreen(new AlertScreen(() -> this.callback.accept(false), DONE_TITLE, DONE_FAILED));
                }
            }, minecraft);
        } else {
            LOGGER.error(
                "Failed to recover world, files not as expected. level.dat: {}, level.dat_old: {}",
                current != null ? current.getMessage() : "no issues",
                old != null ? old.getMessage() : "no issues"
            );
            minecraft.setScreen(new AlertScreen(() -> this.callback.accept(false), DONE_TITLE, DONE_FAILED));
        }
    }
 
    private Component buildInfo(LevelStorageSource.LevelStorageAccess access, boolean fallback, @Nullable Exception exception) {
        if (fallback && exception instanceof FileNotFoundException) {
            return Component.empty();
        } else {
            MutableComponent component = Component.empty();
            Instant timeStamp = access.getFileModificationTime(fallback);
            MutableComponent time = timeStamp != null
                ? Component.literal(WorldSelectionList.DATE_FORMAT.format(ZonedDateTime.ofInstant(timeStamp, ZoneId.systemDefault())))
                : Component.translatable("recover_world.state_entry.unknown");
            component.append(Component.translatable("recover_world.state_entry", time.withStyle(ChatFormatting.GRAY)));
            if (exception == null) {
                component.append(NO_ISSUES);
            } else if (exception instanceof FileNotFoundException) {
                component.append(MISSING_FILE);
            } else if (exception instanceof ReportedNbtException) {
                component.append(Component.literal(exception.getCause().toString()).withStyle(ChatFormatting.RED));
            } else {
                component.append(Component.literal(exception.toString()).withStyle(ChatFormatting.RED));
            }
 
            return component;
        }
    }
 
    private @Nullable Exception collectIssue(LevelStorageSource.LevelStorageAccess access, boolean useFallback) {
        try {
            access.collectIssues(useFallback);
            return null;
        } catch (NbtException | ReportedNbtException | IOException var4) {
            return var4;
        }
    }
 
    @Override
    protected void init() {
        super.init();
        this.repositionElements();
    }
 
    @Override
    protected void repositionElements() {
        this.issuesWidget.setMaxWidth(this.width - 50);
        this.messageWidget.setMaxWidth(this.width - 50);
        this.layout.arrangeElements();
        FrameLayout.centerInRectangle(this.layout, this.getRectangle());
    }
 
    @Override
    public Component getNarrationMessage() {
        return CommonComponents.joinForNarration(super.getNarrationMessage(), this.message);
    }
 
    @Override
    public void onClose() {
        this.callback.accept(false);
    }
}

引用的其他类

  • Minecraft

    • 引用位置: 参数
  • Button

    • 引用位置: 方法调用
    • 关联成员: Button.builder()
  • MultiLineTextWidget

    • 引用位置: 字段/构造调用
    • 关联成员: MultiLineTextWidget()
  • StringWidget

    • 引用位置: 构造调用
    • 关联成员: StringWidget()
  • Tooltip

    • 引用位置: 方法调用
    • 关联成员: Tooltip.create()
  • FrameLayout

    • 引用位置: 方法调用
    • 关联成员: FrameLayout.centerInRectangle()
  • LinearLayout

    • 引用位置: 字段/方法调用
    • 关联成员: LinearLayout.horizontal(), LinearLayout.vertical()
  • AlertScreen

    • 引用位置: 构造调用
    • 关联成员: AlertScreen()
  • ConfirmLinkScreen

    • 引用位置: 方法调用
    • 关联成员: ConfirmLinkScreen.confirmLink()
  • ConfirmScreen

    • 引用位置: 构造调用
    • 关联成员: ConfirmScreen()
  • GenericMessageScreen

    • 引用位置: 构造调用
    • 关联成员: GenericMessageScreen()
  • Screen

    • 引用位置: 继承
  • EditWorldScreen

    • 引用位置: 方法调用
    • 关联成员: EditWorldScreen.makeBackupAndShowToast()
  • CommonComponents

    • 引用位置: 方法调用
    • 关联成员: CommonComponents.joinForNarration()
  • Component

    • 引用位置: 字段/方法调用/返回值
    • 关联成员: Component.empty(), Component.literal(), Component.translatable()
  • LevelStorageSource

    • 引用位置: 参数/字段