Skip to content

Commit

Permalink
Avoid exception on unpopulated lang files, fixes #2063
Browse files Browse the repository at this point in the history
  • Loading branch information
PikaMug committed Jan 28, 2023
1 parent e38e600 commit 43e9b6a
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions api/src/main/java/me/blackvein/quests/util/Lang.java
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ public static void init(final QuestsAPI plugin, String iso) throws InvalidConfig
+ File.separator + "strings.yml");
final File langFile_new = new File(plugin.getPluginDataFolder(), File.separator + "lang" + File.separator + iso
+ File.separator + "strings_new.yml");
final boolean exists_new = langFile_new.exists();
boolean exists_new = langFile_new.exists();
final LinkedHashMap<String, String> allStrings = new LinkedHashMap<>();
if (!(langFile.exists() && iso.split("-").length > 1)) {
if (defaultLang.isEmpty()) {
Expand All @@ -225,21 +225,22 @@ public static void init(final QuestsAPI plugin, String iso) throws InvalidConfig
allStrings.put(key, config.getString(key));
}
}
FileConfiguration config = null;
try {
FileConfiguration config;
if (langFile.length() > 4) {
config = YamlConfiguration
.loadConfiguration(new InputStreamReader(new FileInputStream(langFile), StandardCharsets.UTF_8));
} catch (Exception e) {
plugin.getPluginLogger().severe("Unable to load config for language " + iso);
e.printStackTrace();
}
if (config == null) {
return;
} else {
config = YamlConfiguration.loadConfiguration(new InputStreamReader(Objects
.requireNonNull(plugin.getPluginResource("strings.yml")), StandardCharsets.UTF_8));
}
FileConfiguration config_new = null;
if (exists_new) {
config_new = YamlConfiguration.loadConfiguration(new InputStreamReader(
new FileInputStream(langFile_new), StandardCharsets.UTF_8));
if (langFile_new.length() > 5) {
config_new = YamlConfiguration.loadConfiguration(new InputStreamReader(
new FileInputStream(langFile_new), StandardCharsets.UTF_8));
} else {
exists_new = false;
}
}
// Load user's lang file and determine new strings
for (final String key : config.getKeys(false)) {
Expand Down Expand Up @@ -308,7 +309,11 @@ public static void init(final QuestsAPI plugin, String iso) throws InvalidConfig
} else {
otherLang.put(iso, allStrings);
}
plugin.getPluginLogger().info("Loaded language " + iso + ". Translations via Crowdin");
if (langFile.length() > 4) {
plugin.getPluginLogger().info("Loaded language " + iso + ". Translations via Crowdin");
} else {
plugin.getPluginLogger().info("Failed to load language " + iso + " due to lack of translations");
}
}

private static class LangToken {
Expand Down

0 comments on commit 43e9b6a

Please sign in to comment.