Refactoring

This commit is contained in:
filoghost 2020-06-26 10:42:03 +02:00
parent e08bca7908
commit f7fc2f2baa

View File

@ -155,52 +155,30 @@ public class ChestCommands extends JavaPlugin {
getLogger().log(Level.SEVERE, "Encountered errors while running run automatic configuration upgrades. Some configuration files or menus may require manual updates.", e); getLogger().log(Level.SEVERE, "Encountered errors while running run automatic configuration upgrades. Some configuration files or menus may require manual updates.", e);
} }
try {
PluginConfig settingsYaml = getSettingsConfig(); PluginConfig settingsYaml = getSettingsConfig();
try {
settingsYaml.load(); settingsYaml.load();
settings.load(settingsYaml); settings.load(settingsYaml);
} catch (IOException e) { } catch (Throwable t) {
e.printStackTrace(); logConfigLoadException(settingsYaml, t);
getLogger().warning("I/O error while using the configuration. Default values will be used.");
} catch (InvalidConfigurationException e) {
e.printStackTrace();
getLogger().warning("The config.yml was not a valid YAML, please look at the error above. Default values will be used.");
} catch (Exception e) {
e.printStackTrace();
getLogger().warning("Unhandled error while reading the values for the configuration! Please inform the developer.");
} }
try {
PluginConfig langYaml = getLangConfig(); PluginConfig langYaml = getLangConfig();
try {
langYaml.load(); langYaml.load();
lang.load(langYaml); lang.load(langYaml);
} catch (IOException e) { } catch (Throwable t) {
e.printStackTrace(); logConfigLoadException(langYaml, t);
getLogger().warning("I/O error while using the language file. Default values will be used.");
} catch (InvalidConfigurationException e) {
e.printStackTrace();
getLogger().warning("The lang.yml was not a valid YAML, please look at the error above. Default values will be used.");
} catch (Exception e) {
e.printStackTrace();
getLogger().warning("Unhandled error while reading the values for the configuration! Please inform the developer.");
} }
try {
PluginConfig placeholdersYaml = getPlaceholdersConfig(); PluginConfig placeholdersYaml = getPlaceholdersConfig();
try {
placeholdersYaml.load(); placeholdersYaml.load();
placeholders.load(placeholdersYaml, errors); placeholders.load(placeholdersYaml, errors);
} catch (IOException e) { } catch (Throwable t) {
e.printStackTrace(); logConfigLoadException(placeholdersYaml, t);
getLogger().warning("I/O error while using the placeholders file. Default values will be used.");
} catch (InvalidConfigurationException e) {
e.printStackTrace();
getLogger().warning("The lang.yml was not a valid YAML, please look at the error above. Default values will be used.");
} catch (Exception e) {
e.printStackTrace();
getLogger().warning("Unhandled error while reading the values for the configuration! Please inform the developer.");
} }
// Load the menus // Load the menus
File menusFolder = getMenusFolder(); File menusFolder = getMenusFolder();
@ -214,13 +192,8 @@ public class ChestCommands extends JavaPlugin {
for (PluginConfig menuConfig : menusList) { for (PluginConfig menuConfig : menusList) {
try { try {
menuConfig.load(); menuConfig.load();
} catch (IOException e) { } catch (Throwable t) {
e.printStackTrace(); logConfigLoadException(menuConfig, t);
errors.addError("I/O error while loading the menu \"" + menuConfig.getFileName() + "\". Is the file in use?");
continue;
} catch (InvalidConfigurationException e) {
e.printStackTrace();
errors.addError("Invalid YAML configuration for the menu \"" + menuConfig.getFileName() + "\". Please look at the error above, or use an online YAML parser (google is your friend).");
continue; continue;
} }
@ -244,6 +217,18 @@ public class ChestCommands extends JavaPlugin {
return errors; return errors;
} }
private void logConfigLoadException(PluginConfig config, Throwable t) {
t.printStackTrace();
if (t instanceof IOException) {
getLogger().warning("Error while reading the file \"" + config.getFileName() + "\". Default values will be used.");
} else if (t instanceof InvalidConfigurationException) {
getLogger().warning("Invalid YAML syntax in the file \"" + config.getFileName() + "\", please look at the error above. Default values will be used.");
} else {
getLogger().warning("Unhandled error while parsing the file \"" + config.getFileName() + "\". Please inform the developer.");
}
}
public PluginConfig getLangConfig() { public PluginConfig getLangConfig() {
return new PluginConfig(this, "lang.yml"); return new PluginConfig(this, "lang.yml");
} }