Replaced File with Optional<File> in Statics to make sure I won't forget to check if it exists

2.x-1.16
szymon 2021-06-19 14:44:06 +02:00
parent af8e14f092
commit 725d4098be
5 changed files with 8 additions and 5 deletions

View File

@ -24,6 +24,7 @@ import net.szum123321.textile_backup.core.restore.AwaitThread;
import java.io.File; import java.io.File;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.Optional;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
@ -42,7 +43,7 @@ public class Statics {
public static final AtomicBoolean globalShutdownBackupFlag = new AtomicBoolean(true); public static final AtomicBoolean globalShutdownBackupFlag = new AtomicBoolean(true);
public static boolean disableWatchdog = false; public static boolean disableWatchdog = false;
public static AwaitThread restoreAwaitThread = null; public static AwaitThread restoreAwaitThread = null;
public static File untouchableFile; public static Optional<File> untouchableFile = Optional.empty();
public static boolean tmpAvailable; public static boolean tmpAvailable;
} }

View File

@ -61,7 +61,7 @@ public class DeleteCommand {
.findFirst(); .findFirst();
if(optionalFile.isPresent()) { if(optionalFile.isPresent()) {
if(Statics.untouchableFile == null || (Statics.untouchableFile != null && !Statics.untouchableFile.equals(optionalFile.get()))) { if(Statics.untouchableFile.isEmpty() || !Statics.untouchableFile.get().equals(optionalFile.get())) {
if(optionalFile.get().delete()) { if(optionalFile.get().delete()) {
Statics.LOGGER.sendInfo(source, "File {} successfully deleted!", optionalFile.get().getName()); Statics.LOGGER.sendInfo(source, "File {} successfully deleted!", optionalFile.get().getName());

View File

@ -24,6 +24,8 @@ import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource; import net.minecraft.server.command.ServerCommandSource;
import net.szum123321.textile_backup.Statics; import net.szum123321.textile_backup.Statics;
import java.util.Optional;
public class KillRestoreCommand { public class KillRestoreCommand {
public static LiteralArgumentBuilder<ServerCommandSource> register() { public static LiteralArgumentBuilder<ServerCommandSource> register() {
return CommandManager.literal("killR") return CommandManager.literal("killR")
@ -31,7 +33,7 @@ public class KillRestoreCommand {
if(Statics.restoreAwaitThread != null && Statics.restoreAwaitThread.isAlive()) { if(Statics.restoreAwaitThread != null && Statics.restoreAwaitThread.isAlive()) {
Statics.restoreAwaitThread.interrupt(); Statics.restoreAwaitThread.interrupt();
Statics.globalShutdownBackupFlag.set(true); Statics.globalShutdownBackupFlag.set(true);
Statics.untouchableFile = null; Statics.untouchableFile = Optional.empty();
Statics.LOGGER.info("{} cancelled backup restoration.", ctx.getSource().getEntity() instanceof PlayerEntity ? Statics.LOGGER.info("{} cancelled backup restoration.", ctx.getSource().getEntity() instanceof PlayerEntity ?
"Player: " + ctx.getSource().getName() : "Player: " + ctx.getSource().getName() :

View File

@ -178,7 +178,7 @@ public class BackupHelper {
} }
private static boolean deleteFile(File f, ServerCommandSource ctx) { private static boolean deleteFile(File f, ServerCommandSource ctx) {
if(!Statics.untouchableFile.equals(f)) { if(Statics.untouchableFile.isEmpty()|| !Statics.untouchableFile.get().equals(f)) {
if(f.delete()) { if(f.delete()) {
Statics.LOGGER.sendInfoAL(ctx, "Deleting: {}", f.getName()); Statics.LOGGER.sendInfoAL(ctx, "Deleting: {}", f.getName());
return true; return true;

View File

@ -45,7 +45,7 @@ public class RestoreHelper {
.filter(rf -> rf.getCreationTime().equals(backupTime)) .filter(rf -> rf.getCreationTime().equals(backupTime))
.findFirst(); .findFirst();
optionalFile.ifPresent(file -> Statics.untouchableFile = file.getFile()); Statics.untouchableFile = optionalFile.map(RestoreableFile::getFile);
return optionalFile; return optionalFile;
} }