MakeBackupRunnable now takes BackupContext instead of separate objects

This commit is contained in:
szymon 2020-08-11 09:55:28 +02:00
parent d215edcec9
commit 9ffa3f0005

View File

@ -18,8 +18,6 @@
package net.szum123321.textile_backup.core.create; package net.szum123321.textile_backup.core.create;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.command.ServerCommandSource;
import net.szum123321.textile_backup.Statics; import net.szum123321.textile_backup.Statics;
import net.szum123321.textile_backup.core.create.compressors.*; import net.szum123321.textile_backup.core.create.compressors.*;
import net.szum123321.textile_backup.core.Utilities; import net.szum123321.textile_backup.core.Utilities;
@ -29,26 +27,22 @@ import java.io.IOException;
import java.time.LocalDateTime; import java.time.LocalDateTime;
public class MakeBackupRunnable implements Runnable { public class MakeBackupRunnable implements Runnable {
private final MinecraftServer server; private final BackupContext context;
private final ServerCommandSource commandSource;
private final String comment;
public MakeBackupRunnable(BackupContext context){ public MakeBackupRunnable(BackupContext context){
this.server = context.getServer(); this.context = context;
this.commandSource = context.getCommandSource();
this.comment = context.getComment();
} }
@Override @Override
public void run() { public void run() {
Statics.LOGGER.sendInfo(commandSource, "Starting backup"); Statics.LOGGER.sendInfo(context.getCommandSource(), "Starting backup");
File world = Utilities.getWorldFolder(server); File world = Utilities.getWorldFolder(context.getServer());
Statics.LOGGER.trace("Minecraft world is: {}", world); Statics.LOGGER.trace("Minecraft world is: {}", world);
File outFile = Utilities File outFile = Utilities
.getBackupRootPath(Utilities.getLevelName(server)) .getBackupRootPath(Utilities.getLevelName(context.getServer()))
.toPath() .toPath()
.resolve(getFileName()) .resolve(getFileName())
.toFile(); .toFile();
@ -61,7 +55,7 @@ public class MakeBackupRunnable implements Runnable {
outFile.createNewFile(); outFile.createNewFile();
} catch (IOException e) { } catch (IOException e) {
Statics.LOGGER.error("An exception occurred when trying to create new backup file!", e); Statics.LOGGER.error("An exception occurred when trying to create new backup file!", e);
Statics.LOGGER.sendError(commandSource, "An exception occurred when trying to create new backup file!"); Statics.LOGGER.sendError(context.getCommandSource(), "An exception occurred when trying to create new backup file!");
return; return;
} }
@ -78,37 +72,39 @@ public class MakeBackupRunnable implements Runnable {
switch (Statics.CONFIG.format) { switch (Statics.CONFIG.format) {
case ZIP: case ZIP:
ParallelZipCompressor.createArchive(world, outFile, commandSource, coreCount); ParallelZipCompressor.createArchive(world, outFile, context, coreCount);
break; break;
case BZIP2: case BZIP2:
ParallelBZip2Compressor.getInstance().createArchive(world, outFile, commandSource, coreCount); ParallelBZip2Compressor.getInstance().createArchive(world, outFile, context, coreCount);
break; break;
case GZIP: case GZIP:
ParallelGzipCompressor.getInstance().createArchive(world, outFile, commandSource, coreCount); ParallelGzipCompressor.getInstance().createArchive(world, outFile, context, coreCount);
break; break;
case LZMA: case LZMA:
LZMACompressor.getInstance().createArchive(world, outFile, commandSource, coreCount); LZMACompressor.getInstance().createArchive(world, outFile, context, coreCount);
break; break;
default: default:
Statics.LOGGER.warn("Specified compressor ({}) is not supported! Zip will be used instead!", Statics.CONFIG.format); Statics.LOGGER.warn("Specified compressor ({}) is not supported! Zip will be used instead!", Statics.CONFIG.format);
Statics.LOGGER.sendError(commandSource, "Error! No correct compression format specified! Using default compressor!"); Statics.LOGGER.sendError(context.getCommandSource(), "Error! No correct compression format specified! Using default compressor!");
ParallelZipCompressor.createArchive(world, outFile, commandSource, coreCount); ParallelZipCompressor.createArchive(world, outFile, context, coreCount);
break; break;
} }
BackupHelper.executeFileLimit(commandSource, Utilities.getLevelName(server)); BackupHelper.executeFileLimit(context.getCommandSource(), Utilities.getLevelName(context.getServer()));
Statics.LOGGER.sendInfo(commandSource, "Done!"); Statics.LOGGER.sendInfo(context, "Done!");
} }
private String getFileName(){ private String getFileName(){
LocalDateTime now = LocalDateTime.now(); LocalDateTime now = LocalDateTime.now();
return Utilities.getDateTimeFormatter().format(now) + (comment != null ? "#" + comment.replace("#", "") : "") + Statics.CONFIG.format.getString(); return Utilities.getDateTimeFormatter().format(now) +
(context.getComment() != null ? "#" + context.getComment().replace("#", "") : "") +
Statics.CONFIG.format.getString();
} }
} }