|
1 | 1 | package org.localmc.tools.ftbqkeys; |
2 | 2 |
|
| 3 | +import com.google.common.collect.Lists; |
3 | 4 | import com.google.gson.Gson; |
4 | 5 | import com.google.gson.GsonBuilder; |
| 6 | +import com.mojang.brigadier.arguments.StringArgumentType; |
| 7 | +import com.mojang.brigadier.tree.ArgumentCommandNode; |
| 8 | +import com.mojang.brigadier.tree.LiteralCommandNode; |
| 9 | +import com.mojang.brigadier.tree.RootCommandNode; |
5 | 10 | import dev.architectury.event.events.common.CommandRegistrationEvent; |
6 | 11 | import dev.architectury.platform.Platform; |
| 12 | +import dev.ftb.mods.ftbquests.api.FTBQuestsAPI; |
| 13 | +import dev.ftb.mods.ftbquests.quest.*; |
| 14 | +import dev.ftb.mods.ftbquests.quest.loot.RewardTable; |
| 15 | +import dev.ftb.mods.ftbquests.quest.reward.Reward; |
| 16 | +import dev.ftb.mods.ftbquests.quest.task.Task; |
| 17 | +import net.minecraft.client.MinecraftClient; |
| 18 | +import net.minecraft.command.CommandSource; |
| 19 | +import net.minecraft.server.command.CommandManager; |
| 20 | +import net.minecraft.server.command.ServerCommandSource; |
| 21 | +import net.minecraft.text.Text; |
7 | 22 | import org.apache.commons.io.FileUtils; |
8 | | -import org.localmc.tools.ftbqkeys.command.FTBQKeysCommand; |
| 23 | +import org.localmc.tools.ftbqkeys.mixin.BaseQuestFileAccessor; |
| 24 | +import org.localmc.tools.ftbqkeys.mixin.ChapterImageMixin; |
9 | 25 |
|
10 | 26 | import java.io.File; |
11 | 27 | import java.io.IOException; |
12 | 28 | import java.nio.charset.StandardCharsets; |
13 | 29 | import java.nio.file.Path; |
14 | | -import java.util.Locale; |
15 | | -import java.util.TreeMap; |
| 30 | +import java.util.*; |
| 31 | +import java.util.function.Function; |
| 32 | +import java.util.regex.Matcher; |
| 33 | +import java.util.regex.Pattern; |
16 | 34 |
|
17 | 35 | public class FTBQKeysMod { |
18 | 36 | public static final String MODID = "ftbqkeys"; |
19 | 37 | public static final Path gameDir = Platform.getGameFolder(); |
20 | 38 | public static final Path configDir = Platform.getConfigFolder(); |
21 | 39 | public static final Gson gson = new GsonBuilder().setPrettyPrinting().create(); |
22 | 40 |
|
| 41 | + // https://github.com/shedaniel/RoughlyEnoughItems/blob/21d144a7b605169578ba8e1dc1663d1ab042660d/api/src/main/java/me/shedaniel/rei/api/client/search/method/InputMethod.java#L60C42-L60C42 |
| 42 | + private static List<String> getLocales() { |
| 43 | + return map(MinecraftClient.getInstance().getLanguageManager().getAllLanguages().entrySet(), Map.Entry::getKey); |
| 44 | + } |
| 45 | + |
| 46 | + // https://github.com/shedaniel/RoughlyEnoughItems/blob/21d144a7b605169578ba8e1dc1663d1ab042660d/api/src/main/java/me/shedaniel/rei/api/common/util/CollectionUtils.java#L125 |
| 47 | + private static <T, R> List<R> map(Collection<T> list, Function<T, R> function) { |
| 48 | + List<R> l = new ArrayList<>(list.size() + 1); |
| 49 | + for (T t : list) { |
| 50 | + l.add(function.apply(t)); |
| 51 | + } |
| 52 | + return l; |
| 53 | + } |
| 54 | + |
23 | 55 | public static void saveLang(TreeMap<String, String> transKeys, String lang, File parent) throws IOException { |
24 | | - File fe = new File(parent, lang.toLowerCase(Locale.ROOT) + ".json"); |
25 | | - FileUtils.write(fe, FTBQKeysMod.gson.toJson(transKeys), StandardCharsets.UTF_8); |
| 56 | + File langFile = new File(parent, lang.toLowerCase(Locale.ROOT) + ".json"); |
| 57 | + FileUtils.write(langFile, FTBQKeysMod.gson.toJson(transKeys), StandardCharsets.UTF_8); |
26 | 58 | } |
27 | 59 |
|
28 | 60 | public static void init() { |
29 | | - CommandRegistrationEvent.EVENT.register(FTBQKeysCommand::serverRegisterCommandsEvent); |
| 61 | + CommandRegistrationEvent.EVENT.register((dispatcher, registry, selection) -> { |
| 62 | + RootCommandNode<ServerCommandSource> rootCommandNode = dispatcher.getRoot(); |
| 63 | + LiteralCommandNode<ServerCommandSource> commandNode = CommandManager.literal("ftbqkey").executes(context -> 0).build(); |
| 64 | + |
| 65 | + ArgumentCommandNode<ServerCommandSource, String> argumentCommandNode = CommandManager.argument("lang", StringArgumentType.word()).suggests((commandContext, suggestionsBuilder) -> CommandSource.suggestMatching(getLocales().toArray(new String[0]), suggestionsBuilder)).executes(context -> { |
| 66 | + try { |
| 67 | + File parent = new File(FTBQKeysMod.gameDir.toFile(), "ftbqkeys"); |
| 68 | + File transFiles = new File(parent, "export-lang/"); |
| 69 | + File questsFolder = new File(FTBQKeysMod.configDir.toFile(), "ftbquests/"); |
| 70 | + |
| 71 | + if (questsFolder.exists()) { |
| 72 | + File backup = new File(parent, "backup/ftbquests"); |
| 73 | + FileUtils.copyDirectory(questsFolder, backup); |
| 74 | + } |
| 75 | + |
| 76 | + TreeMap<String, String> transKeys = new TreeMap<>(); |
| 77 | + BaseQuestFile file = FTBQuestsAPI.api().getQuestFile(false); |
| 78 | + |
| 79 | + for (int i = 0; i < file.getRewardTables().size(); i++) { |
| 80 | + RewardTable table = file.getRewardTables().get(i); |
| 81 | + |
| 82 | + transKeys.put("loot_table." + (i + 1), table.getRawTitle()); |
| 83 | + table.getRawTitle().compareTo("{" + "loot_table." + (i + 1) + "}"); |
| 84 | + } |
| 85 | + |
| 86 | + for (int i = 0; i < ((BaseQuestFileAccessor) file).getChapterGroups().size(); i++) { |
| 87 | + ChapterGroup chapterGroup = ((BaseQuestFileAccessor) file).getChapterGroups().get(i); |
| 88 | + |
| 89 | + if (!chapterGroup.getRawTitle().isBlank()) { |
| 90 | + transKeys.put("category." + (i + 1), chapterGroup.getRawTitle()); |
| 91 | + chapterGroup.getRawTitle().compareTo("{" + "category." + (i + 1) + "}"); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + for (int i = 0; i < file.getAllChapters().size(); i++) { |
| 96 | + Chapter chapter = file.getAllChapters().get(i); |
| 97 | + |
| 98 | + String prefix = "chapter." + (i + 1); |
| 99 | + |
| 100 | + if (!chapter.getRawTitle().isBlank()) { |
| 101 | + transKeys.put(prefix + ".title", chapter.getRawTitle()); |
| 102 | + chapter.getRawTitle().compareTo("{" + prefix + ".title" + "}"); |
| 103 | + } |
| 104 | + |
| 105 | + if (chapter.getRawSubtitle().size() > 0) { |
| 106 | + transKeys.put(prefix + ".subtitle", String.join("\n", chapter.getRawTitle())); |
| 107 | + chapter.getRawSubtitle().clear(); |
| 108 | + chapter.getRawSubtitle().add("{" + prefix + ".subtitle" + "}"); |
| 109 | + } |
| 110 | + |
| 111 | + |
| 112 | + for (int i1 = 0; i1 < chapter.images().toList().size(); i1++) { |
| 113 | + ChapterImage chapterImage = chapter.images().toList().get(i1); |
| 114 | + List<String> hover = new ChapterImageMixin().getHovers(); |
| 115 | + |
| 116 | + if (!hover.isEmpty()) { |
| 117 | + transKeys.put(prefix + ".image." + (i1 + 1), String.join("\n", hover)); |
| 118 | + hover.clear(); |
| 119 | + hover.add("{" + prefix + ".image." + (i1 + 1) + "}"); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + for (int i1 = 0; i1 < chapter.getQuests().size(); i1++) { |
| 124 | + Quest quest = chapter.getQuests().get(i1); |
| 125 | + |
| 126 | + if (!quest.getRawTitle().isBlank()) { |
| 127 | + transKeys.put(prefix + ".quest." + (i1 + 1) + ".title", quest.getRawTitle()); |
| 128 | + quest.getRawTitle().compareTo("{" + prefix + ".quest." + (i1 + 1) + ".title}"); |
| 129 | + } |
| 130 | + |
| 131 | + if (!quest.getRawSubtitle().isBlank()) { |
| 132 | + transKeys.put(prefix + ".quest." + (i1 + 1) + ".subtitle", quest.getRawTitle()); |
| 133 | + quest.getRawSubtitle().compareTo("{" + prefix + ".quest." + (i1 + 1) + ".subtitle" + "}"); |
| 134 | + } |
| 135 | + |
| 136 | + if (quest.getRawDescription().size() > 0) { |
| 137 | + List<String> descList = Lists.newArrayList(); |
| 138 | + |
| 139 | + StringJoiner joiner = new StringJoiner("\n"); |
| 140 | + int num = 1; |
| 141 | + |
| 142 | + for (int i2 = 0; i2 < quest.getRawDescription().size(); i2++) { |
| 143 | + String desc = quest.getRawDescription().get(i2); |
| 144 | + |
| 145 | + final String regex = "\\{image:.*?}"; |
| 146 | + |
| 147 | + if (desc.contains("{image:")) { |
| 148 | + if (!joiner.toString().isBlank()) { |
| 149 | + transKeys.put(prefix + ".quest." + (i1 + 1) + ".description." + num, joiner.toString()); |
| 150 | + descList.add("{" + prefix + ".quest." + (i1 + 1) + ".description." + num + "}"); |
| 151 | + joiner = new StringJoiner("\n"); |
| 152 | + num++; |
| 153 | + } |
| 154 | + |
| 155 | + final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE); |
| 156 | + final Matcher matcher = pattern.matcher(desc); |
| 157 | + |
| 158 | + while (matcher.find()) { |
| 159 | + desc = desc.replace(matcher.group(0), ""); |
| 160 | + descList.add(matcher.group(0)); |
| 161 | + } |
| 162 | + } else { |
| 163 | + if (desc.isBlank()) { |
| 164 | + joiner.add("\n"); |
| 165 | + } else { |
| 166 | + joiner.add(desc); |
| 167 | + } |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + if (!joiner.toString().isBlank()) { |
| 172 | + transKeys.put(prefix + ".quest." + (i1 + 1) + ".description." + num, joiner.toString()); |
| 173 | + descList.add("{" + prefix + ".quest." + (i1 + 1) + ".description." + num + "}"); |
| 174 | + } |
| 175 | + |
| 176 | + quest.getRawDescription().clear(); |
| 177 | + quest.getRawDescription().addAll(descList); |
| 178 | + } |
| 179 | + |
| 180 | + for (int i2 = 0; i2 < quest.getTasks().size(); i2++) { |
| 181 | + Task task = quest.getQuestFile().getTask(i2); |
| 182 | + |
| 183 | + if (!task.getRawTitle().isBlank()) { |
| 184 | + transKeys.put(prefix + ".quest." + (i1 + 1) + ".task." + (i2 + 1) + ".title", task.getRawTitle()); |
| 185 | + task.getRawTitle().compareTo("{" + prefix + ".quest." + (i1 + 1) + ".task." + (i2 + 1) + ".title}"); |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + for (int i2 = 0; i2 < quest.getRewards().size(); i2++) { |
| 190 | + Reward reward = quest.getQuestFile().getReward(i2); |
| 191 | + |
| 192 | + if (!reward.getRawTitle().isBlank()) { |
| 193 | + transKeys.put(prefix + ".quest." + (i1 + 1) + ".reward." + (i2 + 1) + ".title", reward.getRawTitle()); |
| 194 | + reward.getRawTitle().compareTo("{" + prefix + ".quest." + (i1 + 1) + ".reward." + (i2 + 1) + ".title}"); |
| 195 | + } |
| 196 | + } |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | + File output = new File(parent, "config/ftbquests"); |
| 201 | + |
| 202 | + file.writeDataFull(output.toPath()); |
| 203 | + |
| 204 | + String lang = context.getArgument("lang", String.class); |
| 205 | + saveLang(transKeys, lang, transFiles); |
| 206 | + |
| 207 | + if (!lang.equalsIgnoreCase("en_us")) { |
| 208 | + saveLang(transKeys, "en_us", transFiles); |
| 209 | + } |
| 210 | + |
| 211 | + context.getSource().sendMessage(Text.translatable("command.ftbqkeys.message", parent.getAbsolutePath())); |
| 212 | + |
| 213 | + } catch (Exception e) { |
| 214 | + e.printStackTrace(); |
| 215 | + } |
| 216 | + |
| 217 | + return 1; |
| 218 | + }).build(); |
| 219 | + |
| 220 | + rootCommandNode.addChild(commandNode); |
| 221 | + commandNode.addChild(argumentCommandNode); |
| 222 | + }); |
30 | 223 | } |
31 | 224 | } |
0 commit comments