package me.xginko.villageroptimizer.config; import io.github.thatsmusic99.configurationmaster.api.ConfigFile; import me.xginko.villageroptimizer.VillagerOptimizer; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.minimessage.MiniMessage; import org.jetbrains.annotations.NotNull; import java.io.File; import java.util.List; public class LanguageCache { private final @NotNull ConfigFile lang; private final @NotNull MiniMessage miniMessage; public final @NotNull Component no_permission; public final @NotNull List nametag_optimize_success, nametag_on_optimize_cooldown, nametag_unoptimize_success, block_optimize_success, block_on_optimize_cooldown, block_unoptimize_success, workstation_optimize_success, workstation_on_optimize_cooldown, workstation_unoptimize_success, command_optimize_success, command_radius_limit_exceed, command_optimize_fail, command_unoptimize_success, command_specify_radius, command_radius_invalid, trades_restocked, optimize_for_trading, villager_leveling_up; public LanguageCache(String lang) throws Exception { this.lang = loadLang(new File(VillagerOptimizer.getInstance().getDataFolder() + File.separator + "lang", lang + ".yml")); this.miniMessage = MiniMessage.miniMessage(); // General this.no_permission = getTranslation("messages.no-permission", "You don't have permission to use this command."); this.trades_restocked = getListTranslation("messages.trades-restocked", List.of("All trades have been restocked! Next restock in %time%")); this.optimize_for_trading = getListTranslation("messages.optimize-to-trade", List.of("You need to optimize this villager before you can trade with it.")); this.villager_leveling_up = getListTranslation("messages.villager-leveling-up", List.of("Villager is currently leveling up! You can use the villager again in %time%.")); // Nametag this.nametag_optimize_success = getListTranslation("messages.nametag.optimize-success", List.of("Successfully optimized villager by using a nametag.")); this.nametag_on_optimize_cooldown = getListTranslation("messages.nametag.optimize-on-cooldown", List.of("You need to wait %time% until you can optimize this villager again.")); this.nametag_unoptimize_success = getListTranslation("messages.nametag.unoptimize-success", List.of("Successfully unoptimized villager by using a nametag.")); // Block this.block_optimize_success = getListTranslation("messages.block.optimize-success", List.of("%villagertype% villager successfully optimized using block %blocktype%.")); this.block_on_optimize_cooldown = getListTranslation("messages.block.optimize-on-cooldown", List.of("You need to wait %time% until you can optimize this villager again.")); this.block_unoptimize_success = getListTranslation("messages.block.unoptimize-success", List.of("Successfully unoptimized %villagertype% villager by removing %blocktype%.")); // Workstation this.workstation_optimize_success = getListTranslation("messages.workstation.optimize-success", List.of("%villagertype% villager successfully optimized using workstation %workstation%.")); this.workstation_on_optimize_cooldown = getListTranslation("messages.workstation.optimize-on-cooldown", List.of("You need to wait %time% until you can optimize this villager again.")); this.workstation_unoptimize_success = getListTranslation("messages.workstation.unoptimize-success", List.of("Successfully unoptimized %villagertype% villager by removing workstation block %workstation%.")); // Command this.command_optimize_success = getListTranslation("messages.command.optimize-success", List.of("Successfully optimized %amount% villager(s) in a radius of %radius% blocks.")); this.command_radius_limit_exceed = getListTranslation("messages.command.radius-limit-exceed", List.of("The radius you entered exceeds the limit of %distance% blocks.")); this.command_optimize_fail = getListTranslation("messages.command.optimize-fail", List.of("%amount% villagers couldn't be optimized because they have recently been optimized.")); this.command_unoptimize_success = getListTranslation("messages.command.unoptimize-success", List.of("Successfully unoptimized %amount% villager(s) in a radius of %radius% blocks.")); this.command_specify_radius = getListTranslation("messages.command.specify-radius", List.of("Please specify a radius.")); this.command_radius_invalid = getListTranslation("messages.command.radius-invalid", List.of("The radius you entered is not a valid number. Try again.")); saveLang(); } private ConfigFile loadLang(File ymlFile) throws Exception { File parent = new File(ymlFile.getParent()); if (!parent.exists()) if (!parent.mkdir()) VillagerOptimizer.getLog().severe("Unable to create lang directory."); if (!ymlFile.exists()) ymlFile.createNewFile(); // Result can be ignored because this method only returns false if the file already exists return ConfigFile.loadConfig(ymlFile); } private void saveLang() { try { lang.save(); } catch (Exception e) { VillagerOptimizer.getLog().severe("Failed to save language file: "+ lang.getFile().getName() +" - " + e.getLocalizedMessage()); } } public @NotNull Component getTranslation(@NotNull String path, @NotNull String defaultTranslation) { lang.addDefault(path, defaultTranslation); return miniMessage.deserialize(lang.getString(path, defaultTranslation)); } public @NotNull Component getTranslation(@NotNull String path, @NotNull String defaultTranslation, @NotNull String comment) { lang.addDefault(path, defaultTranslation, comment); return miniMessage.deserialize(lang.getString(path, defaultTranslation)); } public @NotNull List getListTranslation(@NotNull String path, @NotNull List defaultTranslation) { lang.addDefault(path, defaultTranslation); return lang.getStringList(path).stream().map(miniMessage::deserialize).toList(); } public @NotNull List getListTranslation(@NotNull String path, @NotNull List defaultTranslation, @NotNull String comment) { lang.addDefault(path, defaultTranslation, comment); return lang.getStringList(path).stream().map(miniMessage::deserialize).toList(); } }