RealmsBackupScreen.java

com.mojang.realmsclient.gui.screens.configuration.RealmsBackupScreen

信息

  • 全限定名:com.mojang.realmsclient.gui.screens.configuration.RealmsBackupScreen
  • 类型:public class
  • 包:com.mojang.realmsclient.gui.screens.configuration
  • 源码路径:src/main/java/com/mojang/realmsclient/gui/screens/configuration/RealmsBackupScreen.java
  • 起始行号:L40
  • 继承:RealmsScreen
  • 职责:

    TODO

字段/常量

  • LOGGER

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

      TODO

  • TITLE

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

      TODO

  • RESTORE_TOOLTIP

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

      TODO

  • HAS_CHANGES_TOOLTIP

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

      TODO

  • NO_BACKUPS_LABEL

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

      TODO

  • DOWNLOAD_LATEST

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

      TODO

  • UPLOADED_KEY

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

      TODO

  • PADDING

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

      TODO

  • SHORT_DATE_FORMAT

    • 类型: DateTimeFormatter
    • 修饰符: public static final
    • 源码定位: L49
    • 说明:

      TODO

  • lastScreen

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

      TODO

  • backups

    • 类型: List<Backup>
    • 修饰符: private
    • 源码定位: L51
    • 说明:

      TODO

  • backupList

    • 类型: RealmsBackupScreen.BackupObjectSelectionList
    • 修饰符: private
    • 源码定位: L52
    • 说明:

      TODO

  • layout

    • 类型: HeaderAndFooterLayout
    • 修饰符: private final
    • 源码定位: L53
    • 说明:

      TODO

  • slotId

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

      TODO

  • downloadButton

    • 类型: Button
    • 修饰符: private
    • 源码定位: L55
    • 说明:

      TODO

  • serverData

    • 类型: RealmsServer
    • 修饰符: private final
    • 源码定位: L56
    • 说明:

      TODO

  • noBackups

    • 类型: boolean
    • 修饰符: private
    • 源码定位: L57
    • 说明:

      TODO

内部类/嵌套类型

  • com.mojang.realmsclient.gui.screens.configuration.RealmsBackupScreen.BackupObjectSelectionList

    • 类型: class
    • 修饰符: private
    • 源码定位: L169
    • 说明:

      TODO

  • com.mojang.realmsclient.gui.screens.configuration.RealmsBackupScreen.Entry

    • 类型: class
    • 修饰符: private
    • 源码定位: L190
    • 说明:

      TODO

构造器

public RealmsBackupScreen(RealmsConfigureWorldScreen lastScreen, RealmsServer serverData, int slotId) @ L59

  • 构造器名:RealmsBackupScreen
  • 源码定位:L59
  • 修饰符:public

参数:

  • lastScreen: RealmsConfigureWorldScreen
  • serverData: RealmsServer
  • slotId: int

说明:

TODO

方法

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

public void init() @ L66

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

参数:

说明:

TODO

public void extractRenderState(GuiGraphicsExtractor graphics, int mouseX, int mouseY, float a) @ L79

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

参数:

  • graphics: GuiGraphicsExtractor
  • mouseX: int
  • mouseY: int
  • a: float

说明:

TODO

protected void repositionElements() @ L93

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

参数:

说明:

TODO

private void fetchRealmsBackups() @ L101

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

参数:

说明:

TODO

public void onClose() @ L138

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

参数:

说明:

TODO

private void downloadClicked() @ L143

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

参数:

说明:

TODO

代码

@OnlyIn(Dist.CLIENT)
public class RealmsBackupScreen extends RealmsScreen {
    private static final Logger LOGGER = LogUtils.getLogger();
    private static final Component TITLE = Component.translatable("mco.configure.world.backup");
    private static final Component RESTORE_TOOLTIP = Component.translatable("mco.backup.button.restore");
    private static final Component HAS_CHANGES_TOOLTIP = Component.translatable("mco.backup.changes.tooltip");
    private static final Component NO_BACKUPS_LABEL = Component.translatable("mco.backup.nobackups");
    private static final Component DOWNLOAD_LATEST = Component.translatable("mco.backup.button.download");
    private static final String UPLOADED_KEY = "uploaded";
    private static final int PADDING = 8;
    public static final DateTimeFormatter SHORT_DATE_FORMAT = Util.localizedDateFormatter(FormatStyle.SHORT);
    private final RealmsConfigureWorldScreen lastScreen;
    private List<Backup> backups = Collections.emptyList();
    private RealmsBackupScreen.@Nullable BackupObjectSelectionList backupList;
    private final HeaderAndFooterLayout layout = new HeaderAndFooterLayout(this);
    private final int slotId;
    private @Nullable Button downloadButton;
    private final RealmsServer serverData;
    private boolean noBackups = false;
 
    public RealmsBackupScreen(RealmsConfigureWorldScreen lastScreen, RealmsServer serverData, int slotId) {
        super(TITLE);
        this.lastScreen = lastScreen;
        this.serverData = serverData;
        this.slotId = slotId;
    }
 
    @Override
    public void init() {
        this.layout.addTitleHeader(TITLE, this.font);
        this.backupList = this.layout.addToContents(new RealmsBackupScreen.BackupObjectSelectionList());
        LinearLayout footer = this.layout.addToFooter(LinearLayout.horizontal().spacing(8));
        this.downloadButton = footer.addChild(Button.builder(DOWNLOAD_LATEST, button -> this.downloadClicked()).build());
        this.downloadButton.active = false;
        footer.addChild(Button.builder(CommonComponents.GUI_BACK, button -> this.onClose()).build());
        this.layout.visitWidgets(x$0 -> this.addRenderableWidget(x$0));
        this.repositionElements();
        this.fetchRealmsBackups();
    }
 
    @Override
    public void extractRenderState(GuiGraphicsExtractor graphics, int mouseX, int mouseY, float a) {
        super.extractRenderState(graphics, mouseX, mouseY, a);
        if (this.noBackups && this.backupList != null) {
            graphics.text(
                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
            );
        }
    }
 
    @Override
    protected void repositionElements() {
        this.layout.arrangeElements();
        if (this.backupList != null) {
            this.backupList.updateSize(this.width, this.layout);
        }
    }
 
    private void fetchRealmsBackups() {
        (new Thread("Realms-fetch-backups") {
                {
                    Objects.requireNonNull(RealmsBackupScreen.this);
                }
 
                @Override
                public void run() {
                    RealmsClient client = RealmsClient.getOrCreate();
 
                    try {
                        List<Backup> backups = client.backupsFor(RealmsBackupScreen.this.serverData.id).backups();
                        RealmsBackupScreen.this.minecraft
                            .execute(
                                () -> {
                                    RealmsBackupScreen.this.backups = backups;
                                    RealmsBackupScreen.this.noBackups = RealmsBackupScreen.this.backups.isEmpty();
                                    if (!RealmsBackupScreen.this.noBackups && RealmsBackupScreen.this.downloadButton != null) {
                                        RealmsBackupScreen.this.downloadButton.active = true;
                                    }
 
                                    if (RealmsBackupScreen.this.backupList != null) {
                                        RealmsBackupScreen.this.backupList
                                            .replaceEntries(
                                                RealmsBackupScreen.this.backups.stream().map(x$0 -> RealmsBackupScreen.this.new Entry(x$0)).toList()
                                            );
                                    }
                                }
                            );
                    } catch (RealmsServiceException var3) {
                        RealmsBackupScreen.LOGGER.error("Couldn't request backups", (Throwable)var3);
                    }
                }
            })
            .start();
    }
 
    @Override
    public void onClose() {
        this.minecraft.setScreen(this.lastScreen);
    }
 
    private void downloadClicked() {
        this.minecraft
            .setScreen(
                RealmsPopups.infoPopupScreen(
                    this,
                    Component.translatable("mco.configure.world.restore.download.question.line1"),
                    popup -> this.minecraft
                        .setScreen(
                            new RealmsLongRunningMcoTaskScreen(
                                this.lastScreen.getNewScreen(),
                                new DownloadTask(
                                    this.serverData.id,
                                    this.slotId,
                                    Objects.requireNonNullElse(this.serverData.name, "")
                                        + " ("
                                        + this.serverData.slots.get(this.serverData.activeSlot).options.getSlotName(this.serverData.activeSlot)
                                        + ")",
                                    this
                                )
                            )
                        )
                )
            );
    }
 
    @OnlyIn(Dist.CLIENT)
    private class BackupObjectSelectionList extends ContainerObjectSelectionList<RealmsBackupScreen.Entry> {
        private static final int ITEM_HEIGHT = 36;
 
        public BackupObjectSelectionList() {
            Objects.requireNonNull(RealmsBackupScreen.this);
            super(
                Minecraft.getInstance(),
                RealmsBackupScreen.this.width,
                RealmsBackupScreen.this.layout.getContentHeight(),
                RealmsBackupScreen.this.layout.getHeaderHeight(),
                36
            );
        }
 
        @Override
        public int getRowWidth() {
            return 300;
        }
    }
 
    @OnlyIn(Dist.CLIENT)
    private class Entry extends ContainerObjectSelectionList.Entry<RealmsBackupScreen.Entry> {
        private static final int Y_PADDING = 2;
        private final Backup backup;
        private @Nullable Button restoreButton;
        private @Nullable Button changesButton;
        private final List<AbstractWidget> children;
 
        public Entry(Backup backup) {
            Objects.requireNonNull(RealmsBackupScreen.this);
            super();
            this.children = new ArrayList<>();
            this.backup = backup;
            this.populateChangeList(backup);
            if (!backup.changeList.isEmpty()) {
                this.changesButton = Button.builder(
                        RealmsBackupScreen.HAS_CHANGES_TOOLTIP,
                        button -> RealmsBackupScreen.this.minecraft.setScreen(new RealmsBackupInfoScreen(RealmsBackupScreen.this, this.backup))
                    )
                    .width(8 + RealmsBackupScreen.this.font.width(RealmsBackupScreen.HAS_CHANGES_TOOLTIP))
                    .createNarration(this::narrationForBackupEntry)
                    .build();
                this.children.add(this.changesButton);
            }
 
            if (!RealmsBackupScreen.this.serverData.expired) {
                this.restoreButton = Button.builder(RealmsBackupScreen.RESTORE_TOOLTIP, button -> this.restoreClicked())
                    .width(8 + RealmsBackupScreen.this.font.width(RealmsBackupScreen.HAS_CHANGES_TOOLTIP))
                    .createNarration(this::narrationForBackupEntry)
                    .build();
                this.children.add(this.restoreButton);
            }
        }
 
        private MutableComponent narrationForBackupEntry(Supplier<MutableComponent> defaultNarrationSupplier) {
            return CommonComponents.joinForNarration(
                Component.translatable("mco.backup.narration", RealmsBackupScreen.SHORT_DATE_FORMAT.format(this.backup.lastModifiedDate())),
                defaultNarrationSupplier.get()
            );
        }
 
        private void populateChangeList(Backup backup) {
            int index = RealmsBackupScreen.this.backups.indexOf(backup);
            if (index != RealmsBackupScreen.this.backups.size() - 1) {
                Backup olderBackup = RealmsBackupScreen.this.backups.get(index + 1);
 
                for (String key : backup.metadata.keySet()) {
                    if (!key.contains("uploaded") && olderBackup.metadata.containsKey(key)) {
                        if (!backup.metadata.get(key).equals(olderBackup.metadata.get(key))) {
                            this.addToChangeList(key);
                        }
                    } else {
                        this.addToChangeList(key);
                    }
                }
            }
        }
 
        private void addToChangeList(String key) {
            if (key.contains("uploaded")) {
                String uploadedTime = RealmsBackupScreen.SHORT_DATE_FORMAT.format(this.backup.lastModifiedDate());
                this.backup.changeList.put(key, uploadedTime);
                this.backup.uploadedVersion = true;
            } else {
                this.backup.changeList.put(key, this.backup.metadata.get(key));
            }
        }
 
        private void restoreClicked() {
            Component age = RealmsUtil.convertToAgePresentationFromInstant(this.backup.lastModified);
            String lastModifiedDate = RealmsBackupScreen.SHORT_DATE_FORMAT.format(this.backup.lastModifiedDate());
            Component popupMessage = Component.translatable("mco.configure.world.restore.question.line1", lastModifiedDate, age);
            RealmsBackupScreen.this.minecraft
                .setScreen(
                    RealmsPopups.warningPopupScreen(
                        RealmsBackupScreen.this,
                        popupMessage,
                        popup -> {
                            RealmsConfigureWorldScreen newScreen = RealmsBackupScreen.this.lastScreen.getNewScreen();
                            RealmsBackupScreen.this.minecraft
                                .setScreen(
                                    new RealmsLongRunningMcoTaskScreen(
                                        newScreen, new RestoreTask(this.backup, RealmsBackupScreen.this.serverData.id, newScreen)
                                    )
                                );
                        }
                    )
                );
        }
 
        @Override
        public List<? extends GuiEventListener> children() {
            return this.children;
        }
 
        @Override
        public List<? extends NarratableEntry> narratables() {
            return this.children;
        }
 
        @Override
        public void extractContent(GuiGraphicsExtractor graphics, int mouseX, int mouseY, boolean hovered, float a) {
            int middle = this.getContentYMiddle();
            int firstLineYPos = middle - 9 - 2;
            int secondLineYPos = middle + 2;
            int color = this.backup.uploadedVersion ? -8388737 : -1;
            graphics.text(
                RealmsBackupScreen.this.font,
                Component.translatable("mco.backup.entry", RealmsUtil.convertToAgePresentationFromInstant(this.backup.lastModified)),
                this.getContentX(),
                firstLineYPos,
                color
            );
            graphics.text(
                RealmsBackupScreen.this.font,
                RealmsBackupScreen.SHORT_DATE_FORMAT.format(this.backup.lastModifiedDate()),
                this.getContentX(),
                secondLineYPos,
                -11776948
            );
            int iconXOffet = 0;
            int iconYPos = this.getContentYMiddle() - 10;
            if (this.restoreButton != null) {
                iconXOffet += this.restoreButton.getWidth() + 8;
                this.restoreButton.setX(this.getContentRight() - iconXOffet);
                this.restoreButton.setY(iconYPos);
                this.restoreButton.extractRenderState(graphics, mouseX, mouseY, a);
            }
 
            if (this.changesButton != null) {
                iconXOffet += this.changesButton.getWidth() + 8;
                this.changesButton.setX(this.getContentRight() - iconXOffet);
                this.changesButton.setY(iconYPos);
                this.changesButton.extractRenderState(graphics, mouseX, mouseY, a);
            }
        }
    }
}

引用的其他类

  • RealmsClient

    • 引用位置: 方法调用
    • 关联成员: RealmsClient.getOrCreate()
  • Backup

    • 引用位置: 字段
  • RealmsServer

    • 引用位置: 参数/字段
  • RealmsLongRunningMcoTaskScreen

    • 引用位置: 构造调用
    • 关联成员: RealmsLongRunningMcoTaskScreen()
  • RealmsPopups

    • 引用位置: 方法调用
    • 关联成员: RealmsPopups.infoPopupScreen(), RealmsPopups.warningPopupScreen()
  • RealmsBackupInfoScreen

    • 引用位置: 构造调用
    • 关联成员: RealmsBackupInfoScreen()
  • RealmsConfigureWorldScreen

    • 引用位置: 参数/字段
  • RealmsUtil

    • 引用位置: 方法调用
    • 关联成员: RealmsUtil.convertToAgePresentationFromInstant()
  • DownloadTask

    • 引用位置: 构造调用
    • 关联成员: DownloadTask()
  • RestoreTask

    • 引用位置: 构造调用
    • 关联成员: RestoreTask()
  • Minecraft

    • 引用位置: 方法调用
    • 关联成员: Minecraft.getInstance()
  • GuiGraphicsExtractor

    • 引用位置: 参数
  • Button

    • 引用位置: 字段/方法调用
    • 关联成员: Button.builder()
  • HeaderAndFooterLayout

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

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

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

    • 引用位置: 字段/方法调用
    • 关联成员: Component.translatable()
  • RealmsScreen

    • 引用位置: 继承
  • Util

    • 引用位置: 方法调用
    • 关联成员: Util.localizedDateFormatter()