summaryrefslogtreecommitdiff
path: root/src/main/java/com/keuin/kbackupfabric
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/com/keuin/kbackupfabric')
-rw-r--r--src/main/java/com/keuin/kbackupfabric/KBCommandRegister.java2
-rw-r--r--src/main/java/com/keuin/kbackupfabric/KBCommands.java24
-rw-r--r--src/main/java/com/keuin/kbackupfabric/KBPluginEvents.java6
-rw-r--r--src/main/java/com/keuin/kbackupfabric/metadata/BackupMetadata.java (renamed from src/main/java/com/keuin/kbackupfabric/data/BackupMetadata.java)5
-rw-r--r--src/main/java/com/keuin/kbackupfabric/metadata/MetadataHolder.java22
-rw-r--r--src/main/java/com/keuin/kbackupfabric/util/PrintUtil.java9
-rw-r--r--src/main/java/com/keuin/kbackupfabric/util/ZipUtil.java5
-rw-r--r--src/main/java/com/keuin/kbackupfabric/worker/BackupWorker.java2
8 files changed, 68 insertions, 7 deletions
diff --git a/src/main/java/com/keuin/kbackupfabric/KBCommandRegister.java b/src/main/java/com/keuin/kbackupfabric/KBCommandRegister.java
index 79029bf..674ac0c 100644
--- a/src/main/java/com/keuin/kbackupfabric/KBCommandRegister.java
+++ b/src/main/java/com/keuin/kbackupfabric/KBCommandRegister.java
@@ -12,7 +12,7 @@ public final class KBCommandRegister {
public static void registerCommands(CommandDispatcher<ServerCommandSource> dispatcher) {
// register /kb and /kb help for help menu
- dispatcher.register(CommandManager.literal("kb").executes(KBCommands::help));
+ dispatcher.register(CommandManager.literal("kb").executes(KBCommands::kb));
dispatcher.register(CommandManager.literal("kb").then(CommandManager.literal("help").executes(KBCommands::help)));
// register /kb list for showing the backup list. OP is required.
diff --git a/src/main/java/com/keuin/kbackupfabric/KBCommands.java b/src/main/java/com/keuin/kbackupfabric/KBCommands.java
index dbe5252..f2671ad 100644
--- a/src/main/java/com/keuin/kbackupfabric/KBCommands.java
+++ b/src/main/java/com/keuin/kbackupfabric/KBCommands.java
@@ -1,6 +1,7 @@
package com.keuin.kbackupfabric;
-import com.keuin.kbackupfabric.data.BackupMetadata;
+import com.keuin.kbackupfabric.metadata.BackupMetadata;
+import com.keuin.kbackupfabric.metadata.MetadataHolder;
import com.keuin.kbackupfabric.operation.AbstractConfirmableOperation;
import com.keuin.kbackupfabric.util.BackupFilesystemUtil;
import com.keuin.kbackupfabric.util.BackupNameTimeFormatter;
@@ -48,6 +49,27 @@ public final class KBCommands {
return SUCCESS;
}
+ /**
+ * Print the help menu. (May show extra info during the first run after restoring)
+ *
+ * @param context the context.
+ * @return stat code.
+ */
+ public static int kb(CommandContext<ServerCommandSource> context) {
+ int statCode = list(context);
+ if (MetadataHolder.hasMetadata()) {
+ // Output metadata info
+ msgStress(context, "Restored from backup " + MetadataHolder.getMetadata().getBackupName());
+ }
+ return statCode;
+ }
+
+ /**
+ * List all existing backups.
+ *
+ * @param context the context.
+ * @return stat code.
+ */
public static int list(CommandContext<ServerCommandSource> context) {
msgInfo(context, "Available backups: (file is not checked, manipulation may affect this plugin)");
MinecraftServer server = context.getSource().getMinecraftServer();
diff --git a/src/main/java/com/keuin/kbackupfabric/KBPluginEvents.java b/src/main/java/com/keuin/kbackupfabric/KBPluginEvents.java
index 1f39266..f3a3b07 100644
--- a/src/main/java/com/keuin/kbackupfabric/KBPluginEvents.java
+++ b/src/main/java/com/keuin/kbackupfabric/KBPluginEvents.java
@@ -1,6 +1,7 @@
package com.keuin.kbackupfabric;
-import com.keuin.kbackupfabric.data.BackupMetadata;
+import com.keuin.kbackupfabric.metadata.BackupMetadata;
+import com.keuin.kbackupfabric.metadata.MetadataHolder;
import com.keuin.kbackupfabric.util.BackupFilesystemUtil;
import com.keuin.kbackupfabric.util.BackupNameSuggestionProvider;
import com.keuin.kbackupfabric.util.PrintUtil;
@@ -46,7 +47,7 @@ public final class KBPluginEvents implements ModInitializer, ServerStartCallback
File metadataFile = new File(levelDirectory, BackupMetadata.metadataFileName);
if (metadataFile.exists()) {
// Metadata exists. Deserialize.
- BackupMetadata metadata = null;
+ BackupMetadata metadata;
FileInputStream fileInputStream = new FileInputStream(metadataFile);
ObjectInputStream in = new ObjectInputStream(fileInputStream);
metadata = (BackupMetadata) in.readObject();
@@ -54,6 +55,7 @@ public final class KBPluginEvents implements ModInitializer, ServerStartCallback
fileInputStream.close();
// Print metadata
+ MetadataHolder.setMetadata(metadata);
PrintUtil.info("Recovered from previous backup:");
PrintUtil.info("Backup Name: " + metadata.getBackupName());
PrintUtil.info("Create Time: " + (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date(metadata.getBackupTime())));
diff --git a/src/main/java/com/keuin/kbackupfabric/data/BackupMetadata.java b/src/main/java/com/keuin/kbackupfabric/metadata/BackupMetadata.java
index 48670c6..b8cb852 100644
--- a/src/main/java/com/keuin/kbackupfabric/data/BackupMetadata.java
+++ b/src/main/java/com/keuin/kbackupfabric/metadata/BackupMetadata.java
@@ -1,7 +1,10 @@
-package com.keuin.kbackupfabric.data;
+package com.keuin.kbackupfabric.metadata;
import java.io.Serializable;
+/**
+ * WARNING: DO NOT modify this class, or the plugin will be incompatible with backups created by older versions.
+ */
public class BackupMetadata implements Serializable {
public static final String metadataFileName = "kbackup_metadata";
private static final long serialVersionUID = 1L;
diff --git a/src/main/java/com/keuin/kbackupfabric/metadata/MetadataHolder.java b/src/main/java/com/keuin/kbackupfabric/metadata/MetadataHolder.java
new file mode 100644
index 0000000..4180a19
--- /dev/null
+++ b/src/main/java/com/keuin/kbackupfabric/metadata/MetadataHolder.java
@@ -0,0 +1,22 @@
+package com.keuin.kbackupfabric.metadata;
+
+/**
+ * In the first startup after restoring from a previous backup, the metadata is stored in this class.
+ * The setMetadata can only be called when startup
+ */
+public class MetadataHolder {
+ private static BackupMetadata metadata = null;
+
+ public static BackupMetadata getMetadata() {
+ return metadata;
+ }
+
+ public static void setMetadata(BackupMetadata metadata) {
+ if (MetadataHolder.metadata == null)
+ MetadataHolder.metadata = metadata;
+ }
+
+ public static boolean hasMetadata() {
+ return MetadataHolder.metadata != null;
+ }
+}
diff --git a/src/main/java/com/keuin/kbackupfabric/util/PrintUtil.java b/src/main/java/com/keuin/kbackupfabric/util/PrintUtil.java
index ca00ad4..11995e7 100644
--- a/src/main/java/com/keuin/kbackupfabric/util/PrintUtil.java
+++ b/src/main/java/com/keuin/kbackupfabric/util/PrintUtil.java
@@ -15,11 +15,16 @@ public final class PrintUtil {
private static final Object syncMessage = new Object();
private static final Style infoStyle = new Style().setColor(Formatting.WHITE);
+ private static final Style stressStyle = new Style().setColor(Formatting.AQUA).setBold(true);
private static final Style warnStyle = new Style().setColor(Formatting.YELLOW);
private static final Style errorStyle = new Style().setColor(Formatting.DARK_RED);
private static final Logger LOGGER = LogManager.getLogger();
+ public static CommandContext<ServerCommandSource> msgStress(CommandContext<ServerCommandSource> context, String messageText) {
+ return msgStress(context, messageText, false);
+ }
+
public static CommandContext<ServerCommandSource> msgInfo(CommandContext<ServerCommandSource> context, String messageText) {
return msgInfo(context, messageText, false);
}
@@ -32,6 +37,10 @@ public final class PrintUtil {
return msgErr(context, messageText, false);
}
+ public static CommandContext<ServerCommandSource> msgStress(CommandContext<ServerCommandSource> context, String messageText, boolean broadcastToOps) {
+ return message(context, messageText, broadcastToOps, stressStyle);
+ }
+
public static CommandContext<ServerCommandSource> msgInfo(CommandContext<ServerCommandSource> context, String messageText, boolean broadcastToOps) {
return message(context, messageText, broadcastToOps, infoStyle);
}
diff --git a/src/main/java/com/keuin/kbackupfabric/util/ZipUtil.java b/src/main/java/com/keuin/kbackupfabric/util/ZipUtil.java
index e819afc..ee2907e 100644
--- a/src/main/java/com/keuin/kbackupfabric/util/ZipUtil.java
+++ b/src/main/java/com/keuin/kbackupfabric/util/ZipUtil.java
@@ -1,6 +1,6 @@
package com.keuin.kbackupfabric.util;
-import com.keuin.kbackupfabric.data.BackupMetadata;
+import com.keuin.kbackupfabric.metadata.BackupMetadata;
import java.io.*;
import java.util.Enumeration;
@@ -21,6 +21,9 @@ public final class ZipUtil {
return;
}
+ if (file.getName().equals(BackupMetadata.metadataFileName))
+ return; // Reject
+
// 如果是文件,则直接压缩该文件
if (file.isFile()) {
int count, bufferLen = 1024;
diff --git a/src/main/java/com/keuin/kbackupfabric/worker/BackupWorker.java b/src/main/java/com/keuin/kbackupfabric/worker/BackupWorker.java
index 675d183..e972b4d 100644
--- a/src/main/java/com/keuin/kbackupfabric/worker/BackupWorker.java
+++ b/src/main/java/com/keuin/kbackupfabric/worker/BackupWorker.java
@@ -1,6 +1,6 @@
package com.keuin.kbackupfabric.worker;
-import com.keuin.kbackupfabric.data.BackupMetadata;
+import com.keuin.kbackupfabric.metadata.BackupMetadata;
import com.keuin.kbackupfabric.util.PrintUtil;
import com.keuin.kbackupfabric.util.ZipUtil;
import com.keuin.kbackupfabric.util.ZipUtilException;