This commit is contained in:
iceBear67 committed 2025-09-22 18:13:32 +08:00
1 parent 048451543b
commit 3a4256cee1
6 files changed
+54 -35

No files matched your search

@@ -1,7 +1,14 @@
package io.ib67.sfcraft; package io.ib67.sfcraft;
import com.mojang.brigadier.Command; 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.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.server.command.ServerCommandSource;
import net.minecraft.text.ClickEvent; import net.minecraft.text.ClickEvent;
import net.minecraft.text.Text; import net.minecraft.text.Text;
@@ -11,6 +18,10 @@ import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.Comparator; 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 { public class BackupCommands {
protected final JustBackupMod mod; protected final JustBackupMod mod;
@@ -111,10 +122,41 @@ public class BackupCommands {
} }
public int cmdHelp(CommandContext<ServerCommandSource> serverCommandSourceCommandContext) { public int cmdHelp(CommandContext<ServerCommandSource> serverCommandSourceCommandContext) {
// todo i18n
return 0; return 0;
} }
public int cmdCancelRestore(CommandContext<ServerCommandSource> serverCommandSourceCommandContext) { public int cmdCancelRestore(CommandContext<ServerCommandSource> serverCommandSourceCommandContext) {
restoreIssued = null;
serverCommandSourceCommandContext.getSource().sendMessage(Text.of("Operation cancelled."));
return 0; return 0;
} }
public void registerCommand(
CommandDispatcher<ServerCommandSource> 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<Suggestions> suggestBackups(CommandContext<ServerCommandSource> serverCommandSourceCommandContext, SuggestionsBuilder suggestionsBuilder) {
for (Backup value : mod.tracker.getTrackedBackups().values()) {
suggestionsBuilder = suggestionsBuilder.suggest(value.backupKey());
}
return CompletableFuture.completedFuture(suggestionsBuilder.build());
}
} }
@@ -12,7 +12,6 @@ import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.concurrent.Callable;
import java.util.function.Supplier; import java.util.function.Supplier;
import static io.ib67.sfcraft.JustBackupMod.PERFORMING_BACKUP; import static io.ib67.sfcraft.JustBackupMod.PERFORMING_BACKUP;
@@ -5,6 +5,9 @@ import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken; import com.google.gson.reflect.TypeToken;
import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.StringArgumentType; 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.JustBackupConfig;
import io.ib67.sfcraft.config.StorageOption; import io.ib67.sfcraft.config.StorageOption;
import io.ib67.sfcraft.config.serializer.StorageOptionSerializer; import io.ib67.sfcraft.config.serializer.StorageOptionSerializer;
@@ -104,7 +107,15 @@ public class JustBackupMod implements ModInitializer {
throw new RuntimeException(e); 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<Backup> issueBackup() { public CompletableFuture<Backup> issueBackup() {
@@ -127,33 +138,6 @@ public class JustBackupMod implements ModInitializer {
} }
} }
private void registerCommand(
CommandDispatcher<ServerCommandSource> 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() { private BackupStrategy createStrategy() {
return switch (config.option()) { return switch (config.option()) {
case StorageOption.Local local -> new LocalBackupStrategy(local); case StorageOption.Local local -> new LocalBackupStrategy(local);
@@ -161,7 +145,6 @@ public class JustBackupMod implements ModInitializer {
}; };
} }
@SneakyThrows @SneakyThrows
private void saveConfig(Path configPath) { private void saveConfig(Path configPath) {
LOGGER.info("Saving default configuration"); LOGGER.info("Saving default configuration");
@@ -2,7 +2,6 @@ package io.ib67.sfcraft.bundler;
import com.github.luben.zstd.ZstdOutputStreamNoFinalizer; import com.github.luben.zstd.ZstdOutputStreamNoFinalizer;
import io.ib67.sfcraft.WorldDir; import io.ib67.sfcraft.WorldDir;
import io.netty.buffer.Unpooled;
import lombok.Data; import lombok.Data;
import lombok.SneakyThrows; import lombok.SneakyThrows;
import lombok.experimental.Accessors; import lombok.experimental.Accessors;
@@ -16,7 +15,6 @@ import java.nio.channels.FileChannel;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.StandardOpenOption; import java.nio.file.StandardOpenOption;
import java.util.Arrays;
import java.util.List; import java.util.List;
@Accessors(fluent = true) @Accessors(fluent = true)
@@ -1,13 +1,11 @@
package io.ib67.sfcraft.bundler; package io.ib67.sfcraft.bundler;
import com.github.luben.zstd.ZstdOutputStreamNoFinalizer;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Getter; import lombok.Getter;
import java.io.Closeable; import java.io.Closeable;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.Channels; import java.nio.channels.Channels;
import java.nio.channels.FileChannel; import java.nio.channels.FileChannel;
import java.nio.file.Files; import java.nio.file.Files;
@@ -57,7 +57,6 @@ public class S3BackupStrategy implements BackupStrategy {
.contentType("application/octet-stream") .contentType("application/octet-stream")
.ifNoneMatch("*") // to avoid content overriding .ifNoneMatch("*") // to avoid content overriding
.key(object).build(), pathToBundle); .key(object).build(), pathToBundle);
//todo also calculate sha?
return new Backup(pathToBundle.getFileName().toString(), return new Backup(pathToBundle.getFileName().toString(),
object, "s3", object, "s3",
from.toString(), from.toString(),