| 1 | package com.mojang.realmsclient.util.task;
|
| 2 |
|
| 3 | import com.mojang.logging.LogUtils;
|
| 4 | import com.mojang.realmsclient.client.RealmsClient;
|
| 5 | import com.mojang.realmsclient.dto.WorldDownload;
|
| 6 | import com.mojang.realmsclient.exception.RealmsServiceException;
|
| 7 | import com.mojang.realmsclient.exception.RetryCallException;
|
| 8 | import com.mojang.realmsclient.gui.screens.RealmsDownloadLatestWorldScreen;
|
| 9 | import com.mojang.realmsclient.gui.screens.RealmsGenericErrorScreen;
|
| 10 | import net.fabricmc.api.EnvType;
|
| 11 | import net.fabricmc.api.Environment;
|
| 12 | import net.minecraft.client.gui.screens.Screen;
|
| 13 | import net.minecraft.network.chat.Component;
|
| 14 | import org.slf4j.Logger;
|
| 15 |
|
| 16 | @Environment(EnvType.CLIENT)
|
| 17 | public class DownloadTask extends LongRunningTask {
|
| 18 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 19 | private static final Component TITLE = Component.translatable("mco.download.preparing");
|
| 20 | private final long realmId;
|
| 21 | private final int slot;
|
| 22 | private final Screen lastScreen;
|
| 23 | private final String downloadName;
|
| 24 |
|
| 25 | public DownloadTask(final long realmId, final int slot, final String downloadName, final Screen lastScreen) {
|
| 26 | this.realmId = realmId;
|
| 27 | this.slot = slot;
|
| 28 | this.lastScreen = lastScreen;
|
| 29 | this.downloadName = downloadName;
|
| 30 | }
|
| 31 |
|
| 32 | @Override
|
| 33 | public void run() {
|
| 34 | RealmsClient client = RealmsClient.getOrCreate();
|
| 35 | int i = 0;
|
| 36 |
|
| 37 | while (i < 25) {
|
| 38 | try {
|
| 39 | if (this.aborted()) {
|
| 40 | return;
|
| 41 | }
|
| 42 |
|
| 43 | WorldDownload worldDownload = client.requestDownloadInfo(this.realmId, this.slot);
|
| 44 | pause(1L);
|
| 45 | if (this.aborted()) {
|
| 46 | return;
|
| 47 | }
|
| 48 |
|
| 49 | setScreen(new RealmsDownloadLatestWorldScreen(this.lastScreen, worldDownload, this.downloadName, result -> {}));
|
| 50 | return;
|
| 51 | } catch (RetryCallException e) {
|
| 52 | if (this.aborted()) {
|
| 53 | return;
|
| 54 | }
|
| 55 |
|
| 56 | pause(e.delaySeconds);
|
| 57 | i++;
|
| 58 | } catch (RealmsServiceException e) {
|
| 59 | if (this.aborted()) {
|
| 60 | return;
|
| 61 | }
|
| 62 |
|
| 63 | LOGGER.error("Couldn't download world data", e);
|
| 64 | setScreen(new RealmsGenericErrorScreen(e, this.lastScreen));
|
| 65 | return;
|
| 66 | } catch (Exception e) {
|
| 67 | if (this.aborted()) {
|
| 68 | return;
|
| 69 | }
|
| 70 |
|
| 71 | LOGGER.error("Couldn't download world data", e);
|
| 72 | this.error(e);
|
| 73 | return;
|
| 74 | }
|
| 75 | }
|
| 76 | }
|
| 77 |
|
| 78 | @Override
|
| 79 | public Component getTitle() {
|
| 80 | return TITLE;
|
| 81 | }
|
| 82 | }
|
| 83 |
|