From ea01faf3947814fe7ecaa3705e5fbf0f171f7e48 Mon Sep 17 00:00:00 2001 From: Keuin Date: Fri, 24 Apr 2020 13:43:20 +0800 Subject: Added hint at the "/kb" output in the first run after restoring from a backup. Warning: Backup in this version is not compatible with older version! --- .../com/keuin/kbackupfabric/KBCommandRegister.java | 2 +- .../java/com/keuin/kbackupfabric/KBCommands.java | 24 +++++++++++++++++++- .../com/keuin/kbackupfabric/KBPluginEvents.java | 6 +++-- .../keuin/kbackupfabric/data/BackupMetadata.java | 23 ------------------- .../kbackupfabric/metadata/BackupMetadata.java | 26 ++++++++++++++++++++++ .../kbackupfabric/metadata/MetadataHolder.java | 22 ++++++++++++++++++ .../com/keuin/kbackupfabric/util/PrintUtil.java | 9 ++++++++ .../java/com/keuin/kbackupfabric/util/ZipUtil.java | 5 ++++- .../keuin/kbackupfabric/worker/BackupWorker.java | 2 +- 9 files changed, 90 insertions(+), 29 deletions(-) delete mode 100644 src/main/java/com/keuin/kbackupfabric/data/BackupMetadata.java create mode 100644 src/main/java/com/keuin/kbackupfabric/metadata/BackupMetadata.java create mode 100644 src/main/java/com/keuin/kbackupfabric/metadata/MetadataHolder.java (limited to 'src/main/java') 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 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 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 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/data/BackupMetadata.java deleted file mode 100644 index 48670c6..0000000 --- a/src/main/java/com/keuin/kbackupfabric/data/BackupMetadata.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.keuin.kbackupfabric.data; - -import java.io.Serializable; - -public class BackupMetadata implements Serializable { - public static final String metadataFileName = "kbackup_metadata"; - private static final long serialVersionUID = 1L; - private final long BackupTime; - private final String backupName; - - public BackupMetadata(long backupTime, String backupName) { - BackupTime = backupTime; - this.backupName = backupName; - } - - public long getBackupTime() { - return BackupTime; - } - - public String getBackupName() { - return backupName; - } -} diff --git a/src/main/java/com/keuin/kbackupfabric/metadata/BackupMetadata.java b/src/main/java/com/keuin/kbackupfabric/metadata/BackupMetadata.java new file mode 100644 index 0000000..b8cb852 --- /dev/null +++ b/src/main/java/com/keuin/kbackupfabric/metadata/BackupMetadata.java @@ -0,0 +1,26 @@ +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; + private final long BackupTime; + private final String backupName; + + public BackupMetadata(long backupTime, String backupName) { + BackupTime = backupTime; + this.backupName = backupName; + } + + public long getBackupTime() { + return BackupTime; + } + + public String getBackupName() { + return backupName; + } +} 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 msgStress(CommandContext context, String messageText) { + return msgStress(context, messageText, false); + } + public static CommandContext msgInfo(CommandContext context, String messageText) { return msgInfo(context, messageText, false); } @@ -32,6 +37,10 @@ public final class PrintUtil { return msgErr(context, messageText, false); } + public static CommandContext msgStress(CommandContext context, String messageText, boolean broadcastToOps) { + return message(context, messageText, broadcastToOps, stressStyle); + } + public static CommandContext msgInfo(CommandContext 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; -- cgit v1.2.3