Refactor: Moved code related to loading the config into Settings class.
parent
73173d4525
commit
d50bfaa4d1
|
@ -10,7 +10,6 @@ import java.util.UUID;
|
|||
import org.bstats.bukkit.Metrics;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.configuration.Configuration;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.HandlerList;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
@ -165,34 +164,6 @@ public class SKShopkeepersPlugin extends JavaPlugin implements ShopkeepersPlugin
|
|||
return (NMSManager.getProvider() != null);
|
||||
}
|
||||
|
||||
// Returns null on success, otherwise a severe issue prevented loading the config.
|
||||
private ConfigLoadException loadConfig() {
|
||||
Log.info("Loading config.");
|
||||
|
||||
// Save default config in case the config file doesn't exist:
|
||||
this.saveDefaultConfig();
|
||||
|
||||
// Load config:
|
||||
this.reloadConfig();
|
||||
Configuration config = this.getConfig();
|
||||
|
||||
// Load settings from config:
|
||||
boolean configChanged;
|
||||
try {
|
||||
configChanged = Settings.loadConfiguration(config);
|
||||
} catch (ConfigLoadException e) {
|
||||
// Config loading failed with a severe issue:
|
||||
return e;
|
||||
}
|
||||
|
||||
if (configChanged) {
|
||||
// If the config was modified (migrations, adding missing settings, ..), save it:
|
||||
// TODO Persist comments somehow.
|
||||
this.saveConfig();
|
||||
}
|
||||
return null; // Config loaded successfully
|
||||
}
|
||||
|
||||
private void registerDefaults() {
|
||||
Log.info("Registering defaults.");
|
||||
uiRegistry.registerAll(defaultUITypes.getAllUITypes());
|
||||
|
@ -228,7 +199,7 @@ public class SKShopkeepersPlugin extends JavaPlugin implements ShopkeepersPlugin
|
|||
}
|
||||
|
||||
// Load config:
|
||||
this.configLoadError = this.loadConfig();
|
||||
this.configLoadError = Settings.loadConfig(this);
|
||||
if (this.configLoadError != null) {
|
||||
return;
|
||||
}
|
||||
|
@ -276,7 +247,7 @@ public class SKShopkeepersPlugin extends JavaPlugin implements ShopkeepersPlugin
|
|||
|
||||
// Load config (if not already loaded during onLoad):
|
||||
if (!alreadySetup) {
|
||||
this.configLoadError = this.loadConfig();
|
||||
this.configLoadError = Settings.loadConfig(this);
|
||||
} else {
|
||||
Log.debug("Config already loaded.");
|
||||
}
|
||||
|
|
|
@ -23,6 +23,7 @@ import org.bukkit.configuration.Configuration;
|
|||
import org.bukkit.entity.EntityType;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
import com.nisovin.shopkeepers.api.ShopkeepersPlugin;
|
||||
import com.nisovin.shopkeepers.config.ConfigLoadException;
|
||||
|
@ -523,6 +524,34 @@ public class Settings {
|
|||
|
||||
///// LOADING
|
||||
|
||||
// Returns null on success, otherwise a severe issue prevented loading the config.
|
||||
public static ConfigLoadException loadConfig(Plugin plugin) {
|
||||
Log.info("Loading config.");
|
||||
|
||||
// Save default config in case the config file doesn't exist:
|
||||
plugin.saveDefaultConfig();
|
||||
|
||||
// Load config:
|
||||
plugin.reloadConfig();
|
||||
Configuration config = plugin.getConfig();
|
||||
|
||||
// Load settings from config:
|
||||
boolean configChanged = false;
|
||||
try {
|
||||
configChanged = Settings.loadConfiguration(config);
|
||||
} catch (ConfigLoadException e) {
|
||||
// Config loading failed with a severe issue:
|
||||
return e;
|
||||
}
|
||||
|
||||
if (configChanged) {
|
||||
// If the config was modified (migrations, adding missing settings, ..), save it:
|
||||
// TODO Persist comments somehow.
|
||||
plugin.saveConfig();
|
||||
}
|
||||
return null; // Config loaded successfully
|
||||
}
|
||||
|
||||
// These String / String list settings are exempt from color conversion:
|
||||
private static final Set<String> noColorConversionKeys = new HashSet<>(Arrays.asList(
|
||||
toConfigKey("debugOptions"), toConfigKey("fileEncoding"), toConfigKey("shopCreationItemSpawnEggEntityType"),
|
||||
|
|
Loading…
Reference in New Issue