Skip to content

Commit

Permalink
自定义dump还原
Browse files Browse the repository at this point in the history
@A0000_Xz的功能请求
  • Loading branch information
xia-mc committed Mar 20, 2024
1 parent 543b8fa commit bb9a694
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 7 deletions.
30 changes: 30 additions & 0 deletions src/main/java/top/infsky/timerecorder/command/DumpCommand.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<CommandSourceStack> context) {
Expand Down Expand Up @@ -45,5 +49,31 @@ public static int execute(CommandContext<CommandSourceStack> context) {
context.getSource().sendSuccess(() -> Component.literal("统计数据还原失败").withStyle(ChatFormatting.RED), true);
return 1;
}

public static class Custom {
public static int execute(CommandContext<CommandSourceStack> 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;
}
}
}
}
10 changes: 6 additions & 4 deletions src/main/java/top/infsky/timerecorder/command/ICmdEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<CommandSourceStack> dispatcher) {
public static void register(@NotNull CommandDispatcher<CommandSourceStack> dispatcher) {
dispatcher.register(literal("tr") // 没参数默认显示帮助信息
.executes(HelpCommand::execute)
.then(literal("help") // 显示帮助信息
Expand All @@ -21,7 +22,9 @@ public static void register(CommandDispatcher<CommandSourceStack> 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") // 对自己显示当日截止目前的所有玩家的统计信息
Expand All @@ -34,8 +37,7 @@ public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
.executes(RecallCommand::execute)
.then(literal("confirm") // 确认撤回(由/tr recall触发)
.then(argument("message_id", IntegerArgumentType.integer())
.executes(RecallCommand.Confirm::execute)
)))
.executes(RecallCommand.Confirm::execute))))
);
}
}
13 changes: 10 additions & 3 deletions src/main/java/top/infsky/timerecorder/data/StatsDump.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -60,7 +68,6 @@ public static int load() throws RuntimeException {
return -1;
}
}

/**
* 由StatsData实例生成它的dump
* @param stats StatsData实例
Expand Down

0 comments on commit bb9a694

Please sign in to comment.