From bb9a6948981ee806d38731f4c10173c7aed14160 Mon Sep 17 00:00:00 2001 From: xia-mc <2052472631@qq.com> Date: Wed, 20 Mar 2024 14:24:58 +0800 Subject: [PATCH] =?UTF-8?q?=E8=87=AA=E5=AE=9A=E4=B9=89dump=E8=BF=98?= =?UTF-8?q?=E5=8E=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit @A0000_Xz的功能请求 --- .../timerecorder/command/DumpCommand.java | 30 +++++++++++++++++++ .../timerecorder/command/ICmdEvent.java | 10 ++++--- .../infsky/timerecorder/data/StatsDump.java | 13 ++++++-- 3 files changed, 46 insertions(+), 7 deletions(-) diff --git a/src/main/java/top/infsky/timerecorder/command/DumpCommand.java b/src/main/java/top/infsky/timerecorder/command/DumpCommand.java index 54d01bc..55c2143 100644 --- a/src/main/java/top/infsky/timerecorder/command/DumpCommand.java +++ b/src/main/java/top/infsky/timerecorder/command/DumpCommand.java @@ -1,5 +1,6 @@ package top.infsky.timerecorder.command; +import com.mojang.brigadier.arguments.StringArgumentType; import com.mojang.brigadier.context.CommandContext; import net.minecraft.ChatFormatting; import net.minecraft.commands.CommandSourceStack; @@ -8,6 +9,9 @@ import top.infsky.timerecorder.data.StatsDump; import top.infsky.timerecorder.log.LogUtils; +import java.nio.file.InvalidPathException; +import java.nio.file.Path; + public class DumpCommand { public static class Save { public static int execute(CommandContext context) { @@ -45,5 +49,31 @@ public static int execute(CommandContext context) { context.getSource().sendSuccess(() -> Component.literal("统计数据还原失败").withStyle(ChatFormatting.RED), true); return 1; } + + public static class Custom { + public static int execute(CommandContext context) { + LogUtils.LOGGER.info("通过指令加载自定义统计数据"); + + Path path; + try { + path = Utils.CONFIG_FOLDER.resolve(StringArgumentType.getString(context, "filename")); + } catch (InvalidPathException e) { + path = Path.of(StringArgumentType.getString(context, "filename")); + } + + try { + if (StatsDump.load(path) == 1) { + context.getSource().sendSuccess(() -> Component.literal("成功还原统计数据。").withStyle(ChatFormatting.GREEN), true); + return 1; + } + } catch (RuntimeException e) { + LogUtils.LOGGER.error("在加载统计数据中出现了", e); + context.getSource().sendFailure(Component.literal(e.getMessage())); + return -1; + } + context.getSource().sendSuccess(() -> Component.literal("统计数据还原失败").withStyle(ChatFormatting.RED), true); + return 1; + } + } } } diff --git a/src/main/java/top/infsky/timerecorder/command/ICmdEvent.java b/src/main/java/top/infsky/timerecorder/command/ICmdEvent.java index c488eda..8693c82 100644 --- a/src/main/java/top/infsky/timerecorder/command/ICmdEvent.java +++ b/src/main/java/top/infsky/timerecorder/command/ICmdEvent.java @@ -4,11 +4,12 @@ import com.mojang.brigadier.arguments.IntegerArgumentType; import com.mojang.brigadier.arguments.StringArgumentType; import net.minecraft.commands.CommandSourceStack; +import org.jetbrains.annotations.NotNull; import static net.minecraft.commands.Commands.*; public class ICmdEvent { - public static void register(CommandDispatcher dispatcher) { + public static void register(@NotNull CommandDispatcher dispatcher) { dispatcher.register(literal("tr") // 没参数默认显示帮助信息 .executes(HelpCommand::execute) .then(literal("help") // 显示帮助信息 @@ -21,7 +22,9 @@ public static void register(CommandDispatcher dispatcher) { .then(literal("save") // 保存迄今的统计数据 .executes(DumpCommand.Save::execute)) .then(literal("load") // 从文件还原统计数据 - .executes(DumpCommand.Load::execute))) + .executes(DumpCommand.Load::execute) + .then(argument("filename", StringArgumentType.string()) + .executes(DumpCommand.Load.Custom::execute)))) .then(literal("report") // 对自己显示当日截止目前的统计信息 .executes(ReportCommand::execute)) .then(literal("reportall") // 对自己显示当日截止目前的所有玩家的统计信息 @@ -34,8 +37,7 @@ public static void register(CommandDispatcher dispatcher) { .executes(RecallCommand::execute) .then(literal("confirm") // 确认撤回(由/tr recall触发) .then(argument("message_id", IntegerArgumentType.integer()) - .executes(RecallCommand.Confirm::execute) - ))) + .executes(RecallCommand.Confirm::execute)))) ); } } diff --git a/src/main/java/top/infsky/timerecorder/data/StatsDump.java b/src/main/java/top/infsky/timerecorder/data/StatsDump.java index d8be7ef..5aad551 100644 --- a/src/main/java/top/infsky/timerecorder/data/StatsDump.java +++ b/src/main/java/top/infsky/timerecorder/data/StatsDump.java @@ -48,10 +48,18 @@ public static int save(final @NotNull JsonObject dump) { public static int load() throws RuntimeException { try { - Path path = getLatestDump(); + return load(getLatestDump()); + } catch (IOException e) { + LogUtils.LOGGER.error("尝试还原统计数据时遇到异常", e); + return -1; + } + } + + public static int load(Path path) throws RuntimeException { + try { if (!Files.exists(path)) { LogUtils.LOGGER.error("尝试还原统计数据时遇到异常:文件不存在"); - throw new RuntimeException("文件不存在!"); + throw new IOException("文件不存在!"); } JsonObject dump = new Gson().fromJson(Files.readString(path), JsonObject.class); return fromDump(dump, false); @@ -60,7 +68,6 @@ public static int load() throws RuntimeException { return -1; } } - /** * 由StatsData实例生成它的dump * @param stats StatsData实例