Spaces:
Build error
Build error
File size: 14,453 Bytes
d46f4a3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 |
package com.mojang.realmsclient.gui.screens;
import com.mojang.logging.LogUtils;
import com.mojang.realmsclient.client.RealmsClient;
import com.mojang.realmsclient.dto.Backup;
import com.mojang.realmsclient.dto.RealmsServer;
import com.mojang.realmsclient.exception.RealmsServiceException;
import com.mojang.realmsclient.util.RealmsUtil;
import com.mojang.realmsclient.util.task.DownloadTask;
import com.mojang.realmsclient.util.task.RestoreTask;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.function.Supplier;
import javax.annotation.Nullable;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.AbstractWidget;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.components.ContainerObjectSelectionList;
import net.minecraft.client.gui.components.PopupScreen;
import net.minecraft.client.gui.components.events.GuiEventListener;
import net.minecraft.client.gui.layouts.HeaderAndFooterLayout;
import net.minecraft.client.gui.layouts.LinearLayout;
import net.minecraft.client.gui.narration.NarratableEntry;
import net.minecraft.network.chat.CommonComponents;
import net.minecraft.network.chat.Component;
import net.minecraft.realms.RealmsScreen;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import org.slf4j.Logger;
@OnlyIn(Dist.CLIENT)
public class RealmsBackupScreen extends RealmsScreen {
static final Logger LOGGER = LogUtils.getLogger();
private static final Component TITLE = Component.translatable("mco.configure.world.backup");
static final Component RESTORE_TOOLTIP = Component.translatable("mco.backup.button.restore");
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;
final RealmsConfigureWorldScreen lastScreen;
List<Backup> backups = Collections.emptyList();
@Nullable
RealmsBackupScreen.BackupObjectSelectionList backupList;
final HeaderAndFooterLayout layout = new HeaderAndFooterLayout(this);
private final int slotId;
@Nullable
Button downloadButton;
final RealmsServer serverData;
boolean noBackups = false;
public RealmsBackupScreen(RealmsConfigureWorldScreen p_88126_, RealmsServer p_88127_, int p_88128_) {
super(TITLE);
this.lastScreen = p_88126_;
this.serverData = p_88127_;
this.slotId = p_88128_;
}
@Override
public void init() {
this.layout.addTitleHeader(TITLE, this.font);
this.backupList = this.layout.addToContents(new RealmsBackupScreen.BackupObjectSelectionList());
LinearLayout linearlayout = this.layout.addToFooter(LinearLayout.horizontal().spacing(8));
this.downloadButton = linearlayout.addChild(Button.builder(DOWNLOAD_LATEST, p_88185_ -> this.downloadClicked()).build());
this.downloadButton.active = false;
linearlayout.addChild(Button.builder(CommonComponents.GUI_BACK, p_325106_ -> this.onClose()).build());
this.layout.visitWidgets(p_325105_ -> {
AbstractWidget abstractwidget = this.addRenderableWidget(p_325105_);
});
this.repositionElements();
this.fetchRealmsBackups();
}
@Override
public void render(GuiGraphics p_283405_, int p_282020_, int p_282404_, float p_281280_) {
super.render(p_283405_, p_282020_, p_282404_, p_281280_);
if (this.noBackups && this.backupList != null) {
p_283405_.drawString(
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") {
@Override
public void run() {
RealmsClient realmsclient = RealmsClient.create();
try {
List<Backup> list = realmsclient.backupsFor(RealmsBackupScreen.this.serverData.id).backups;
RealmsBackupScreen.this.minecraft
.execute(
() -> {
RealmsBackupScreen.this.backups = list;
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(p_357551_ -> RealmsBackupScreen.this.new Entry(p_357551_))
.toList()
);
}
}
);
} catch (RealmsServiceException realmsserviceexception) {
RealmsBackupScreen.LOGGER.error("Couldn't request backups", (Throwable)realmsserviceexception);
}
}
})
.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"),
p_357549_ -> 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).getSlotName(this.serverData.activeSlot)
+ ")",
this
)
)
)
)
);
}
@OnlyIn(Dist.CLIENT)
class BackupObjectSelectionList extends ContainerObjectSelectionList<RealmsBackupScreen.Entry> {
private static final int ITEM_HEIGHT = 36;
public BackupObjectSelectionList() {
super(
Minecraft.getInstance(),
RealmsBackupScreen.this.width,
RealmsBackupScreen.this.layout.getContentHeight(),
RealmsBackupScreen.this.layout.getHeaderHeight(),
36
);
}
@Override
public int getRowWidth() {
return 300;
}
}
@OnlyIn(Dist.CLIENT)
class Entry extends ContainerObjectSelectionList.Entry<RealmsBackupScreen.Entry> {
private static final int Y_PADDING = 2;
private final Backup backup;
@Nullable
private Button restoreButton;
@Nullable
private Button changesButton;
private final List<AbstractWidget> children = new ArrayList<>();
public Entry(final Backup p_88250_) {
this.backup = p_88250_;
this.populateChangeList(p_88250_);
if (!p_88250_.changeList.isEmpty()) {
this.changesButton = Button.builder(
RealmsBackupScreen.HAS_CHANGES_TOOLTIP,
p_340707_ -> RealmsBackupScreen.this.minecraft.setScreen(new RealmsBackupInfoScreen(RealmsBackupScreen.this, this.backup))
)
.width(8 + RealmsBackupScreen.this.font.width(RealmsBackupScreen.HAS_CHANGES_TOOLTIP))
.createNarration(p_325109_ -> CommonComponents.joinForNarration(Component.translatable("mco.backup.narration", this.getShortBackupDate()), p_325109_.get()))
.build();
this.children.add(this.changesButton);
}
if (!RealmsBackupScreen.this.serverData.expired) {
this.restoreButton = Button.builder(RealmsBackupScreen.RESTORE_TOOLTIP, p_325108_ -> this.restoreClicked())
.width(8 + RealmsBackupScreen.this.font.width(RealmsBackupScreen.HAS_CHANGES_TOOLTIP))
.createNarration(p_325111_ -> CommonComponents.joinForNarration(Component.translatable("mco.backup.narration", this.getShortBackupDate()), p_325111_.get()))
.build();
this.children.add(this.restoreButton);
}
}
private void populateChangeList(Backup p_279365_) {
int i = RealmsBackupScreen.this.backups.indexOf(p_279365_);
if (i != RealmsBackupScreen.this.backups.size() - 1) {
Backup backup = RealmsBackupScreen.this.backups.get(i + 1);
for (String s : p_279365_.metadata.keySet()) {
if (!s.contains("uploaded") && backup.metadata.containsKey(s)) {
if (!p_279365_.metadata.get(s).equals(backup.metadata.get(s))) {
this.addToChangeList(s);
}
} else {
this.addToChangeList(s);
}
}
}
}
private void addToChangeList(String p_279195_) {
if (p_279195_.contains("uploaded")) {
String s = DateFormat.getDateTimeInstance(3, 3).format(this.backup.lastModifiedDate);
this.backup.changeList.put(p_279195_, s);
this.backup.setUploadedVersion(true);
} else {
this.backup.changeList.put(p_279195_, this.backup.metadata.get(p_279195_));
}
}
private String getShortBackupDate() {
return DateFormat.getDateTimeInstance(3, 3).format(this.backup.lastModifiedDate);
}
private void restoreClicked() {
Component component = RealmsUtil.convertToAgePresentationFromInstant(this.backup.lastModifiedDate);
Component component1 = Component.translatable("mco.configure.world.restore.question.line1", this.getShortBackupDate(), component);
RealmsBackupScreen.this.minecraft
.setScreen(
RealmsPopups.warningPopupScreen(
RealmsBackupScreen.this,
component1,
p_340708_ -> RealmsBackupScreen.this.minecraft
.setScreen(
new RealmsLongRunningMcoTaskScreen(
RealmsBackupScreen.this.lastScreen.getNewScreen(),
new RestoreTask(this.backup, RealmsBackupScreen.this.serverData.id, RealmsBackupScreen.this.lastScreen)
)
)
)
);
}
@Override
public List<? extends GuiEventListener> children() {
return this.children;
}
@Override
public List<? extends NarratableEntry> narratables() {
return this.children;
}
@Override
public void render(
GuiGraphics p_281408_,
int p_281974_,
int p_282495_,
int p_282463_,
int p_281562_,
int p_282782_,
int p_281638_,
int p_283190_,
boolean p_283105_,
float p_282066_
) {
int i = p_282495_ + p_282782_ / 2;
int j = i - 9 - 2;
int k = i + 2;
int l = this.backup.isUploadedVersion() ? -8388737 : -1;
p_281408_.drawString(
RealmsBackupScreen.this.font, Component.translatable("mco.backup.entry", RealmsUtil.convertToAgePresentationFromInstant(this.backup.lastModifiedDate)), p_282463_, j, l
);
p_281408_.drawString(RealmsBackupScreen.this.font, this.getMediumDatePresentation(this.backup.lastModifiedDate), p_282463_, k, 5000268);
int i1 = 0;
int j1 = p_282495_ + p_282782_ / 2 - 10;
if (this.restoreButton != null) {
i1 += this.restoreButton.getWidth() + 8;
this.restoreButton.setX(p_282463_ + p_281562_ - i1);
this.restoreButton.setY(j1);
this.restoreButton.render(p_281408_, p_281638_, p_283190_, p_282066_);
}
if (this.changesButton != null) {
i1 += this.changesButton.getWidth() + 8;
this.changesButton.setX(p_282463_ + p_281562_ - i1);
this.changesButton.setY(j1);
this.changesButton.render(p_281408_, p_281638_, p_283190_, p_282066_);
}
}
private String getMediumDatePresentation(Date p_88276_) {
return DateFormat.getDateTimeInstance(3, 3).format(p_88276_);
}
}
} |