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