Skip to content

Commit

Permalink
Fixed some bugs during backup creation
Browse files Browse the repository at this point in the history
  • Loading branch information
MehradN committed Apr 23, 2023
1 parent 4cca3b7 commit 11bb13e
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@
@Mixin(GameRenderer.class)
public interface GameRendererAccessor {
@Invoker("takeAutoScreenshot")
void InvokeTakeAutoScreenshot(Path path);
void invokeTakeAutoScreenshot(Path path);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
@Mixin(WorldSelectionList.class)
public interface WorldSelectionListAccessor {
@Invoker("reloadWorldList")
void InvokeReloadWorldList();
void invokeReloadWorldList();
}
45 changes: 23 additions & 22 deletions src/main/java/ir/mehradn/rollback/util/backup/BackupManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand All @@ -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;
}

Expand Down

0 comments on commit 11bb13e

Please sign in to comment.