Added a better exception handling when loading locales

This commit is contained in:
Florian CUNY 2018-07-28 10:15:36 +02:00
parent 5fc4967e1b
commit 96c68957c5
2 changed files with 22 additions and 12 deletions

View File

@ -6,6 +6,7 @@ import java.util.Locale;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.inventory.ItemStack;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.util.ItemParser;
/**
@ -17,9 +18,9 @@ public class BSBLocale {
private YamlConfiguration config;
private ItemStack banner;
public BSBLocale(Locale locale, File file) {
public BSBLocale(Locale locale, YamlConfiguration config) {
this.locale = locale;
config = YamlConfiguration.loadConfiguration(file);
this.config = config;
// Load the banner from the configuration
banner = ItemParser.parse(config.getString("banner"));
@ -79,10 +80,9 @@ public class BSBLocale {
/**
* Merges a language YAML file to this locale
* @param language - language file
* @param toBeMerged the YamlConfiguration of the language file
*/
public void merge(File language) {
YamlConfiguration toBeMerged = YamlConfiguration.loadConfiguration(language);
public void merge(YamlConfiguration toBeMerged) {
for (String key : toBeMerged.getKeys(true)) {
if (!config.contains(key)) {
config.set(key, toBeMerged.get(key));
@ -93,5 +93,4 @@ public class BSBLocale {
public boolean contains(String reference) {
return config.contains(reference);
}
}

View File

@ -12,6 +12,7 @@ import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import org.bukkit.configuration.file.YamlConfiguration;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.api.localization.BSBLocale;
import us.tastybento.bskyblock.api.user.User;
@ -85,12 +86,22 @@ public class LocalesManager {
// Store all the locales available
for (File language : Objects.requireNonNull(localeDir.listFiles(ymlFilter))) {
Locale localeObject = Locale.forLanguageTag(language.getName().substring(0, language.getName().length() - 4));
try {
YamlConfiguration languageYaml = YamlConfiguration.loadConfiguration(language);
if (languages.containsKey(localeObject)) {
// Merge into current language
languages.get(localeObject).merge(language);
languages.get(localeObject).merge(languageYaml);
} else {
// New language
languages.put(localeObject, new BSBLocale(localeObject, language));
languages.put(localeObject, new BSBLocale(localeObject, languageYaml));
}
} catch (Exception e) {
BSkyBlock.getInstance().logError("Could not load '" + language.getName() + "' : " + e.getMessage()
+ " with the following cause '" + e.getCause() + "'." +
" The file has likely an invalid YML format or has been made unreadable during the process."
);
}
}
}