diff --git a/src/main/java/io/ib67/sfcraft/BackupCommands.java b/src/main/java/io/ib67/sfcraft/BackupCommands.java index 7ac61bd..7b10391 100644 --- a/src/main/java/io/ib67/sfcraft/BackupCommands.java +++ b/src/main/java/io/ib67/sfcraft/BackupCommands.java @@ -1,7 +1,14 @@ package io.ib67.sfcraft; import com.mojang.brigadier.Command; +import com.mojang.brigadier.CommandDispatcher; +import com.mojang.brigadier.arguments.StringArgumentType; import com.mojang.brigadier.context.CommandContext; +import com.mojang.brigadier.suggestion.Suggestions; +import com.mojang.brigadier.suggestion.SuggestionsBuilder; +import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents; +import net.minecraft.command.CommandRegistryAccess; +import net.minecraft.server.command.CommandManager; import net.minecraft.server.command.ServerCommandSource; import net.minecraft.text.ClickEvent; import net.minecraft.text.Text; @@ -11,6 +18,10 @@ import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.util.Comparator; +import java.util.concurrent.CompletableFuture; + +import static net.minecraft.server.command.CommandManager.argument; +import static net.minecraft.server.command.CommandManager.literal; public class BackupCommands { protected final JustBackupMod mod; @@ -86,7 +97,7 @@ public class BackupCommands { try { var toOverride = Path.of(backup.from()); var old = Path.of(backup.from() + "_old"); - if(Files.exists(old)){ + if (Files.exists(old)) { try (var f = Files.walk(old)) { f.sorted(Comparator.reverseOrder()) .forEach(it -> { @@ -111,10 +122,41 @@ public class BackupCommands { } public int cmdHelp(CommandContext serverCommandSourceCommandContext) { + // todo i18n return 0; } public int cmdCancelRestore(CommandContext serverCommandSourceCommandContext) { + restoreIssued = null; + serverCommandSourceCommandContext.getSource().sendMessage(Text.of("Operation cancelled.")); return 0; } + + public void registerCommand( + CommandDispatcher dispatcher, + CommandRegistryAccess registry, CommandManager.RegistrationEnvironment env) { + dispatcher.register(literal("backup") + .requires(it -> it.hasPermissionLevel(3)) + .then(literal("help").executes(this::cmdHelp)) + .then(literal("list").executes(this::cmdList)) + .then(literal("cancelrestore").executes(this::cmdCancelRestore)) + .then(literal("delete") + .then(argument("backupName", StringArgumentType.greedyString()) + .suggests(this::suggestBackups) + .executes(this::cmdDelete))) + .then(literal("restore") + .then(argument("backupName", StringArgumentType.greedyString()) + .suggests(this::suggestBackups) + .executes(this::cmdRestore))) + .then(literal("create").executes(this::cmdIssueBackup)) + .then(literal("suspend").executes(this::cmdSuspend)) + ); + } + + private CompletableFuture suggestBackups(CommandContext serverCommandSourceCommandContext, SuggestionsBuilder suggestionsBuilder) { + for (Backup value : mod.tracker.getTrackedBackups().values()) { + suggestionsBuilder = suggestionsBuilder.suggest(value.backupKey()); + } + return CompletableFuture.completedFuture(suggestionsBuilder.build()); + } } diff --git a/src/main/java/io/ib67/sfcraft/BackupWorker.java b/src/main/java/io/ib67/sfcraft/BackupWorker.java index 5b34983..c3f3240 100644 --- a/src/main/java/io/ib67/sfcraft/BackupWorker.java +++ b/src/main/java/io/ib67/sfcraft/BackupWorker.java @@ -12,7 +12,6 @@ import java.nio.file.Files; import java.nio.file.Path; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; -import java.util.concurrent.Callable; import java.util.function.Supplier; import static io.ib67.sfcraft.JustBackupMod.PERFORMING_BACKUP; diff --git a/src/main/java/io/ib67/sfcraft/JustBackupMod.java b/src/main/java/io/ib67/sfcraft/JustBackupMod.java index 0aecb2f..e0c7ad2 100644 --- a/src/main/java/io/ib67/sfcraft/JustBackupMod.java +++ b/src/main/java/io/ib67/sfcraft/JustBackupMod.java @@ -5,6 +5,9 @@ import com.google.gson.GsonBuilder; import com.google.gson.reflect.TypeToken; import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.arguments.StringArgumentType; +import com.mojang.brigadier.context.CommandContext; +import com.mojang.brigadier.suggestion.Suggestions; +import com.mojang.brigadier.suggestion.SuggestionsBuilder; import io.ib67.sfcraft.config.JustBackupConfig; import io.ib67.sfcraft.config.StorageOption; import io.ib67.sfcraft.config.serializer.StorageOptionSerializer; @@ -104,7 +107,15 @@ public class JustBackupMod implements ModInitializer { throw new RuntimeException(e); } }); - CommandRegistrationCallback.EVENT.register(this::registerCommand); + var command = new BackupCommands(this); + CommandRegistrationCallback.EVENT.register(command::registerCommand); + ServerLifecycleEvents.SERVER_STOPPED.register(server -> { + scheduledBackupExecutor.shutdown(); + if (command.restoreIssued != null) { + command.restoreIssued.run(); + } + tracker.close(); + }); } public CompletableFuture issueBackup() { @@ -127,33 +138,6 @@ public class JustBackupMod implements ModInitializer { } } - private void registerCommand( - CommandDispatcher dispatcher, - CommandRegistryAccess registry, CommandManager.RegistrationEnvironment env) { - var commandHandler = new BackupCommands(this); - dispatcher.register(literal("backup") - .requires(it -> it.hasPermissionLevel(3)) - .then(literal("help").executes(commandHandler::cmdHelp)) - .then(literal("list").executes(commandHandler::cmdList)) - .then(literal("cancelrestore").executes(commandHandler::cmdCancelRestore)) - .then(literal("delete") - .then(argument("backupName", StringArgumentType.greedyString()) - .executes(commandHandler::cmdDelete))) - .then(literal("restore") - .then(argument("backupName", StringArgumentType.greedyString()) - .executes(commandHandler::cmdRestore))) - .then(literal("create").executes(commandHandler::cmdIssueBackup)) - .then(literal("suspend").executes(commandHandler::cmdSuspend)) - ); - ServerLifecycleEvents.SERVER_STOPPED.register(server -> { - scheduledBackupExecutor.shutdown(); - if (commandHandler.restoreIssued != null) { - commandHandler.restoreIssued.run(); - } - tracker.close(); - }); - } - private BackupStrategy createStrategy() { return switch (config.option()) { case StorageOption.Local local -> new LocalBackupStrategy(local); @@ -161,7 +145,6 @@ public class JustBackupMod implements ModInitializer { }; } - @SneakyThrows private void saveConfig(Path configPath) { LOGGER.info("Saving default configuration"); diff --git a/src/main/java/io/ib67/sfcraft/bundler/Bundle.java b/src/main/java/io/ib67/sfcraft/bundler/Bundle.java index 6574352..18f3011 100644 --- a/src/main/java/io/ib67/sfcraft/bundler/Bundle.java +++ b/src/main/java/io/ib67/sfcraft/bundler/Bundle.java @@ -2,7 +2,6 @@ package io.ib67.sfcraft.bundler; import com.github.luben.zstd.ZstdOutputStreamNoFinalizer; import io.ib67.sfcraft.WorldDir; -import io.netty.buffer.Unpooled; import lombok.Data; import lombok.SneakyThrows; import lombok.experimental.Accessors; @@ -16,7 +15,6 @@ import java.nio.channels.FileChannel; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.StandardOpenOption; -import java.util.Arrays; import java.util.List; @Accessors(fluent = true) diff --git a/src/main/java/io/ib67/sfcraft/bundler/SerialBundlerWriter.java b/src/main/java/io/ib67/sfcraft/bundler/SerialBundlerWriter.java index 6dae16f..8f20ec5 100644 --- a/src/main/java/io/ib67/sfcraft/bundler/SerialBundlerWriter.java +++ b/src/main/java/io/ib67/sfcraft/bundler/SerialBundlerWriter.java @@ -1,13 +1,11 @@ package io.ib67.sfcraft.bundler; -import com.github.luben.zstd.ZstdOutputStreamNoFinalizer; import lombok.AllArgsConstructor; import lombok.Getter; import java.io.Closeable; import java.io.IOException; import java.io.OutputStream; -import java.nio.ByteBuffer; import java.nio.channels.Channels; import java.nio.channels.FileChannel; import java.nio.file.Files; diff --git a/src/main/java/io/ib67/sfcraft/strategy/S3BackupStrategy.java b/src/main/java/io/ib67/sfcraft/strategy/S3BackupStrategy.java index bf67bc6..50cda5a 100644 --- a/src/main/java/io/ib67/sfcraft/strategy/S3BackupStrategy.java +++ b/src/main/java/io/ib67/sfcraft/strategy/S3BackupStrategy.java @@ -57,7 +57,6 @@ public class S3BackupStrategy implements BackupStrategy { .contentType("application/octet-stream") .ifNoneMatch("*") // to avoid content overriding .key(object).build(), pathToBundle); - //todo also calculate sha? return new Backup(pathToBundle.getFileName().toString(), object, "s3", from.toString(),