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! --- README.md | 3 +-- gradle.properties | 2 +- .../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 +- 11 files changed, 92 insertions(+), 32 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 diff --git a/README.md b/README.md index e3daa47..5441653 100644 --- a/README.md +++ b/README.md @@ -19,9 +19,8 @@ commands: To-Do List: -- Restoration of player data may not be correct. -- Optimize log output, normal output and op broadcast output. - More thorough test. - Enhance ZipUtil for hashing sub-files and generating incremental diff-table. (A:Add, M:Modification, D:Deletion) - Optimize help menu. (colored command help menu) +- Add op login hint in the next start after restoring. - Implement incremental backup. diff --git a/gradle.properties b/gradle.properties index 16593c4..aba6679 100644 --- a/gradle.properties +++ b/gradle.properties @@ -6,7 +6,7 @@ minecraft_version=1.14.4 yarn_mappings=1.14.4+build.16 loader_version=0.8.2+build.194 # Mod Properties -mod_version=1.2.0-dev +mod_version=1.3.0-dev maven_group=com.keuin.kbackupfabric archives_base_name=kbackup-fabric # Dependencies 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