diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/main/java/com/keuin/kbackupfabric/KBCommands.java | 18 | ||||
-rw-r--r-- | src/main/java/com/keuin/kbackupfabric/KBPluginEvents.java | 15 | ||||
-rw-r--r-- | src/main/java/com/keuin/kbackupfabric/data/PendingOperation.java | 41 | ||||
-rw-r--r-- | src/main/java/com/keuin/kbackupfabric/data/PendingOperationType.java | 6 | ||||
-rw-r--r-- | src/main/java/com/keuin/kbackupfabric/operation/AbstractConfirmableOperation.java (renamed from src/main/java/com/keuin/kbackupfabric/operation/Confirmable.java) | 6 | ||||
-rw-r--r-- | src/main/java/com/keuin/kbackupfabric/operation/DeleteOperation.java | 13 | ||||
-rw-r--r-- | src/main/java/com/keuin/kbackupfabric/operation/RestoreOperation.java | 8 | ||||
-rw-r--r-- | src/main/java/com/keuin/kbackupfabric/util/PrintUtil.java | 20 | ||||
-rw-r--r-- | src/main/java/com/keuin/kbackupfabric/worker/BackupWorker.java | 12 | ||||
-rw-r--r-- | src/main/java/com/keuin/kbackupfabric/worker/RestoreWorker.java | 19 |
10 files changed, 61 insertions, 97 deletions
diff --git a/src/main/java/com/keuin/kbackupfabric/KBCommands.java b/src/main/java/com/keuin/kbackupfabric/KBCommands.java index 8bade76..dbe5252 100644 --- a/src/main/java/com/keuin/kbackupfabric/KBCommands.java +++ b/src/main/java/com/keuin/kbackupfabric/KBCommands.java @@ -1,7 +1,7 @@ package com.keuin.kbackupfabric; import com.keuin.kbackupfabric.data.BackupMetadata; -import com.keuin.kbackupfabric.operation.Confirmable; +import com.keuin.kbackupfabric.operation.AbstractConfirmableOperation; import com.keuin.kbackupfabric.util.BackupFilesystemUtil; import com.keuin.kbackupfabric.util.BackupNameTimeFormatter; import com.keuin.kbackupfabric.util.PrintUtil; @@ -10,8 +10,6 @@ import com.mojang.brigadier.arguments.StringArgumentType; import com.mojang.brigadier.context.CommandContext; import net.minecraft.server.MinecraftServer; import net.minecraft.server.command.ServerCommandSource; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; import java.io.File; import java.util.ArrayList; @@ -28,10 +26,10 @@ public final class KBCommands { private static final int SUCCESS = 1; private static final int FAILED = -1; - private static final Logger LOGGER = LogManager.getLogger(); + //private static final Logger LOGGER = LogManager.getLogger(); private static final List<String> backupNameList = new ArrayList<>(); // index -> backupName - private static Confirmable pendingOperation = null; + private static AbstractConfirmableOperation pendingOperation = null; /** * Print the help menu. @@ -109,7 +107,7 @@ public final class KBCommands { } // Update pending task - pendingOperation = Confirmable.createDeleteOperation(context, backupName); + pendingOperation = AbstractConfirmableOperation.createDeleteOperation(context, backupName); msgWarn(context, String.format("DELETION WARNING: The deletion is irreversible! You will lose the backup %s permanently. Use /kb confirm to start or /kb cancel to abort.", backupName), true); return SUCCESS; @@ -140,7 +138,7 @@ public final class KBCommands { } // Update pending task - pendingOperation = Confirmable.createRestoreOperation(context, backupName); + pendingOperation = AbstractConfirmableOperation.createRestoreOperation(context, backupName); msgWarn(context, String.format("RESET WARNING: You will LOSE YOUR CURRENT WORLD PERMANENTLY! The worlds will be replaced with backup %s . Use /kb confirm to start or /kb cancel to abort.", backupName), true); return SUCCESS; @@ -173,7 +171,7 @@ public final class KBCommands { // Do backup BackupMetadata metadata = new BackupMetadata(System.currentTimeMillis(), backupName); - LOGGER.info("Invoking backup worker ..."); + PrintUtil.info("Invoking backup worker ..."); BackupWorker.invoke(context, backupName, metadata); return SUCCESS; } @@ -189,9 +187,9 @@ public final class KBCommands { msgWarn(context, "Nothing to be confirmed. Please execute /kb restore <backup_name> first."); return FAILED; } - Confirmable confirmable = pendingOperation; + AbstractConfirmableOperation operation = pendingOperation; pendingOperation = null; - return confirmable.confirm() ? SUCCESS : FAILED; // block compiler's complain. + return operation.confirm() ? SUCCESS : FAILED; // block compiler's complain. } /** diff --git a/src/main/java/com/keuin/kbackupfabric/KBPluginEvents.java b/src/main/java/com/keuin/kbackupfabric/KBPluginEvents.java index 47983b7..1f39266 100644 --- a/src/main/java/com/keuin/kbackupfabric/KBPluginEvents.java +++ b/src/main/java/com/keuin/kbackupfabric/KBPluginEvents.java @@ -3,12 +3,11 @@ package com.keuin.kbackupfabric; import com.keuin.kbackupfabric.data.BackupMetadata; import com.keuin.kbackupfabric.util.BackupFilesystemUtil; import com.keuin.kbackupfabric.util.BackupNameSuggestionProvider; +import com.keuin.kbackupfabric.util.PrintUtil; import net.fabricmc.api.ModInitializer; import net.fabricmc.fabric.api.event.server.ServerStartCallback; import net.fabricmc.fabric.api.registry.CommandRegistry; import net.minecraft.server.MinecraftServer; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; import java.io.File; import java.io.FileInputStream; @@ -25,18 +24,18 @@ import static org.apache.commons.io.FileUtils.forceDelete; */ public final class KBPluginEvents implements ModInitializer, ServerStartCallback { - private static final Logger LOGGER = LogManager.getLogger(); + //private static final Logger LOGGER = LogManager.getLogger(); @Override public void onInitialize() { - System.out.println("[KBackup] Binding events and commands ..."); + System.out.println("Binding events and commands ..."); CommandRegistry.INSTANCE.register(false, KBCommandRegister::registerCommands); ServerStartCallback.EVENT.register(this); } @Override public void onStartServer(MinecraftServer server) { - LOGGER.debug("[KBackup] Initializing ..."); + PrintUtil.debug("Initializing ..."); // Update backup suggestion list BackupNameSuggestionProvider.setBackupSaveDirectory(BackupFilesystemUtil.getBackupSaveDirectory(server).getPath()); @@ -55,9 +54,9 @@ public final class KBPluginEvents implements ModInitializer, ServerStartCallback fileInputStream.close(); // Print metadata - LOGGER.info("[KBackup] Recovered from previous backup:"); - LOGGER.info("Backup Name: " + metadata.getBackupName()); - LOGGER.info("Create Time: " + (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date(metadata.getBackupTime()))); + 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()))); // Delete metadata file if (!metadataFile.delete()) { diff --git a/src/main/java/com/keuin/kbackupfabric/data/PendingOperation.java b/src/main/java/com/keuin/kbackupfabric/data/PendingOperation.java deleted file mode 100644 index 8f7490c..0000000 --- a/src/main/java/com/keuin/kbackupfabric/data/PendingOperation.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.keuin.kbackupfabric.data; - -public class PendingOperation { - private final PendingOperationType operationType; - private final String backupName; - - public PendingOperation(PendingOperationType operationType, String backupName) { - this.operationType = operationType; - this.backupName = backupName; - } - - public static PendingOperation deleteOperation(String backupName) { - return new PendingOperation(PendingOperationType.DELETE, backupName); - } - - public static PendingOperation restoreOperation(String backupName) { - return new PendingOperation(PendingOperationType.RESTORE_TO, backupName); - } - - public boolean isDelete() { - return operationType == PendingOperationType.DELETE; - } - - public boolean isRestore() { - return operationType == PendingOperationType.RESTORE_TO; - } - - public String getBackupName() { - return backupName; - } - - @Override - public String toString() { - String op = "operation"; - if (isDelete()) - op = "deletion"; - if (isRestore()) - op = "restoration"; - return String.format("%s on backup %s", op, getBackupName()); - } -} diff --git a/src/main/java/com/keuin/kbackupfabric/data/PendingOperationType.java b/src/main/java/com/keuin/kbackupfabric/data/PendingOperationType.java deleted file mode 100644 index f86fc0d..0000000 --- a/src/main/java/com/keuin/kbackupfabric/data/PendingOperationType.java +++ /dev/null @@ -1,6 +0,0 @@ -package com.keuin.kbackupfabric.data; - -enum PendingOperationType { - RESTORE_TO, - DELETE -} diff --git a/src/main/java/com/keuin/kbackupfabric/operation/Confirmable.java b/src/main/java/com/keuin/kbackupfabric/operation/AbstractConfirmableOperation.java index c8fdd71..d666a34 100644 --- a/src/main/java/com/keuin/kbackupfabric/operation/Confirmable.java +++ b/src/main/java/com/keuin/kbackupfabric/operation/AbstractConfirmableOperation.java @@ -3,13 +3,13 @@ package com.keuin.kbackupfabric.operation; import com.mojang.brigadier.context.CommandContext; import net.minecraft.server.command.ServerCommandSource; -public abstract class Confirmable { +public abstract class AbstractConfirmableOperation { - public static Confirmable createRestoreOperation(CommandContext<ServerCommandSource> context, String backupName) { + public static AbstractConfirmableOperation createRestoreOperation(CommandContext<ServerCommandSource> context, String backupName) { return new RestoreOperation(context, backupName); } - public static Confirmable createDeleteOperation(CommandContext<ServerCommandSource> context, String backupName) { + public static AbstractConfirmableOperation createDeleteOperation(CommandContext<ServerCommandSource> context, String backupName) { return new DeleteOperation(context, backupName); } diff --git a/src/main/java/com/keuin/kbackupfabric/operation/DeleteOperation.java b/src/main/java/com/keuin/kbackupfabric/operation/DeleteOperation.java index 1e28324..be7734d 100644 --- a/src/main/java/com/keuin/kbackupfabric/operation/DeleteOperation.java +++ b/src/main/java/com/keuin/kbackupfabric/operation/DeleteOperation.java @@ -1,10 +1,9 @@ package com.keuin.kbackupfabric.operation; +import com.keuin.kbackupfabric.util.PrintUtil; import com.mojang.brigadier.context.CommandContext; import net.minecraft.server.MinecraftServer; import net.minecraft.server.command.ServerCommandSource; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; import java.io.File; import java.io.IOException; @@ -15,9 +14,9 @@ import static com.keuin.kbackupfabric.util.PrintUtil.msgErr; import static com.keuin.kbackupfabric.util.PrintUtil.msgInfo; import static org.apache.commons.io.FileUtils.forceDelete; -class DeleteOperation extends Confirmable { +class DeleteOperation extends AbstractConfirmableOperation { - private static final Logger LOGGER = LogManager.getLogger(); + //private static final Logger LOGGER = LogManager.getLogger(); private final String backupName; private final CommandContext<ServerCommandSource> context; @@ -30,13 +29,13 @@ class DeleteOperation extends Confirmable { public boolean confirm() { MinecraftServer server = context.getSource().getMinecraftServer(); String backupFileName = getBackupFileName(backupName); - LOGGER.info("Deleting backup " + backupName); + PrintUtil.info("Deleting backup " + backupName); File backupFile = new File(getBackupSaveDirectory(server), backupFileName); int tryCounter = 0; do { if (tryCounter == 5) { String msg = "Failed to delete file " + backupFileName; - LOGGER.error(msg); + PrintUtil.error(msg); msgErr(context, msg); return false; } @@ -47,7 +46,7 @@ class DeleteOperation extends Confirmable { } ++tryCounter; } while (backupFile.exists()); - LOGGER.info("Deleted backup " + backupName); + PrintUtil.info("Deleted backup " + backupName); msgInfo(context, "Deleted backup " + backupName); return true; } diff --git a/src/main/java/com/keuin/kbackupfabric/operation/RestoreOperation.java b/src/main/java/com/keuin/kbackupfabric/operation/RestoreOperation.java index 48dd413..2d0c403 100644 --- a/src/main/java/com/keuin/kbackupfabric/operation/RestoreOperation.java +++ b/src/main/java/com/keuin/kbackupfabric/operation/RestoreOperation.java @@ -5,16 +5,14 @@ import com.keuin.kbackupfabric.worker.RestoreWorker; import com.mojang.brigadier.context.CommandContext; import net.minecraft.server.MinecraftServer; import net.minecraft.server.command.ServerCommandSource; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; import java.io.File; import static com.keuin.kbackupfabric.util.BackupFilesystemUtil.*; -class RestoreOperation extends Confirmable { +class RestoreOperation extends AbstractConfirmableOperation { - private static final Logger LOGGER = LogManager.getLogger(); + //private static final Logger LOGGER = LogManager.getLogger(); private final String backupName; private final CommandContext<ServerCommandSource> context; @@ -30,7 +28,7 @@ class RestoreOperation extends Confirmable { PrintUtil.msgInfo(context, String.format("Restoring to previous world %s ...", backupName), true); String backupFileName = getBackupFileName(backupName); - LOGGER.debug("Backup file name: " + backupFileName); + PrintUtil.debug("Backup file name: " + backupFileName); File backupFile = new File(getBackupSaveDirectory(server), backupFileName); PrintUtil.msgInfo(context, "Server will shutdown in a few seconds, depended on your world size and the disk speed, the restore progress may take seconds or minutes.", true); diff --git a/src/main/java/com/keuin/kbackupfabric/util/PrintUtil.java b/src/main/java/com/keuin/kbackupfabric/util/PrintUtil.java index fe98cfa..ca00ad4 100644 --- a/src/main/java/com/keuin/kbackupfabric/util/PrintUtil.java +++ b/src/main/java/com/keuin/kbackupfabric/util/PrintUtil.java @@ -6,6 +6,9 @@ import net.minecraft.text.LiteralText; import net.minecraft.text.Style; import net.minecraft.text.Text; import net.minecraft.util.Formatting; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + public final class PrintUtil { @@ -15,6 +18,7 @@ public final class PrintUtil { 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> msgInfo(CommandContext<ServerCommandSource> context, String messageText) { return msgInfo(context, messageText, false); @@ -48,4 +52,20 @@ public final class PrintUtil { } return context; } + + public static void debug(String string) { + LOGGER.debug("[KBackup] " + string); + } + + public static void info(String string) { + LOGGER.info("[KBackup] " + string); + } + + public static void warn(String string) { + LOGGER.warn("[KBackup] " + string); + } + + public static void error(String string) { + LOGGER.error("[KBackup] " + string); + } } diff --git a/src/main/java/com/keuin/kbackupfabric/worker/BackupWorker.java b/src/main/java/com/keuin/kbackupfabric/worker/BackupWorker.java index 3303f70..675d183 100644 --- a/src/main/java/com/keuin/kbackupfabric/worker/BackupWorker.java +++ b/src/main/java/com/keuin/kbackupfabric/worker/BackupWorker.java @@ -8,8 +8,6 @@ import com.mojang.brigadier.context.CommandContext; import net.minecraft.server.MinecraftServer; import net.minecraft.server.command.ServerCommandSource; import net.minecraft.world.World; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; import java.io.File; import java.io.IOException; @@ -25,7 +23,7 @@ import static com.keuin.kbackupfabric.util.PrintUtil.msgInfo; */ public final class BackupWorker implements Runnable { - private static final Logger LOGGER = LogManager.getLogger(); + //private static final Logger LOGGER = LogManager.getLogger(); private final CommandContext<ServerCommandSource> context; private final String backupName; private final Map<World, Boolean> oldWorldsSavingDisabled; @@ -56,9 +54,9 @@ public final class BackupWorker implements Runnable { }); // Force to save all player data and worlds - LOGGER.info("Saving players ..."); + PrintUtil.info("Saving players ..."); server.getPlayerManager().saveAllPlayerData(); - LOGGER.info("Saving worlds ..."); + PrintUtil.info("Saving worlds ..."); server.save(true, true, true); // Start threaded worker @@ -85,8 +83,8 @@ public final class BackupWorker implements Runnable { // Make zip String levelPath = getLevelPath(server); String backupFileName = getBackupFileName(backupName); - LOGGER.debug(String.format("zip(srcPath=%s, destPath=%s)", levelPath, backupSaveDirectoryFile.toString())); - LOGGER.info("Compressing level ..."); + PrintUtil.debug(String.format("zip(srcPath=%s, destPath=%s)", levelPath, backupSaveDirectoryFile.toString())); + PrintUtil.info("Compressing level ..."); ZipUtil.makeBackupZip(levelPath, backupSaveDirectoryFile.toString(), backupFileName, backupMetadata); File backupZipFile = new File(backupSaveDirectoryFile, backupFileName); diff --git a/src/main/java/com/keuin/kbackupfabric/worker/RestoreWorker.java b/src/main/java/com/keuin/kbackupfabric/worker/RestoreWorker.java index 54e7beb..58560a1 100644 --- a/src/main/java/com/keuin/kbackupfabric/worker/RestoreWorker.java +++ b/src/main/java/com/keuin/kbackupfabric/worker/RestoreWorker.java @@ -1,10 +1,9 @@ package com.keuin.kbackupfabric.worker; +import com.keuin.kbackupfabric.util.PrintUtil; import com.keuin.kbackupfabric.util.ZipUtil; import com.keuin.kbackupfabric.util.ZipUtilException; import net.minecraft.server.MinecraftServer; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; import java.io.File; import java.io.IOException; @@ -17,7 +16,7 @@ import static org.apache.commons.io.FileUtils.forceDelete; */ public final class RestoreWorker implements Runnable { - private static final Logger LOGGER = LogManager.getLogger(); + //private static final Logger LOGGER = LogManager.getLogger(); private final Thread serverThread; private final String backupFilePath; @@ -44,7 +43,7 @@ public final class RestoreWorker implements Runnable { public void run() { try { // Wait server thread die - LOGGER.info("Waiting for the server thread to exit ..."); + PrintUtil.info("Waiting for the server thread to exit ..."); while (serverThread.isAlive()) { try { serverThread.join(); @@ -52,14 +51,14 @@ public final class RestoreWorker implements Runnable { } } - LOGGER.info("Wait for 5 seconds ..."); + PrintUtil.info("Wait for 5 seconds ..."); try { Thread.sleep(5000); } catch (InterruptedException ignored) { } // Delete old level - LOGGER.info("Server stopped. Deleting old level ..."); + PrintUtil.info("Server stopped. Deleting old level ..."); File levelDirFile = new File(levelDirectory); long startTime = System.currentTimeMillis(); @@ -80,17 +79,17 @@ public final class RestoreWorker implements Runnable { } } if (levelDirFile.exists()) { - LOGGER.error(String.format("Cannot restore: failed to delete old level %s .", levelDirFile.getName())); + PrintUtil.error(String.format("Cannot restore: failed to delete old level %s .", levelDirFile.getName())); return; } // Decompress archive - LOGGER.info("Decompressing archived level"); + PrintUtil.info("Decompressing archived level"); ZipUtil.unzip(backupFilePath, levelDirectory, false); long endTime = System.currentTimeMillis(); - LOGGER.info(String.format("Restore complete! (%.2fs) Please restart the server manually.", (endTime - startTime) / 1000.0)); + PrintUtil.info(String.format("Restore complete! (%.2fs) Please restart the server manually.", (endTime - startTime) / 1000.0)); } catch (SecurityException | IOException | ZipUtilException e) { - LOGGER.error("An exception occurred while restoring: " + e.getMessage()); + PrintUtil.error("An exception occurred while restoring: " + e.getMessage()); } } } |