Spaces:
Build error
Build error
File size: 11,643 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 |
package com.mojang.realmsclient.gui.screens;
import com.google.common.collect.Lists;
import com.google.common.util.concurrent.RateLimiter;
import com.mojang.realmsclient.RealmsMainScreen;
import com.mojang.realmsclient.Unit;
import com.mojang.realmsclient.client.UploadStatus;
import com.mojang.realmsclient.client.worldupload.RealmsUploadException;
import com.mojang.realmsclient.client.worldupload.RealmsWorldUpload;
import com.mojang.realmsclient.client.worldupload.RealmsWorldUploadStatusTracker;
import com.mojang.realmsclient.dto.RealmsWorldOptions;
import com.mojang.realmsclient.util.task.LongRunningTask;
import com.mojang.realmsclient.util.task.RealmCreationTask;
import com.mojang.realmsclient.util.task.SwitchSlotTask;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.CompletionException;
import java.util.concurrent.atomic.AtomicReference;
import javax.annotation.Nullable;
import net.minecraft.client.GameNarrator;
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.events.GuiEventListener;
import net.minecraft.client.gui.layouts.HeaderAndFooterLayout;
import net.minecraft.client.gui.screens.TitleScreen;
import net.minecraft.network.chat.CommonComponents;
import net.minecraft.network.chat.Component;
import net.minecraft.realms.RealmsScreen;
import net.minecraft.world.level.storage.LevelSummary;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
@OnlyIn(Dist.CLIENT)
public class RealmsUploadScreen extends RealmsScreen implements RealmsWorldUploadStatusTracker {
private static final int BAR_WIDTH = 200;
private static final int BAR_TOP = 80;
private static final int BAR_BOTTOM = 95;
private static final int BAR_BORDER = 1;
private static final String[] DOTS = new String[]{"", ".", ". .", ". . ."};
private static final Component VERIFYING_TEXT = Component.translatable("mco.upload.verifying");
private final RealmsResetWorldScreen lastScreen;
private final LevelSummary selectedLevel;
@Nullable
private final RealmCreationTask realmCreationTask;
private final long realmId;
private final int slotId;
final AtomicReference<RealmsWorldUpload> currentUpload = new AtomicReference<>();
private final UploadStatus uploadStatus;
private final RateLimiter narrationRateLimiter;
@Nullable
private volatile Component[] errorMessage;
private volatile Component status = Component.translatable("mco.upload.preparing");
@Nullable
private volatile String progress;
private volatile boolean cancelled;
private volatile boolean uploadFinished;
private volatile boolean showDots = true;
private volatile boolean uploadStarted;
@Nullable
private Button backButton;
@Nullable
private Button cancelButton;
private int tickCount;
private final HeaderAndFooterLayout layout = new HeaderAndFooterLayout(this);
public RealmsUploadScreen(@Nullable RealmCreationTask p_332847_, long p_90083_, int p_90084_, RealmsResetWorldScreen p_90085_, LevelSummary p_90086_) {
super(GameNarrator.NO_TITLE);
this.realmCreationTask = p_332847_;
this.realmId = p_90083_;
this.slotId = p_90084_;
this.lastScreen = p_90085_;
this.selectedLevel = p_90086_;
this.uploadStatus = new UploadStatus();
this.narrationRateLimiter = RateLimiter.create(0.1F);
}
@Override
public void init() {
this.backButton = this.layout.addToFooter(Button.builder(CommonComponents.GUI_BACK, p_90118_ -> this.onBack()).build());
this.backButton.visible = false;
this.cancelButton = this.layout.addToFooter(Button.builder(CommonComponents.GUI_CANCEL, p_90104_ -> this.onCancel()).build());
if (!this.uploadStarted) {
if (this.lastScreen.slot == -1) {
this.uploadStarted = true;
this.upload();
} else {
List<LongRunningTask> list = new ArrayList<>();
if (this.realmCreationTask != null) {
list.add(this.realmCreationTask);
}
list.add(new SwitchSlotTask(this.realmId, this.lastScreen.slot, () -> {
if (!this.uploadStarted) {
this.uploadStarted = true;
this.minecraft.execute(() -> {
this.minecraft.setScreen(this);
this.upload();
});
}
}));
this.minecraft.setScreen(new RealmsLongRunningMcoTaskScreen(this.lastScreen, list.toArray(new LongRunningTask[0])));
}
}
this.layout.visitWidgets(p_325163_ -> {
AbstractWidget abstractwidget = this.addRenderableWidget(p_325163_);
});
this.repositionElements();
}
@Override
protected void repositionElements() {
this.layout.arrangeElements();
}
private void onBack() {
this.minecraft.setScreen(new RealmsConfigureWorldScreen(new RealmsMainScreen(new TitleScreen()), this.realmId));
}
private void onCancel() {
this.cancelled = true;
RealmsWorldUpload realmsworldupload = this.currentUpload.get();
if (realmsworldupload != null) {
realmsworldupload.cancel();
} else {
this.minecraft.setScreen(this.lastScreen);
}
}
@Override
public boolean keyPressed(int p_90089_, int p_90090_, int p_90091_) {
if (p_90089_ == 256) {
if (this.showDots) {
this.onCancel();
} else {
this.onBack();
}
return true;
} else {
return super.keyPressed(p_90089_, p_90090_, p_90091_);
}
}
@Override
public void render(GuiGraphics p_282140_, int p_90097_, int p_90098_, float p_90099_) {
super.render(p_282140_, p_90097_, p_90098_, p_90099_);
if (!this.uploadFinished && this.uploadStatus.uploadStarted() && this.uploadStatus.uploadCompleted() && this.cancelButton != null) {
this.status = VERIFYING_TEXT;
this.cancelButton.active = false;
}
p_282140_.drawCenteredString(this.font, this.status, this.width / 2, 50, -1);
if (this.showDots) {
p_282140_.drawString(
this.font, DOTS[this.tickCount / 10 % DOTS.length], this.width / 2 + this.font.width(this.status) / 2 + 5, 50, -1
);
}
if (this.uploadStatus.uploadStarted() && !this.cancelled) {
this.drawProgressBar(p_282140_);
this.drawUploadSpeed(p_282140_);
}
Component[] acomponent = this.errorMessage;
if (acomponent != null) {
for (int i = 0; i < acomponent.length; i++) {
p_282140_.drawCenteredString(this.font, acomponent[i], this.width / 2, 110 + 12 * i, -65536);
}
}
}
private void drawProgressBar(GuiGraphics p_282575_) {
double d0 = this.uploadStatus.getPercentage();
this.progress = String.format(Locale.ROOT, "%.1f", d0 * 100.0);
int i = (this.width - 200) / 2;
int j = i + (int)Math.round(200.0 * d0);
p_282575_.fill(i - 1, 79, j + 1, 96, -1);
p_282575_.fill(i, 80, j, 95, -8355712);
p_282575_.drawCenteredString(this.font, Component.translatable("mco.upload.percent", this.progress), this.width / 2, 84, -1);
}
private void drawUploadSpeed(GuiGraphics p_281884_) {
this.drawUploadSpeed0(p_281884_, this.uploadStatus.getBytesPerSecond());
}
private void drawUploadSpeed0(GuiGraphics p_282279_, long p_282827_) {
String s = this.progress;
if (p_282827_ > 0L && s != null) {
int i = this.font.width(s);
String s1 = "(" + Unit.humanReadable(p_282827_) + "/s)";
p_282279_.drawString(this.font, s1, this.width / 2 + i / 2 + 15, 84, -1);
}
}
@Override
public void tick() {
super.tick();
this.tickCount++;
this.uploadStatus.refreshBytesPerSecond();
if (this.narrationRateLimiter.tryAcquire(1)) {
Component component = this.createProgressNarrationMessage();
this.minecraft.getNarrator().sayNow(component);
}
}
private Component createProgressNarrationMessage() {
List<Component> list = Lists.newArrayList();
list.add(this.status);
if (this.progress != null) {
list.add(Component.translatable("mco.upload.percent", this.progress));
}
Component[] acomponent = this.errorMessage;
if (acomponent != null) {
list.addAll(Arrays.asList(acomponent));
}
return CommonComponents.joinLines(list);
}
private void upload() {
Path path = this.minecraft.gameDirectory.toPath().resolve("saves").resolve(this.selectedLevel.getLevelId());
RealmsWorldOptions realmsworldoptions = RealmsWorldOptions.createFromSettings(this.selectedLevel.getSettings(), this.selectedLevel.levelVersion().minecraftVersionName());
RealmsWorldUpload realmsworldupload = new RealmsWorldUpload(path, realmsworldoptions, this.minecraft.getUser(), this.realmId, this.slotId, this);
if (!this.currentUpload.compareAndSet(null, realmsworldupload)) {
throw new IllegalStateException("Tried to start uploading but was already uploading");
} else {
realmsworldupload.packAndUpload().handleAsync((p_357567_, p_357568_) -> {
if (p_357568_ != null) {
if (p_357568_ instanceof CompletionException completionexception) {
p_357568_ = completionexception.getCause();
}
if (p_357568_ instanceof RealmsUploadException realmsuploadexception) {
if (realmsuploadexception.getStatusMessage() != null) {
this.status = realmsuploadexception.getStatusMessage();
}
this.setErrorMessage(realmsuploadexception.getErrorMessages());
} else {
this.status = Component.translatable("mco.upload.failed", p_357568_.getMessage());
}
} else {
this.status = Component.translatable("mco.upload.done");
if (this.backButton != null) {
this.backButton.setMessage(CommonComponents.GUI_DONE);
}
}
this.uploadFinished = true;
this.showDots = false;
if (this.backButton != null) {
this.backButton.visible = true;
}
if (this.cancelButton != null) {
this.cancelButton.visible = false;
}
this.currentUpload.set(null);
return null;
}, this.minecraft);
}
}
private void setErrorMessage(@Nullable Component... p_90113_) {
this.errorMessage = p_90113_;
}
@Override
public UploadStatus getUploadStatus() {
return this.uploadStatus;
}
@Override
public void setUploading() {
this.status = Component.translatable("mco.upload.uploading", this.selectedLevel.getLevelName());
}
} |