From 725d4098be27c78ba536abc716081bc30c966319 Mon Sep 17 00:00:00 2001 From: szymon Date: Sat, 19 Jun 2021 14:44:06 +0200 Subject: [PATCH] Replaced File with Optional in Statics to make sure I won't forget to check if it exists --- src/main/java/net/szum123321/textile_backup/Statics.java | 3 ++- .../textile_backup/commands/manage/DeleteCommand.java | 2 +- .../textile_backup/commands/restore/KillRestoreCommand.java | 4 +++- .../szum123321/textile_backup/core/create/BackupHelper.java | 2 +- .../szum123321/textile_backup/core/restore/RestoreHelper.java | 2 +- 5 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/main/java/net/szum123321/textile_backup/Statics.java b/src/main/java/net/szum123321/textile_backup/Statics.java index c56212c..1cfd42a 100644 --- a/src/main/java/net/szum123321/textile_backup/Statics.java +++ b/src/main/java/net/szum123321/textile_backup/Statics.java @@ -24,6 +24,7 @@ import net.szum123321.textile_backup.core.restore.AwaitThread; import java.io.File; import java.time.format.DateTimeFormatter; +import java.util.Optional; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.atomic.AtomicBoolean; @@ -42,7 +43,7 @@ public class Statics { public static final AtomicBoolean globalShutdownBackupFlag = new AtomicBoolean(true); public static boolean disableWatchdog = false; public static AwaitThread restoreAwaitThread = null; - public static File untouchableFile; + public static Optional untouchableFile = Optional.empty(); public static boolean tmpAvailable; } diff --git a/src/main/java/net/szum123321/textile_backup/commands/manage/DeleteCommand.java b/src/main/java/net/szum123321/textile_backup/commands/manage/DeleteCommand.java index 48ce7ac..d98db4c 100644 --- a/src/main/java/net/szum123321/textile_backup/commands/manage/DeleteCommand.java +++ b/src/main/java/net/szum123321/textile_backup/commands/manage/DeleteCommand.java @@ -61,7 +61,7 @@ public class DeleteCommand { .findFirst(); 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()) { Statics.LOGGER.sendInfo(source, "File {} successfully deleted!", optionalFile.get().getName()); diff --git a/src/main/java/net/szum123321/textile_backup/commands/restore/KillRestoreCommand.java b/src/main/java/net/szum123321/textile_backup/commands/restore/KillRestoreCommand.java index 852560e..c6a474d 100644 --- a/src/main/java/net/szum123321/textile_backup/commands/restore/KillRestoreCommand.java +++ b/src/main/java/net/szum123321/textile_backup/commands/restore/KillRestoreCommand.java @@ -24,6 +24,8 @@ import net.minecraft.server.command.CommandManager; import net.minecraft.server.command.ServerCommandSource; import net.szum123321.textile_backup.Statics; +import java.util.Optional; + public class KillRestoreCommand { public static LiteralArgumentBuilder register() { return CommandManager.literal("killR") @@ -31,7 +33,7 @@ public class KillRestoreCommand { if(Statics.restoreAwaitThread != null && Statics.restoreAwaitThread.isAlive()) { Statics.restoreAwaitThread.interrupt(); Statics.globalShutdownBackupFlag.set(true); - Statics.untouchableFile = null; + Statics.untouchableFile = Optional.empty(); Statics.LOGGER.info("{} cancelled backup restoration.", ctx.getSource().getEntity() instanceof PlayerEntity ? "Player: " + ctx.getSource().getName() : diff --git a/src/main/java/net/szum123321/textile_backup/core/create/BackupHelper.java b/src/main/java/net/szum123321/textile_backup/core/create/BackupHelper.java index deb8157..f722ad6 100644 --- a/src/main/java/net/szum123321/textile_backup/core/create/BackupHelper.java +++ b/src/main/java/net/szum123321/textile_backup/core/create/BackupHelper.java @@ -178,7 +178,7 @@ public class BackupHelper { } 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()) { Statics.LOGGER.sendInfoAL(ctx, "Deleting: {}", f.getName()); return true; diff --git a/src/main/java/net/szum123321/textile_backup/core/restore/RestoreHelper.java b/src/main/java/net/szum123321/textile_backup/core/restore/RestoreHelper.java index 7ceec62..44b7c42 100644 --- a/src/main/java/net/szum123321/textile_backup/core/restore/RestoreHelper.java +++ b/src/main/java/net/szum123321/textile_backup/core/restore/RestoreHelper.java @@ -45,7 +45,7 @@ public class RestoreHelper { .filter(rf -> rf.getCreationTime().equals(backupTime)) .findFirst(); - optionalFile.ifPresent(file -> Statics.untouchableFile = file.getFile()); + Statics.untouchableFile = optionalFile.map(RestoreableFile::getFile); return optionalFile; }