|
| 1 | +package io.github.qwerty770.advhelper; |
| 2 | + |
| 3 | +import com.google.gson.JsonObject; |
| 4 | +import com.google.gson.internal.Streams; |
| 5 | +import com.google.gson.stream.JsonWriter; |
| 6 | +import com.mojang.brigadier.CommandDispatcher; |
| 7 | +import com.mojang.brigadier.arguments.StringArgumentType; |
| 8 | +import com.mojang.brigadier.context.CommandContext; |
| 9 | +import net.fabricmc.api.EnvType; |
| 10 | +import net.fabricmc.api.Environment; |
| 11 | +import net.fabricmc.fabric.api.client.command.v2.ClientCommandManager; |
| 12 | +import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource; |
| 13 | +import net.minecraft.advancements.*; |
| 14 | +import net.minecraft.network.chat.Component; |
| 15 | + |
| 16 | +import java.io.File; |
| 17 | +import java.io.FileWriter; |
| 18 | +import java.io.IOException; |
| 19 | +import java.nio.charset.StandardCharsets; |
| 20 | +import java.text.SimpleDateFormat; |
| 21 | +import java.util.*; |
| 22 | + |
| 23 | +import static io.github.qwerty770.advhelper.AdvancementHelper.version; |
| 24 | +import static io.github.qwerty770.advhelper.AdvancementTool.*; |
| 25 | + |
| 26 | +public class AdvancementCommand { |
| 27 | + @Environment(EnvType.CLIENT) |
| 28 | + public static void register(CommandDispatcher<FabricClientCommandSource> dispatcher) { |
| 29 | + dispatcher.register(ClientCommandManager.literal("advhelper") |
| 30 | + .executes(context -> { |
| 31 | + context.getSource().sendFeedback(Component.literal("Advancement Helper " + version + " running!")); |
| 32 | + return 0; |
| 33 | + }) |
| 34 | + .then(ClientCommandManager.literal("export").executes(context -> { |
| 35 | + exportAllAdvancements(context); |
| 36 | + return 1; |
| 37 | + }).then(ClientCommandManager.argument("namespace", StringArgumentType.string()).executes(context -> { |
| 38 | + exportAdvancements(context.getArgument("namespace", String.class), context); |
| 39 | + return 1; |
| 40 | + }))) |
| 41 | + .then(ClientCommandManager.literal("progress").executes(context -> { |
| 42 | + exportAllProgress(context); |
| 43 | + return 1; |
| 44 | + }).then(ClientCommandManager.argument("namespace", StringArgumentType.string()).executes(context -> { |
| 45 | + exportProgress(context.getArgument("namespace", String.class), context); |
| 46 | + return 1; |
| 47 | + }))) |
| 48 | + ); |
| 49 | + } |
| 50 | + |
| 51 | + private static void exportAdvancements(String namespace, CommandContext<FabricClientCommandSource> context) { |
| 52 | + AdvancementTree tree = getAdvancementTree(); |
| 53 | + List<AdvancementHolder> advancements = getAdvancements(); |
| 54 | + try { |
| 55 | + JsonObject json = new JsonObject(); |
| 56 | + for (AdvancementHolder adv : advancements){ |
| 57 | + if (!adv.id().getNamespace().equals(namespace)) continue; |
| 58 | + json.add(adv.id().toString(), getJson(adv, tree)); |
| 59 | + } |
| 60 | + FileWriter fileWriter = createFile(namespace, "adv"); |
| 61 | + writeJson(fileWriter, json); |
| 62 | + fileWriter.close(); |
| 63 | + AdvancementHelper.LOGGER.info("Advancement Helper exported the advancements of namespace {}", namespace); |
| 64 | + context.getSource().sendFeedback(Component.translatable("commands.advhelper.export.success")); |
| 65 | + } |
| 66 | + catch (IOException exception){ |
| 67 | + sendFailure(exception, context); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + private static void exportAllAdvancements(CommandContext<FabricClientCommandSource> context) { |
| 72 | + AdvancementTree tree = getAdvancementTree(); |
| 73 | + List<AdvancementHolder> advancements = getAdvancements(); |
| 74 | + Map<String, JsonObject> jsonMap = new HashMap<>(); |
| 75 | + Map<String, FileWriter> fileWriterMap = new HashMap<>(); |
| 76 | + try { |
| 77 | + for (AdvancementHolder adv : advancements){ |
| 78 | + String ns = adv.id().getNamespace(); |
| 79 | + if (!fileWriterMap.containsKey(ns)){ |
| 80 | + FileWriter writer = createFile(ns, "adv"); |
| 81 | + fileWriterMap.put(ns, writer); |
| 82 | + } |
| 83 | + if (!jsonMap.containsKey(ns)){ |
| 84 | + jsonMap.put(ns, new JsonObject()); |
| 85 | + } |
| 86 | + jsonMap.get(ns).add(adv.id().toString(), getJson(adv, tree)); |
| 87 | + } |
| 88 | + for (String ns : jsonMap.keySet()){ |
| 89 | + writeJson(fileWriterMap.get(ns), jsonMap.get(ns)); |
| 90 | + } |
| 91 | + for (FileWriter writer : fileWriterMap.values()){ |
| 92 | + writer.close(); |
| 93 | + } |
| 94 | + AdvancementHelper.LOGGER.info("Advancement Helper exported {} advancements", advancements.size()); |
| 95 | + context.getSource().sendFeedback(Component.translatable("commands.advhelper.export.success")); |
| 96 | + } |
| 97 | + catch (IOException exception){ |
| 98 | + sendFailure(exception, context); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + private static void exportProgress(String namespace, CommandContext<FabricClientCommandSource> context) { |
| 103 | + Map<AdvancementHolder, AdvancementProgress> progress = getProgress(); |
| 104 | + try { |
| 105 | + JsonObject json = new JsonObject(); |
| 106 | + for (AdvancementHolder adv : progress.keySet()){ |
| 107 | + if (!adv.id().getNamespace().equals(namespace)) continue; |
| 108 | + json.add(adv.id().toString(), getJson(progress.get(adv))); |
| 109 | + } |
| 110 | + FileWriter fileWriter = createFile(namespace, "progress"); |
| 111 | + writeJson(fileWriter, json); |
| 112 | + fileWriter.close(); |
| 113 | + AdvancementHelper.LOGGER.info("Advancement Helper exported the advancements' progress of namespace {}", namespace); |
| 114 | + context.getSource().sendFeedback(Component.translatable("commands.advhelper.export.success")); |
| 115 | + } |
| 116 | + catch (IOException exception){ |
| 117 | + sendFailure(exception, context); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + private static void exportAllProgress(CommandContext<FabricClientCommandSource> context) { |
| 122 | + Map<AdvancementHolder, AdvancementProgress> progress = getProgress(); |
| 123 | + Map<String, JsonObject> jsonMap = new HashMap<>(); |
| 124 | + Map<String, FileWriter> fileWriterMap = new HashMap<>(); |
| 125 | + try { |
| 126 | + for (AdvancementHolder adv : progress.keySet()){ |
| 127 | + String ns = adv.id().getNamespace(); |
| 128 | + if (!fileWriterMap.containsKey(ns)){ |
| 129 | + FileWriter writer = createFile(ns, "progress"); |
| 130 | + fileWriterMap.put(ns, writer); |
| 131 | + } |
| 132 | + if (!jsonMap.containsKey(ns)){ |
| 133 | + jsonMap.put(ns, new JsonObject()); |
| 134 | + } |
| 135 | + jsonMap.get(ns).add(adv.id().toString(), getJson(progress.get(adv))); |
| 136 | + } |
| 137 | + for (String ns : jsonMap.keySet()){ |
| 138 | + writeJson(fileWriterMap.get(ns), jsonMap.get(ns)); |
| 139 | + } |
| 140 | + for (FileWriter writer : fileWriterMap.values()){ |
| 141 | + writer.close(); |
| 142 | + } |
| 143 | + AdvancementHelper.LOGGER.info("Advancement Helper exported {} advancements' progress", progress.size()); |
| 144 | + context.getSource().sendFeedback(Component.translatable("commands.advhelper.export.success")); |
| 145 | + } |
| 146 | + catch (IOException exception){ |
| 147 | + sendFailure(exception, context); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + private static FileWriter createFile(String namespace, String type) throws IOException { |
| 152 | + File file = new File("export/" + namespace + "-" + type + "-" + |
| 153 | + new SimpleDateFormat("yyyy-MM-dd-HHmmss").format(new Date()) + ".json"); |
| 154 | + if (!file.getParentFile().exists()) { |
| 155 | + if(file.getParentFile().mkdirs()){ |
| 156 | + AdvancementHelper.LOGGER.debug("Created path: {}", file.getParentFile().getPath()); |
| 157 | + } |
| 158 | + } |
| 159 | + if (!file.createNewFile()) { |
| 160 | + AdvancementHelper.LOGGER.warn("File already exists: {}.", file.getName()); |
| 161 | + } |
| 162 | + return new FileWriter(file, StandardCharsets.UTF_8); |
| 163 | + } |
| 164 | + |
| 165 | + private static void sendFailure(IOException exception, CommandContext<FabricClientCommandSource> context){ |
| 166 | + AdvancementHelper.LOGGER.error("Failed to export to a file"); |
| 167 | + AdvancementHelper.LOGGER.error(exception.getMessage()); |
| 168 | + context.getSource().sendFeedback(Component.translatable("commands.advhelper.export.fail")); |
| 169 | + } |
| 170 | + |
| 171 | + private static void writeJson(FileWriter fileWriter, JsonObject jsonObject) throws IOException { |
| 172 | + JsonWriter jsonWriter = new JsonWriter(fileWriter); |
| 173 | + jsonWriter.setLenient(true); |
| 174 | + jsonWriter.setIndent(" "); |
| 175 | + Streams.write(jsonObject, jsonWriter); |
| 176 | + } |
| 177 | +} |
0 commit comments