Skip to content

Commit

Permalink
fix language loader
Browse files Browse the repository at this point in the history
  • Loading branch information
xGinko committed Dec 11, 2023
1 parent d334c7e commit 02378fa
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 22 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>me.xGinko</groupId>
<artifactId>BetterWorldStats</artifactId>
<version>1.7.2</version>
<version>1.8.0</version>
<packaging>jar</packaging>

<name>BetterWorldStats</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
Expand Down
9 changes: 3 additions & 6 deletions src/main/java/me/xGinko/betterworldstats/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
25 changes: 13 additions & 12 deletions src/main/java/me/xGinko/betterworldstats/config/LanguageCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,28 @@ public class LanguageCache {
public final List<String> 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 {
Expand All @@ -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));
Expand Down

0 comments on commit 02378fa

Please sign in to comment.