summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKeuin <[email protected]>2021-01-13 13:22:19 +0800
committerkeuin <[email protected]>2021-01-13 13:22:19 +0800
commitc82e1ee286e89f6e36e8920282d348ce8cfae9cf (patch)
tree63e9828aacc7cd691655dffc2c2f046d0c4dd807
parent81497ebee2cdb0dac3bcdc26c5d77d06bdb98b5d (diff)
Refactor for a better code quality
-rw-r--r--src/main/java/com/keuin/kbackupfabric/KBCommands.java67
-rw-r--r--src/main/java/com/keuin/kbackupfabric/operation/BackupOperation.java6
-rw-r--r--src/main/java/com/keuin/kbackupfabric/operation/RestoreOperation.java15
-rw-r--r--src/main/java/com/keuin/kbackupfabric/operation/backup/feedback/BackupFeedback.java1
-rw-r--r--src/main/java/com/keuin/kbackupfabric/operation/backup/feedback/IncrementalBackupFeedback.java28
-rw-r--r--src/main/java/com/keuin/kbackupfabric/operation/backup/feedback/PrimitiveBackupFeedback.java3
-rw-r--r--src/main/java/com/keuin/kbackupfabric/operation/backup/method/BackupMethod.java (renamed from src/main/java/com/keuin/kbackupfabric/operation/backup/BackupMethod.java)6
-rw-r--r--src/main/java/com/keuin/kbackupfabric/operation/backup/method/IncrementalBackupMethod.java17
-rw-r--r--src/main/java/com/keuin/kbackupfabric/operation/backup/method/PrimitiveBackupMethod.java (renamed from src/main/java/com/keuin/kbackupfabric/operation/backup/PrimitiveBackupMethod.java)2
-rw-r--r--src/main/java/com/keuin/kbackupfabric/util/backup/name/BackupFileNameEncoder.java9
-rw-r--r--src/main/java/com/keuin/kbackupfabric/util/backup/name/IncrementalBackupFileNameEncoder.java33
-rw-r--r--src/main/java/com/keuin/kbackupfabric/util/backup/name/PrimitiveBackupFileNameEncoder.java1
12 files changed, 149 insertions, 39 deletions
diff --git a/src/main/java/com/keuin/kbackupfabric/KBCommands.java b/src/main/java/com/keuin/kbackupfabric/KBCommands.java
index ef4a401..0e268ce 100644
--- a/src/main/java/com/keuin/kbackupfabric/KBCommands.java
+++ b/src/main/java/com/keuin/kbackupfabric/KBCommands.java
@@ -5,9 +5,10 @@ import com.keuin.kbackupfabric.operation.BackupOperation;
import com.keuin.kbackupfabric.operation.DeleteOperation;
import com.keuin.kbackupfabric.operation.RestoreOperation;
import com.keuin.kbackupfabric.operation.abstracts.i.Invokable;
-import com.keuin.kbackupfabric.operation.backup.PrimitiveBackupMethod;
+import com.keuin.kbackupfabric.operation.backup.method.PrimitiveBackupMethod;
import com.keuin.kbackupfabric.util.PrintUtil;
import com.keuin.kbackupfabric.util.backup.BackupFilesystemUtil;
+import com.keuin.kbackupfabric.util.backup.name.PrimitiveBackupFileNameEncoder;
import com.keuin.kbackupfabric.util.backup.suggestion.BackupNameSuggestionProvider;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.context.CommandContext;
@@ -82,23 +83,24 @@ public final class KBCommands {
(dir, name) -> dir.isDirectory() && name.toLowerCase().endsWith(".zip") && name.toLowerCase().startsWith(getBackupFileNamePrefix())
);
- backupFileNameList.clear();
- if (files != null) {
- if (files.length != 0) {
- msgInfo(context, "Available backups: (file is not checked, manipulation may affect this plugin)");
+ synchronized (backupFileNameList) {
+ backupFileNameList.clear();
+ if (files != null) {
+ if (files.length != 0) {
+ msgInfo(context, "Available backups: (file is not checked, manipulation may affect this plugin)");
+ } else {
+ msgInfo(context, "There are no available backups. To make a new backup, run /kb backup.");
+ }
+ int i = 0;
+ for (File file : files) {
+ ++i;
+ String backupFileName = file.getName();
+ msgInfo(context, String.format("[%d] %s", i, getPrimitiveBackupInformationString(backupFileName, file.length())));
+ backupFileNameList.add(backupFileName);
+ }
} else {
- msgInfo(context, "There are no available backups. To make a new backup, run /kb backup.");
+ msgErr(context, "Error: failed to list files in backup folder.");
}
- int i = 0;
- for (File file : files) {
- ++i;
- String backupFileName = file.getName();
- String sizeString = getFriendlyFileSizeString(file.length());
- msgInfo(context, String.format("[%d] %s, size: %s", i, backupFileName, sizeString));
- backupFileNameList.add(backupFileName);
- }
- } else {
- msgErr(context, "Error: failed to list files in backup folder.");
}
return SUCCESS;
}
@@ -293,14 +295,21 @@ public final class KBCommands {
files.sort((x, y) -> (int) (BackupFilesystemUtil.getBackupTimeFromBackupFileName(y.getName()) - BackupFilesystemUtil.getBackupTimeFromBackupFileName(x.getName())));
File prevBackupFile = files.get(0);
String backupFileName = prevBackupFile.getName();
- int i = backupFileNameList.indexOf(backupFileName);
- if (i == -1) {
- backupFileNameList.add(backupFileName);
- i = backupFileNameList.size();
- } else {
- ++i;
+ int i;
+ synchronized (backupFileNameList) {
+ i = backupFileNameList.indexOf(backupFileName);
+ if (i == -1) {
+ backupFileNameList.add(backupFileName);
+ i = backupFileNameList.size();
+ } else {
+ ++i;
+ }
}
- msgInfo(context, String.format("The most recent backup: [%d] %s , size: %s", i, backupFileName, getFriendlyFileSizeString(prevBackupFile.length())));
+ msgInfo(context, String.format(
+ "The most recent backup: [%d] %s",
+ i,
+ getPrimitiveBackupInformationString(backupFileName, prevBackupFile.length())
+ ));
} catch (NullPointerException e) {
msgInfo(context, "There are no backups available.");
} catch (SecurityException ignored) {
@@ -310,6 +319,14 @@ public final class KBCommands {
return SUCCESS;
}
+ private static String getPrimitiveBackupInformationString(String backupFileName, long backupFileSizeBytes) {
+ return String.format(
+ "%s , size: %s",
+ new PrimitiveBackupFileNameEncoder().decode(backupFileName),
+ getFriendlyFileSizeString(backupFileSizeBytes)
+ );
+ }
+
// /**
// * Select the backup method we use.
// * @param context the context.
@@ -338,7 +355,9 @@ public final class KBCommands {
if (backupName.matches("[0-9]*")) {
// If numeric input
int index = Integer.parseInt(backupName) - 1;
- return backupFileNameList.get(index); // Replace input number with real backup file name.
+ synchronized (backupFileNameList) {
+ return backupFileNameList.get(index); // Replace input number with real backup file name.
+ }
}
} catch (NumberFormatException | IndexOutOfBoundsException ignored) {
}
diff --git a/src/main/java/com/keuin/kbackupfabric/operation/BackupOperation.java b/src/main/java/com/keuin/kbackupfabric/operation/BackupOperation.java
index f8736c8..3183cf0 100644
--- a/src/main/java/com/keuin/kbackupfabric/operation/BackupOperation.java
+++ b/src/main/java/com/keuin/kbackupfabric/operation/BackupOperation.java
@@ -1,8 +1,8 @@
package com.keuin.kbackupfabric.operation;
import com.keuin.kbackupfabric.operation.abstracts.InvokableAsyncBlockingOperation;
-import com.keuin.kbackupfabric.operation.backup.BackupMethod;
-import com.keuin.kbackupfabric.operation.backup.feedback.PrimitiveBackupFeedback;
+import com.keuin.kbackupfabric.operation.backup.feedback.BackupFeedback;
+import com.keuin.kbackupfabric.operation.backup.method.BackupMethod;
import com.keuin.kbackupfabric.util.PrintUtil;
import com.mojang.brigadier.context.CommandContext;
import net.minecraft.server.MinecraftServer;
@@ -52,7 +52,7 @@ public class BackupOperation extends InvokableAsyncBlockingOperation {
String levelPath = getLevelPath(server);
String backupFileName = getBackupFileName(customBackupName);
- PrimitiveBackupFeedback result = backupMethod.backup(customBackupName,levelPath,backupSaveDirectory);
+ BackupFeedback result = backupMethod.backup(customBackupName,levelPath,backupSaveDirectory);
if(result.isSuccess()) {
// Restore old auto-save switch stat
server.getWorlds().forEach(world -> world.savingDisabled = oldWorldsSavingDisabled.getOrDefault(world, true));
diff --git a/src/main/java/com/keuin/kbackupfabric/operation/RestoreOperation.java b/src/main/java/com/keuin/kbackupfabric/operation/RestoreOperation.java
index b95c767..02b76f1 100644
--- a/src/main/java/com/keuin/kbackupfabric/operation/RestoreOperation.java
+++ b/src/main/java/com/keuin/kbackupfabric/operation/RestoreOperation.java
@@ -1,8 +1,8 @@
package com.keuin.kbackupfabric.operation;
import com.keuin.kbackupfabric.operation.abstracts.InvokableBlockingOperation;
-import com.keuin.kbackupfabric.operation.backup.BackupMethod;
-import com.keuin.kbackupfabric.operation.backup.PrimitiveBackupMethod;
+import com.keuin.kbackupfabric.operation.backup.method.BackupMethod;
+import com.keuin.kbackupfabric.operation.backup.method.PrimitiveBackupMethod;
import com.keuin.kbackupfabric.util.PrintUtil;
import com.mojang.brigadier.context.CommandContext;
import net.minecraft.server.MinecraftServer;
@@ -94,10 +94,12 @@ public class RestoreOperation extends InvokableBlockingOperation {
}while(--cnt > 0);
////////////////////
- backupMethod.restore(backupFileName, levelPath, backupSavePath);
-
- //ServerRestartUtil.forkAndRestart();
- System.exit(111);
+ if (backupMethod.restore(backupFileName, levelPath, backupSavePath)) {
+ //ServerRestartUtil.forkAndRestart();
+ System.exit(111);
+ } else {
+ PrintUtil.error("Failed to restore! server will not restart automatically.");
+ }
} catch (SecurityException e) {
PrintUtil.error("An exception occurred while restoring: " + e.getMessage());
@@ -105,6 +107,7 @@ public class RestoreOperation extends InvokableBlockingOperation {
PrintUtil.error(e.toString());
PrintUtil.error("Failed to restore.");
}
+ System.exit(0); // all failed restoration will eventually go here
}
}
}
diff --git a/src/main/java/com/keuin/kbackupfabric/operation/backup/feedback/BackupFeedback.java b/src/main/java/com/keuin/kbackupfabric/operation/backup/feedback/BackupFeedback.java
index 92a9f39..0fe0766 100644
--- a/src/main/java/com/keuin/kbackupfabric/operation/backup/feedback/BackupFeedback.java
+++ b/src/main/java/com/keuin/kbackupfabric/operation/backup/feedback/BackupFeedback.java
@@ -2,4 +2,5 @@ package com.keuin.kbackupfabric.operation.backup.feedback;
public interface BackupFeedback {
String getFeedback();
+ boolean isSuccess();
}
diff --git a/src/main/java/com/keuin/kbackupfabric/operation/backup/feedback/IncrementalBackupFeedback.java b/src/main/java/com/keuin/kbackupfabric/operation/backup/feedback/IncrementalBackupFeedback.java
new file mode 100644
index 0000000..f39fde6
--- /dev/null
+++ b/src/main/java/com/keuin/kbackupfabric/operation/backup/feedback/IncrementalBackupFeedback.java
@@ -0,0 +1,28 @@
+package com.keuin.kbackupfabric.operation.backup.feedback;
+
+public class IncrementalBackupFeedback implements BackupFeedback {
+ private final boolean success;
+ private final int newFilesAdded;
+
+ public IncrementalBackupFeedback(boolean success, int newFilesAdded) {
+ this.success = success;
+ this.newFilesAdded = newFilesAdded;
+ }
+
+ @Override
+ public boolean isSuccess() {
+ return success;
+ }
+
+ public long getNewFilesAdded() {
+ return newFilesAdded;
+ }
+
+ @Override
+ public String getFeedback() {
+ if (success && newFilesAdded >= 0)
+ return String.format("File(s) added: %d.", newFilesAdded);
+ else
+ return "";
+ }
+}
diff --git a/src/main/java/com/keuin/kbackupfabric/operation/backup/feedback/PrimitiveBackupFeedback.java b/src/main/java/com/keuin/kbackupfabric/operation/backup/feedback/PrimitiveBackupFeedback.java
index 6d7a15b..3bcd012 100644
--- a/src/main/java/com/keuin/kbackupfabric/operation/backup/feedback/PrimitiveBackupFeedback.java
+++ b/src/main/java/com/keuin/kbackupfabric/operation/backup/feedback/PrimitiveBackupFeedback.java
@@ -11,6 +11,7 @@ public class PrimitiveBackupFeedback implements BackupFeedback {
this.backupSizeBytes = backupSizeBytes;
}
+ @Override
public boolean isSuccess() {
return success;
}
@@ -22,7 +23,7 @@ public class PrimitiveBackupFeedback implements BackupFeedback {
@Override
public String getFeedback() {
if (success && backupSizeBytes >= 0)
- return String.format(" File size: %s.", getFriendlyFileSizeString(backupSizeBytes));
+ return String.format("File size: %s.", getFriendlyFileSizeString(backupSizeBytes));
else
return "";
}
diff --git a/src/main/java/com/keuin/kbackupfabric/operation/backup/BackupMethod.java b/src/main/java/com/keuin/kbackupfabric/operation/backup/method/BackupMethod.java
index b65a076..25a5952 100644
--- a/src/main/java/com/keuin/kbackupfabric/operation/backup/BackupMethod.java
+++ b/src/main/java/com/keuin/kbackupfabric/operation/backup/method/BackupMethod.java
@@ -1,6 +1,6 @@
-package com.keuin.kbackupfabric.operation.backup;
+package com.keuin.kbackupfabric.operation.backup.method;
-import com.keuin.kbackupfabric.operation.backup.feedback.PrimitiveBackupFeedback;
+import com.keuin.kbackupfabric.operation.backup.feedback.BackupFeedback;
import java.io.IOException;
@@ -15,7 +15,7 @@ public interface BackupMethod {
* @param backupName the backup name.
* @return if the backup operation succeed.
*/
- PrimitiveBackupFeedback backup(String backupName, String levelPath, String backupSaveDirectory) throws IOException;
+ BackupFeedback backup(String backupName, String levelPath, String backupSaveDirectory) throws IOException;
boolean restore(String backupName, String levelPath, String backupSaveDirectory) throws IOException;
diff --git a/src/main/java/com/keuin/kbackupfabric/operation/backup/method/IncrementalBackupMethod.java b/src/main/java/com/keuin/kbackupfabric/operation/backup/method/IncrementalBackupMethod.java
new file mode 100644
index 0000000..1dacc44
--- /dev/null
+++ b/src/main/java/com/keuin/kbackupfabric/operation/backup/method/IncrementalBackupMethod.java
@@ -0,0 +1,17 @@
+package com.keuin.kbackupfabric.operation.backup.method;
+
+import com.keuin.kbackupfabric.operation.backup.feedback.IncrementalBackupFeedback;
+
+import java.io.IOException;
+
+public class IncrementalBackupMethod implements BackupMethod {
+ @Override
+ public IncrementalBackupFeedback backup(String backupName, String levelPath, String backupSaveDirectory) throws IOException {
+ return null;
+ }
+
+ @Override
+ public boolean restore(String backupName, String levelPath, String backupSaveDirectory) throws IOException {
+ return false;
+ }
+}
diff --git a/src/main/java/com/keuin/kbackupfabric/operation/backup/PrimitiveBackupMethod.java b/src/main/java/com/keuin/kbackupfabric/operation/backup/method/PrimitiveBackupMethod.java
index 9c065a6..1f15346 100644
--- a/src/main/java/com/keuin/kbackupfabric/operation/backup/PrimitiveBackupMethod.java
+++ b/src/main/java/com/keuin/kbackupfabric/operation/backup/method/PrimitiveBackupMethod.java
@@ -1,4 +1,4 @@
-package com.keuin.kbackupfabric.operation.backup;
+package com.keuin.kbackupfabric.operation.backup.method;
import com.keuin.kbackupfabric.exception.ZipUtilException;
import com.keuin.kbackupfabric.metadata.BackupMetadata;
diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/name/BackupFileNameEncoder.java b/src/main/java/com/keuin/kbackupfabric/util/backup/name/BackupFileNameEncoder.java
index 83967b7..972403b 100644
--- a/src/main/java/com/keuin/kbackupfabric/util/backup/name/BackupFileNameEncoder.java
+++ b/src/main/java/com/keuin/kbackupfabric/util/backup/name/BackupFileNameEncoder.java
@@ -1,6 +1,7 @@
package com.keuin.kbackupfabric.util.backup.name;
import java.time.LocalDateTime;
+import java.time.format.DateTimeFormatter;
/**
* Encode and decode backup file name for a specific backup type.
@@ -38,12 +39,20 @@ public interface BackupFileNameEncoder {
}
class BackupBasicInformation {
+
public final String customName;
public final LocalDateTime time;
+ private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm.ss");
+
BackupBasicInformation(String customName, LocalDateTime time) {
this.customName = customName;
this.time = time;
}
+
+ @Override
+ public String toString() {
+ return String.format("%s, %s", customName, time.format(formatter));
+ }
}
}
diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/name/IncrementalBackupFileNameEncoder.java b/src/main/java/com/keuin/kbackupfabric/util/backup/name/IncrementalBackupFileNameEncoder.java
new file mode 100644
index 0000000..ae54930
--- /dev/null
+++ b/src/main/java/com/keuin/kbackupfabric/util/backup/name/IncrementalBackupFileNameEncoder.java
@@ -0,0 +1,33 @@
+package com.keuin.kbackupfabric.util.backup.name;
+
+import java.time.LocalDateTime;
+import java.time.format.DateTimeFormatter;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+public class IncrementalBackupFileNameEncoder implements BackupFileNameEncoder {
+ private static final String backupFileNamePrefix = "incremental-";
+ private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd_HH-mm-ss");
+
+ @Override
+ public String encode(String customName, LocalDateTime time) {
+ if (!isValidCustomName(customName))
+ throw new IllegalArgumentException("Invalid custom name");
+ String timeString = time.format(formatter);
+ return backupFileNamePrefix + "-" + timeString + "_" + customName + ".kbi";
+ }
+
+ @Override
+ public BackupFileNameEncoder.BackupBasicInformation decode(String fileName) {
+ Pattern pattern = Pattern.compile(
+ backupFileNamePrefix + "-" + "([0-9]{4}-[0-9]{2}-[0-9]{2}_[0-9]{2}-[0-9]{2}-[0-9]{2})_(.+)\\.kbi"
+ );
+ Matcher matcher = pattern.matcher(fileName);
+ if (matcher.find()) {
+ String timeString = matcher.group(1);
+ String customName = matcher.group(2);
+ return new BackupFileNameEncoder.BackupBasicInformation(customName, LocalDateTime.parse(timeString, formatter));
+ }
+ return null;
+ }
+}
diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/name/PrimitiveBackupFileNameEncoder.java b/src/main/java/com/keuin/kbackupfabric/util/backup/name/PrimitiveBackupFileNameEncoder.java
index 39c2403..bcba114 100644
--- a/src/main/java/com/keuin/kbackupfabric/util/backup/name/PrimitiveBackupFileNameEncoder.java
+++ b/src/main/java/com/keuin/kbackupfabric/util/backup/name/PrimitiveBackupFileNameEncoder.java
@@ -6,7 +6,6 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class PrimitiveBackupFileNameEncoder implements BackupFileNameEncoder {
-
private static final String backupFileNamePrefix = "kbackup";
private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd_HH-mm-ss");