| 1 | package com.mojang.realmsclient.gui.screens;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import com.google.common.util.concurrent.RateLimiter;
|
| 5 | import com.mojang.realmsclient.RealmsMainScreen;
|
| 6 | import com.mojang.realmsclient.Unit;
|
| 7 | import com.mojang.realmsclient.client.UploadStatus;
|
| 8 | import com.mojang.realmsclient.client.worldupload.RealmsUploadException;
|
| 9 | import com.mojang.realmsclient.client.worldupload.RealmsWorldUpload;
|
| 10 | import com.mojang.realmsclient.client.worldupload.RealmsWorldUploadStatusTracker;
|
| 11 | import com.mojang.realmsclient.dto.RealmsSetting;
|
| 12 | import com.mojang.realmsclient.dto.RealmsSlot;
|
| 13 | import com.mojang.realmsclient.dto.RealmsWorldOptions;
|
| 14 | import com.mojang.realmsclient.gui.screens.configuration.RealmsConfigureWorldScreen;
|
| 15 | import com.mojang.realmsclient.util.task.LongRunningTask;
|
| 16 | import com.mojang.realmsclient.util.task.RealmCreationTask;
|
| 17 | import com.mojang.realmsclient.util.task.SwitchSlotTask;
|
| 18 | import java.nio.file.Path;
|
| 19 | import java.util.ArrayList;
|
| 20 | import java.util.Arrays;
|
| 21 | import java.util.List;
|
| 22 | import java.util.Locale;
|
| 23 | import java.util.concurrent.CompletionException;
|
| 24 | import java.util.concurrent.atomic.AtomicReference;
|
| 25 | import net.fabricmc.api.EnvType;
|
| 26 | import net.fabricmc.api.Environment;
|
| 27 | import net.minecraft.client.GameNarrator;
|
| 28 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 29 | import net.minecraft.client.gui.components.Button;
|
| 30 | import net.minecraft.client.gui.layouts.HeaderAndFooterLayout;
|
| 31 | import net.minecraft.client.gui.screens.TitleScreen;
|
| 32 | import net.minecraft.client.input.KeyEvent;
|
| 33 | import net.minecraft.network.chat.CommonComponents;
|
| 34 | import net.minecraft.network.chat.Component;
|
| 35 | import net.minecraft.realms.RealmsScreen;
|
| 36 | import net.minecraft.world.level.storage.LevelSummary;
|
| 37 | import org.jspecify.annotations.Nullable;
|
| 38 |
|
| 39 | @Environment(EnvType.CLIENT)
|
| 40 | public class RealmsUploadScreen extends RealmsScreen implements RealmsWorldUploadStatusTracker {
|
| 41 | private static final int BAR_WIDTH = 200;
|
| 42 | private static final int BAR_TOP = 80;
|
| 43 | private static final int BAR_BOTTOM = 95;
|
| 44 | private static final int BAR_BORDER = 1;
|
| 45 | private static final String[] DOTS = new String[]{"", ".", ". .", ". . ."};
|
| 46 | private static final Component VERIFYING_TEXT = Component.translatable("mco.upload.verifying");
|
| 47 | private final RealmsResetWorldScreen lastScreen;
|
| 48 | private final LevelSummary selectedLevel;
|
| 49 | private final @Nullable RealmCreationTask realmCreationTask;
|
| 50 | private final long realmId;
|
| 51 | private final int slotId;
|
| 52 | private final AtomicReference<@Nullable RealmsWorldUpload> currentUpload = new AtomicReference<>();
|
| 53 | private final UploadStatus uploadStatus;
|
| 54 | private final RateLimiter narrationRateLimiter;
|
| 55 | private volatile Component @Nullable [] errorMessage;
|
| 56 | private volatile Component status = Component.translatable("mco.upload.preparing");
|
| 57 | private volatile @Nullable String progress;
|
| 58 | private volatile boolean cancelled;
|
| 59 | private volatile boolean uploadFinished;
|
| 60 | private volatile boolean showDots = true;
|
| 61 | private volatile boolean uploadStarted;
|
| 62 | private @Nullable Button backButton;
|
| 63 | private @Nullable Button cancelButton;
|
| 64 | private int tickCount;
|
| 65 | private final HeaderAndFooterLayout layout = new HeaderAndFooterLayout(this);
|
| 66 |
|
| 67 | public RealmsUploadScreen(
|
| 68 | final @Nullable RealmCreationTask realmCreationTask,
|
| 69 | final long realmId,
|
| 70 | final int slotId,
|
| 71 | final RealmsResetWorldScreen lastScreen,
|
| 72 | final LevelSummary selectedLevel
|
| 73 | ) {
|
| 74 | super(GameNarrator.NO_TITLE);
|
| 75 | this.realmCreationTask = realmCreationTask;
|
| 76 | this.realmId = realmId;
|
| 77 | this.slotId = slotId;
|
| 78 | this.lastScreen = lastScreen;
|
| 79 | this.selectedLevel = selectedLevel;
|
| 80 | this.uploadStatus = new UploadStatus();
|
| 81 | this.narrationRateLimiter = RateLimiter.create(0.1F);
|
| 82 | }
|
| 83 |
|
| 84 | @Override
|
| 85 | public void init() {
|
| 86 | this.backButton = this.layout.addToFooter(Button.builder(CommonComponents.GUI_BACK, button -> this.onBack()).build());
|
| 87 | this.backButton.visible = false;
|
| 88 | this.cancelButton = this.layout.addToFooter(Button.builder(CommonComponents.GUI_CANCEL, button -> this.onCancel()).build());
|
| 89 | if (!this.uploadStarted) {
|
| 90 | if (this.lastScreen.slot == -1) {
|
| 91 | this.uploadStarted = true;
|
| 92 | this.upload();
|
| 93 | } else {
|
| 94 | List<LongRunningTask> tasks = new ArrayList<>();
|
| 95 | if (this.realmCreationTask != null) {
|
| 96 | tasks.add(this.realmCreationTask);
|
| 97 | }
|
| 98 |
|
| 99 | tasks.add(new SwitchSlotTask(this.realmId, this.lastScreen.slot, () -> {
|
| 100 | if (!this.uploadStarted) {
|
| 101 | this.uploadStarted = true;
|
| 102 | this.minecraft.execute(() -> {
|
| 103 | this.minecraft.gui.setScreen(this);
|
| 104 | this.upload();
|
| 105 | });
|
| 106 | }
|
| 107 | }));
|
| 108 | this.minecraft.gui.setScreen(new RealmsLongRunningMcoTaskScreen(this.lastScreen, tasks.toArray(new LongRunningTask[0])));
|
| 109 | }
|
| 110 | }
|
| 111 |
|
| 112 | this.layout.visitWidgets(x$0 -> this.addRenderableWidget(x$0));
|
| 113 | this.repositionElements();
|
| 114 | }
|
| 115 |
|
| 116 | @Override
|
| 117 | protected void repositionElements() {
|
| 118 | this.layout.arrangeElements();
|
| 119 | }
|
| 120 |
|
| 121 | private void onBack() {
|
| 122 | this.minecraft.gui.setScreen(new RealmsConfigureWorldScreen(new RealmsMainScreen(new TitleScreen()), this.realmId));
|
| 123 | }
|
| 124 |
|
| 125 | private void onCancel() {
|
| 126 | this.cancelled = true;
|
| 127 | RealmsWorldUpload realmsWorldUpload = this.currentUpload.get();
|
| 128 | if (realmsWorldUpload != null) {
|
| 129 | realmsWorldUpload.cancel();
|
| 130 | } else {
|
| 131 | this.minecraft.gui.setScreen(this.lastScreen);
|
| 132 | }
|
| 133 | }
|
| 134 |
|
| 135 | @Override
|
| 136 | public boolean keyPressed(final KeyEvent event) {
|
| 137 | if (event.isEscape()) {
|
| 138 | if (this.showDots) {
|
| 139 | this.onCancel();
|
| 140 | } else {
|
| 141 | this.onBack();
|
| 142 | }
|
| 143 |
|
| 144 | return true;
|
| 145 | } else {
|
| 146 | return super.keyPressed(event);
|
| 147 | }
|
| 148 | }
|
| 149 |
|
| 150 | @Override
|
| 151 | public void extractRenderState(final GuiGraphicsExtractor graphics, final int xm, final int ym, final float a) {
|
| 152 | super.extractRenderState(graphics, xm, ym, a);
|
| 153 | if (!this.uploadFinished && this.uploadStatus.uploadStarted() && this.uploadStatus.uploadCompleted() && this.cancelButton != null) {
|
| 154 | this.status = VERIFYING_TEXT;
|
| 155 | this.cancelButton.active = false;
|
| 156 | }
|
| 157 |
|
| 158 | graphics.centeredText(this.font, this.status, this.width / 2, 50, -1);
|
| 159 | if (this.showDots) {
|
| 160 | graphics.text(this.font, DOTS[this.tickCount / 10 % DOTS.length], this.width / 2 + this.font.width(this.status) / 2 + 5, 50, -1);
|
| 161 | }
|
| 162 |
|
| 163 | if (this.uploadStatus.uploadStarted() && !this.cancelled) {
|
| 164 | this.extractProgressBar(graphics);
|
| 165 | this.extractUploadSpeed(graphics);
|
| 166 | }
|
| 167 |
|
| 168 | Component[] errorMessages = this.errorMessage;
|
| 169 | if (errorMessages != null) {
|
| 170 | for (int i = 0; i < errorMessages.length; i++) {
|
| 171 | graphics.centeredText(this.font, errorMessages[i], this.width / 2, 110 + 12 * i, -65536);
|
| 172 | }
|
| 173 | }
|
| 174 | }
|
| 175 |
|
| 176 | private void extractProgressBar(final GuiGraphicsExtractor graphics) {
|
| 177 | double percentage = this.uploadStatus.getPercentage();
|
| 178 | this.progress = String.format(Locale.ROOT, "%.1f", percentage * 100.0);
|
| 179 | int left = (this.width - 200) / 2;
|
| 180 | int right = left + (int)Math.round(200.0 * percentage);
|
| 181 | graphics.fill(left - 1, 79, right + 1, 96, -1);
|
| 182 | graphics.fill(left, 80, right, 95, -8355712);
|
| 183 | graphics.centeredText(this.font, Component.translatable("mco.upload.percent", this.progress), this.width / 2, 84, -1);
|
| 184 | }
|
| 185 |
|
| 186 | private void extractUploadSpeed(final GuiGraphicsExtractor graphics) {
|
| 187 | this.extractUploadSpeed0(graphics, this.uploadStatus.getBytesPerSecond());
|
| 188 | }
|
| 189 |
|
| 190 | private void extractUploadSpeed0(final GuiGraphicsExtractor graphics, final long bytesPerSecond) {
|
| 191 | String uploadProgress = this.progress;
|
| 192 | if (bytesPerSecond > 0L && uploadProgress != null) {
|
| 193 | int progressLength = this.font.width(uploadProgress);
|
| 194 | String stringPresentation = "(" + Unit.humanReadable(bytesPerSecond) + "/s)";
|
| 195 | graphics.text(this.font, stringPresentation, this.width / 2 + progressLength / 2 + 15, 84, -1);
|
| 196 | }
|
| 197 | }
|
| 198 |
|
| 199 | @Override
|
| 200 | public void tick() {
|
| 201 | super.tick();
|
| 202 | this.tickCount++;
|
| 203 | this.uploadStatus.refreshBytesPerSecond();
|
| 204 | if (this.narrationRateLimiter.tryAcquire(1)) {
|
| 205 | Component message = this.createProgressNarrationMessage();
|
| 206 | this.minecraft.getNarrator().saySystemNow(message);
|
| 207 | }
|
| 208 | }
|
| 209 |
|
| 210 | private Component createProgressNarrationMessage() {
|
| 211 | List<Component> elements = Lists.newArrayList();
|
| 212 | elements.add(this.status);
|
| 213 | if (this.progress != null) {
|
| 214 | elements.add(Component.translatable("mco.upload.percent", this.progress));
|
| 215 | }
|
| 216 |
|
| 217 | Component[] errorMessages = this.errorMessage;
|
| 218 | if (errorMessages != null) {
|
| 219 | elements.addAll(Arrays.asList(errorMessages));
|
| 220 | }
|
| 221 |
|
| 222 | return CommonComponents.joinLines(elements);
|
| 223 | }
|
| 224 |
|
| 225 | private void upload() {
|
| 226 | Path worldFolder = this.minecraft.gameDirectory.toPath().resolve("saves").resolve(this.selectedLevel.getLevelId());
|
| 227 | RealmsWorldOptions worldOptions = RealmsWorldOptions.createFromSettings(
|
| 228 | this.selectedLevel.getSettings(), this.selectedLevel.levelVersion().minecraftVersionName()
|
| 229 | );
|
| 230 | RealmsSlot realmsSlot = new RealmsSlot(this.slotId, worldOptions, List.of(RealmsSetting.hardcoreSetting(this.selectedLevel.isHardcore())));
|
| 231 | RealmsWorldUpload newUpload = new RealmsWorldUpload(worldFolder, realmsSlot, this.minecraft.getUser(), this.realmId, this);
|
| 232 | if (!this.currentUpload.compareAndSet(null, newUpload)) {
|
| 233 | throw new IllegalStateException("Tried to start uploading but was already uploading");
|
| 234 | }
|
| 235 |
|
| 236 | newUpload.packAndUpload().handleAsync((result, exception) -> {
|
| 237 | if (exception != null) {
|
| 238 | if (exception instanceof CompletionException e) {
|
| 239 | exception = e.getCause();
|
| 240 | }
|
| 241 |
|
| 242 | if (exception instanceof RealmsUploadException e) {
|
| 243 | if (e.getStatusMessage() != null) {
|
| 244 | this.status = e.getStatusMessage();
|
| 245 | }
|
| 246 |
|
| 247 | this.setErrorMessage(e.getErrorMessages());
|
| 248 | } else {
|
| 249 | this.status = Component.translatable("mco.upload.failed", exception.getMessage());
|
| 250 | }
|
| 251 | } else {
|
| 252 | this.status = Component.translatable("mco.upload.done");
|
| 253 | if (this.backButton != null) {
|
| 254 | this.backButton.setMessage(CommonComponents.GUI_DONE);
|
| 255 | }
|
| 256 | }
|
| 257 |
|
| 258 | this.uploadFinished = true;
|
| 259 | this.showDots = false;
|
| 260 | if (this.backButton != null) {
|
| 261 | this.backButton.visible = true;
|
| 262 | }
|
| 263 |
|
| 264 | if (this.cancelButton != null) {
|
| 265 | this.cancelButton.visible = false;
|
| 266 | }
|
| 267 |
|
| 268 | this.currentUpload.set(null);
|
| 269 | return null;
|
| 270 | }, this.minecraft);
|
| 271 | }
|
| 272 |
|
| 273 | private void setErrorMessage(final Component @Nullable ... messages) {
|
| 274 | this.errorMessage = messages;
|
| 275 | }
|
| 276 |
|
| 277 | @Override
|
| 278 | public UploadStatus getUploadStatus() {
|
| 279 | return this.uploadStatus;
|
| 280 | }
|
| 281 |
|
| 282 | @Override
|
| 283 | public void setUploading() {
|
| 284 | this.status = Component.translatable("mco.upload.uploading", this.selectedLevel.getLevelName());
|
| 285 | }
|
| 286 | }
|
| 287 |
|