From 02378fa582cb6b14d23c1b7945da654bcb5811a8 Mon Sep 17 00:00:00 2001 From: xGinko Date: Mon, 11 Dec 2023 23:24:45 +0100 Subject: [PATCH] fix language loader --- pom.xml | 2 +- .../betterworldstats/BetterWorldStats.java | 6 ++--- .../betterworldstats/config/Config.java | 9 +++---- .../config/LanguageCache.java | 25 ++++++++++--------- 4 files changed, 20 insertions(+), 22 deletions(-) diff --git a/pom.xml b/pom.xml index 26b43fa..534c12f 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ me.xGinko BetterWorldStats - 1.7.2 + 1.8.0 jar BetterWorldStats diff --git a/src/main/java/me/xGinko/betterworldstats/BetterWorldStats.java b/src/main/java/me/xGinko/betterworldstats/BetterWorldStats.java index 10f5b96..4495914 100644 --- a/src/main/java/me/xGinko/betterworldstats/BetterWorldStats.java +++ b/src/main/java/me/xGinko/betterworldstats/BetterWorldStats.java @@ -98,11 +98,11 @@ private void reloadConfiguration() { public void reloadLang() { languageCacheMap = new HashMap<>(); try { - File langDirectory = new File(instance.getDataFolder()+ "/lang"); + File langDirectory = new File(getDataFolder() + "/lang"); Files.createDirectories(langDirectory.toPath()); for (String fileName : getDefaultLanguageFiles()) { String localeString = fileName.substring(fileName.lastIndexOf('/') + 1, fileName.lastIndexOf('.')); - logger.info(String.format("Found language file for %s", localeString)); + logger.info("Found language file for " + localeString); LanguageCache langCache = new LanguageCache(localeString); languageCacheMap.put(localeString, langCache); } @@ -111,7 +111,7 @@ public void reloadLang() { Matcher langMatcher = langPattern.matcher(langFile.getName()); if (langMatcher.find()) { String localeString = langMatcher.group(1).toLowerCase(); - if(!languageCacheMap.containsKey(localeString)) { // make sure it wasn't a default file that we already loaded + if (!languageCacheMap.containsKey(localeString)) { // make sure it wasn't a default file that we already loaded logger.info(String.format("Found language file for %s", localeString)); LanguageCache langCache = new LanguageCache(localeString); languageCacheMap.put(localeString, langCache); diff --git a/src/main/java/me/xGinko/betterworldstats/config/Config.java b/src/main/java/me/xGinko/betterworldstats/config/Config.java index 3d4945d..25fec27 100644 --- a/src/main/java/me/xGinko/betterworldstats/config/Config.java +++ b/src/main/java/me/xGinko/betterworldstats/config/Config.java @@ -42,12 +42,9 @@ public Config() throws Exception { } private ConfigFile loadConfig(File ymlFile) throws Exception { - File parent = new File(ymlFile.getParent()); - if (!parent.exists()) - if (!parent.mkdir()) - BetterWorldStats.getLog().severe("Unable to create plugin config directory."); - if (!ymlFile.exists()) - ymlFile.createNewFile(); // Result can be ignored because this method only returns false if the file already exists + File parent = ymlFile.getParentFile(); + if (!parent.exists() && !parent.mkdir()) + BetterWorldStats.getLog().severe("Unable to create plugin directory."); return ConfigFile.loadConfig(ymlFile); } diff --git a/src/main/java/me/xGinko/betterworldstats/config/LanguageCache.java b/src/main/java/me/xGinko/betterworldstats/config/LanguageCache.java index beb51ca..c3ded2b 100644 --- a/src/main/java/me/xGinko/betterworldstats/config/LanguageCache.java +++ b/src/main/java/me/xGinko/betterworldstats/config/LanguageCache.java @@ -16,17 +16,28 @@ public class LanguageCache { public final List world_stats_message; public LanguageCache(String lang) throws Exception { - this.langFile = loadLang(new File(BetterWorldStats.getInstance().getDataFolder() + File.separator + "lang", lang + ".yml")); + BetterWorldStats plugin = BetterWorldStats.getInstance(); + File langYML = new File(plugin.getDataFolder() + File.separator + "lang", lang + ".yml"); + // Check if the lang folder has already been created + File parent = langYML.getParentFile(); + if (!parent.exists() && !parent.mkdir()) + BetterWorldStats.getLog().severe("Unable to create lang directory."); + // Check if the file already exists and save the one from the plugins resources folder if it does not + if (!langYML.exists()) + plugin.saveResource("lang/" + lang + ".yml", false); + // Finally load the lang file with configmaster + this.langFile = ConfigFile.loadConfig(langYML); langFile.addComment("Command Placeholders:\n" + "%size% | %players% | %years% | %months% | %days%"); this.world_stats_message = getStringListTranslation("stats-message", Arrays.asList( "&3-----------------------------------------------------", " &7The server has spawned &6%players% player(s)&7 at least once", - " &7The map is &6%years% year(s)&7, &6%month% month(s)&7 and &6%days% day(s)&7 old", + " &7The map is &6%years% year(s)&7, &6%months% month(s)&7 and &6%days% day(s)&7 old", " &7The world (with compression) is a total of &6%size% GB", "&3-----------------------------------------------------" )); + this.no_permission = getStringTranslation("no-permission", "You don't have permission to use this command."); try { @@ -36,16 +47,6 @@ public LanguageCache(String lang) throws Exception { } } - private ConfigFile loadLang(File ymlFile) throws Exception { - File parent = new File(ymlFile.getParent()); - if (!parent.exists()) - if (!parent.mkdir()) - BetterWorldStats.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 String getStringTranslation(String path, String defaultTranslation) { langFile.addDefault(path, defaultTranslation); return ChatColor.translateAlternateColorCodes('&', langFile.getString(path, defaultTranslation));