From 11bb13ea7d8cdf0802b287cf0830679687227ae1 Mon Sep 17 00:00:00 2001 From: Mehrad Nayyeri Date: Sun, 23 Apr 2023 10:44:41 +0330 Subject: [PATCH] Fixed some bugs during backup creation --- .../rollback/mixin/GameRendererAccessor.java | 2 +- .../rollback/mixin/WorldListEntryMixin.java | 2 +- .../mixin/WorldSelectionListAccessor.java | 2 +- .../rollback/util/backup/BackupManager.java | 45 ++++++++++--------- 4 files changed, 26 insertions(+), 25 deletions(-) diff --git a/src/main/java/ir/mehradn/rollback/mixin/GameRendererAccessor.java b/src/main/java/ir/mehradn/rollback/mixin/GameRendererAccessor.java index 87a1561..436f3f5 100644 --- a/src/main/java/ir/mehradn/rollback/mixin/GameRendererAccessor.java +++ b/src/main/java/ir/mehradn/rollback/mixin/GameRendererAccessor.java @@ -12,5 +12,5 @@ @Mixin(GameRenderer.class) public interface GameRendererAccessor { @Invoker("takeAutoScreenshot") - void InvokeTakeAutoScreenshot(Path path); + void invokeTakeAutoScreenshot(Path path); } diff --git a/src/main/java/ir/mehradn/rollback/mixin/WorldListEntryMixin.java b/src/main/java/ir/mehradn/rollback/mixin/WorldListEntryMixin.java index 83f83ce..35e9094 100644 --- a/src/main/java/ir/mehradn/rollback/mixin/WorldListEntryMixin.java +++ b/src/main/java/ir/mehradn/rollback/mixin/WorldListEntryMixin.java @@ -48,7 +48,7 @@ public void rollbackWorld() { switch (action) { case NOTHING -> this.minecraft.setScreen(this.screen); case RELOAD_WORLD_LIST -> { - ((WorldSelectionListAccessor)this.field_19135).InvokeReloadWorldList(); + ((WorldSelectionListAccessor)this.field_19135).invokeReloadWorldList(); this.minecraft.setScreen(this.screen); } case JOIN_WORLD -> this.joinWorld(); diff --git a/src/main/java/ir/mehradn/rollback/mixin/WorldSelectionListAccessor.java b/src/main/java/ir/mehradn/rollback/mixin/WorldSelectionListAccessor.java index dfddb78..2fbbe29 100644 --- a/src/main/java/ir/mehradn/rollback/mixin/WorldSelectionListAccessor.java +++ b/src/main/java/ir/mehradn/rollback/mixin/WorldSelectionListAccessor.java @@ -10,5 +10,5 @@ @Mixin(WorldSelectionList.class) public interface WorldSelectionListAccessor { @Invoker("reloadWorldList") - void InvokeReloadWorldList(); + void invokeReloadWorldList(); } diff --git a/src/main/java/ir/mehradn/rollback/util/backup/BackupManager.java b/src/main/java/ir/mehradn/rollback/util/backup/BackupManager.java index ab44214..f2119e8 100644 --- a/src/main/java/ir/mehradn/rollback/util/backup/BackupManager.java +++ b/src/main/java/ir/mehradn/rollback/util/backup/BackupManager.java @@ -18,11 +18,11 @@ import net.minecraft.FileUtil; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.components.toasts.SystemToast; -import net.minecraft.client.multiplayer.ClientLevel; import net.minecraft.client.renderer.GameRenderer; import net.minecraft.network.chat.Component; import net.minecraft.server.MinecraftServer; import net.minecraft.util.Mth; +import net.minecraft.world.level.Level; import net.minecraft.world.level.storage.LevelStorageSource; import net.minecraft.world.level.storage.LevelSummary; @@ -81,6 +81,7 @@ public boolean createRollbackBackup(MinecraftServer server, String name) { LevelStorageSource.LevelStorageAccess levelAccess = ((MinecraftServerExpanded)server).getLevelAccess(); String worldName = levelAccess.getLevelId(); RollbackWorld world = getWorld(worldName); + int id = world.lastID + 1; while (world.backups.size() >= RollbackConfig.maxBackupsPerWorld()) deleteBackup(worldName, 0); @@ -107,42 +108,42 @@ public boolean createRollbackBackup(MinecraftServer server, String name) { try { path2 = this.rollbackDirectory.resolve(FileUtil.findAvailableName( this.rollbackDirectory, - worldName + "_" + (world.lastID + 1), + worldName + "_" + id, ".zip" )); Files.move(path1, path2); } catch (IOException e) { showError("rollback.createBackup.failed", "Failed to move the backup file!", e); + Rollback.LOGGER.info("Deleting the backup file..."); + try { + Files.deleteIfExists(path1); + } catch (IOException ignored) { } return false; } - Rollback.LOGGER.debug("Creating an icon..."); - Path path3; - try { - path3 = this.iconsDirectory.resolve(FileUtil.findAvailableName(this.iconsDirectory, - worldName + "_" + (world.lastID + 1), ".png")); - Path finalPath = path3; - Minecraft minecraft = Minecraft.getInstance(); - minecraft.execute(() -> { - GameRenderer renderer = minecraft.gameRenderer; - ((GameRendererAccessor)renderer).InvokeTakeAutoScreenshot(finalPath); - }); - } catch (IOException e) { - showError("rollback.createBackup.failed", "Failed to make an icon for the backup!", e); - return false; - } - - ClientLevel level = Minecraft.getInstance().level; + Level level = server.overworld(); int daysPlayed = (level == null ? -1 : (int)level.getDayTime() / 24000); Rollback.LOGGER.debug("Adding the metadata..."); path2 = this.rollbackDirectory.relativize(path2); - path3 = this.rollbackDirectory.relativize(path3); - RollbackBackup backup = new RollbackBackup(path2, path3, LocalDateTime.now(), daysPlayed, name); + RollbackBackup backup = new RollbackBackup(path2, null, LocalDateTime.now(), daysPlayed, name); world.backups.add(backup); world.lastID++; - saveMetadata(); + + Minecraft minecraft = Minecraft.getInstance(); + minecraft.execute(() -> { + Rollback.LOGGER.debug("Creating an icon..."); + try { + Path path = this.iconsDirectory.resolve(FileUtil.findAvailableName(this.iconsDirectory, + worldName + "_" + id, ".png")); + GameRenderer renderer = minecraft.gameRenderer; + ((GameRendererAccessor)renderer).invokeTakeAutoScreenshot(path); + backup.iconPath = this.rollbackDirectory.relativize(path); + saveMetadata(); + } catch (IOException ignored) { } + }); + return true; }