Updates to config loading.

This commit is contained in:
GJ 2012-05-23 13:52:33 -04:00
parent 0fe90df01e
commit b88e076f27
12 changed files with 24 additions and 340 deletions

View File

@ -385,7 +385,7 @@ public class Config extends ConfigLoader {
} }
@Override @Override
protected void load() { public void load() {
if (!configFile.exists()) { if (!configFile.exists()) {
dataFolder.mkdir(); dataFolder.mkdir();
plugin.saveDefaultConfig(); plugin.saveDefaultConfig();

View File

@ -4,6 +4,7 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.mcMMO;
@ -18,12 +19,21 @@ public abstract class ConfigLoader {
this.plugin = plugin; this.plugin = plugin;
dataFolder = plugin.getDataFolder(); dataFolder = plugin.getDataFolder();
configFile = new File(dataFolder, File.separator + fileName); configFile = new File(dataFolder, File.separator + fileName);
config = YamlConfiguration.loadConfiguration(this.configFile);
} }
/** /**
* Load this config file. * Load this config file.
*/ */
protected abstract void load(); public void load() {
if (!configFile.exists()) {
dataFolder.mkdir();
saveConfig();
}
addDefaults();
loadKeys();
}
/** /**
* Save this config file. * Save this config file.
@ -32,8 +42,8 @@ public abstract class ConfigLoader {
try { try {
config.save(configFile); config.save(configFile);
} }
catch (IOException e) { catch (IOException ex) {
e.printStackTrace(); plugin.getLogger().severe("Could not save config to " + configFile + ex);
} }
} }
@ -41,7 +51,6 @@ public abstract class ConfigLoader {
* Add the defaults to this config file. * Add the defaults to this config file.
*/ */
protected void addDefaults() { protected void addDefaults() {
// Load from included config.yml
config.options().copyDefaults(true); config.options().copyDefaults(true);
saveConfig(); saveConfig();
} }

View File

@ -27,8 +27,10 @@ public class HiddenConfig extends ConfigLoader {
} }
@Override @Override
protected void load() { public void load() {
if(plugin.isInJar(fileName)) loadKeys(); if (plugin.getResource(fileName) != null) {
loadKeys();
}
} }
@Override @Override

View File

@ -22,7 +22,7 @@ public class RepairConfig extends ConfigLoader {
} }
@Override @Override
protected void load() { public void load() {
loadKeys(); loadKeys();
} }
@ -32,6 +32,7 @@ public class RepairConfig extends ConfigLoader {
ConfigurationSection section = config.getConfigurationSection("Repairables"); ConfigurationSection section = config.getConfigurationSection("Repairables");
Set<String> keys = section.getKeys(false); Set<String> keys = section.getKeys(false);
for (String key : keys) { for (String key : keys) {
// Validate all the things! // Validate all the things!
List<String> reason = new ArrayList<String>(); List<String> reason = new ArrayList<String>();

View File

@ -16,8 +16,8 @@ public class RepairConfigManager {
Pattern pattern = Pattern.compile("repair\\.(?:.+)\\.yml"); Pattern pattern = Pattern.compile("repair\\.(?:.+)\\.yml");
File dataFolder = plugin.getDataFolder(); File dataFolder = plugin.getDataFolder();
File vanilla = new File(dataFolder, "repair.vanilla.yml"); File vanilla = new File(dataFolder, "repair.vanilla.yml");
if (!vanilla.exists()) { if (!vanilla.exists()) {
plugin.saveResource("repair.vanilla.yml", false); plugin.saveResource("repair.vanilla.yml", false);
} }

View File

@ -18,11 +18,11 @@ public class SpoutConfig extends ConfigLoader {
public SpoutConfig(mcMMO plugin) { public SpoutConfig(mcMMO plugin) {
super(plugin, "spout.yml"); super(plugin, "spout.yml");
config = plugin.getSpoutConfig();
} }
public boolean spoutEnabled; public boolean spoutEnabled;
public boolean getShowPowerLevel() { return config.getBoolean("HUD.Show_Power_Level", true); } public boolean getShowPowerLevel() { return config.getBoolean("HUD.Show_Power_Level", true); }
public String getMenuKey() { return config.getString("Menu.Key", "KEY_M"); }
/* XP Bar */ /* XP Bar */
public boolean getXPBarEnabled() { return config.getBoolean("XP.Bar.Enabled", true); } public boolean getXPBarEnabled() { return config.getBoolean("XP.Bar.Enabled", true); }
@ -77,17 +77,6 @@ public class SpoutConfig extends ConfigLoader {
public double getRetroHUDFishingGreen() { return config.getDouble("HUD.Retro.Colors.Fishing.GREEN", 0.3); } public double getRetroHUDFishingGreen() { return config.getDouble("HUD.Retro.Colors.Fishing.GREEN", 0.3); }
public double getRetroHUDFishingBlue() { return config.getDouble("HUD.Retro.Colors.Fishing.BLUE", 0.75); } public double getRetroHUDFishingBlue() { return config.getDouble("HUD.Retro.Colors.Fishing.BLUE", 0.75); }
@Override
public void load() {
if (!configFile.exists()) {
dataFolder.mkdir();
plugin.saveSpoutConfig();
}
addDefaults();
loadKeys();
}
@Override @Override
protected void loadKeys() { protected void loadKeys() {
plugin.getLogger().info("Loading mcMMO spout.yml File..."); plugin.getLogger().info("Loading mcMMO spout.yml File...");

View File

@ -43,18 +43,6 @@ public class TreasuresConfig extends ConfigLoader{
private TreasuresConfig(mcMMO plugin) { private TreasuresConfig(mcMMO plugin) {
super(plugin, "treasures.yml"); super(plugin, "treasures.yml");
config = plugin.getTreasuresConfig();
}
@Override
protected void load() {
if (!configFile.exists()) {
dataFolder.mkdir();
plugin.saveTreasuresConfig();
}
addDefaults();
loadKeys();
} }
@Override @Override

View File

@ -37,18 +37,6 @@ public class CustomArmorConfig extends ModConfigLoader{
public CustomArmorConfig(mcMMO plugin) { public CustomArmorConfig(mcMMO plugin) {
super(plugin, "armor.yml"); super(plugin, "armor.yml");
config = plugin.getArmorConfig();
}
@Override
public void load() {
if (!configFile.exists()) {
dataFolder.mkdir();
plugin.saveArmorConfig();
}
addDefaults();
loadKeys();
} }
@Override @Override

View File

@ -36,18 +36,6 @@ public class CustomBlocksConfig extends ModConfigLoader{
public CustomBlocksConfig(mcMMO plugin) { public CustomBlocksConfig(mcMMO plugin) {
super(plugin, "blocks.yml"); super(plugin, "blocks.yml");
config = plugin.getBlocksConfig();
}
@Override
public void load() {
if (!configFile.exists()) {
dataFolder.mkdir();
plugin.saveBlocksConfig();
}
addDefaults();
loadKeys();
} }
@Override @Override

View File

@ -39,18 +39,6 @@ public class CustomToolsConfig extends ModConfigLoader {
private CustomToolsConfig(mcMMO plugin) { private CustomToolsConfig(mcMMO plugin) {
super(plugin, "tools.yml"); super(plugin, "tools.yml");
config = plugin.getToolsConfig();
}
@Override
public void load() {
if (!configFile.exists()) {
dataFolder.mkdir();
plugin.saveToolsConfig();
}
addDefaults();
loadKeys();
} }
@Override @Override

View File

@ -389,274 +389,4 @@ public class mcMMO extends JavaPlugin {
getCommand("mchud").setExecutor(new MchudCommand(this)); getCommand("mchud").setExecutor(new MchudCommand(this));
} }
/*
* Boilerplate Custom Config Stuff (Treasures)
*/
private FileConfiguration treasuresConfig = null;
private File treasuresConfigFile = null;
/**
* Reload the Treasures.yml file.
*/
public void reloadTreasuresConfig() {
if (treasuresConfigFile == null) {
treasuresConfigFile = new File(getDataFolder(), "treasures.yml");
}
treasuresConfig = YamlConfiguration.loadConfiguration(treasuresConfigFile);
if (isInJar("treasures.yml")) {
InputStream defConfigStream = getResource("treasures.yml");
YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
treasuresConfig.setDefaults(defConfig);
}
}
/**
* Get the Treasures config information.
*
* @return the configuration object for treasures.yml
*/
public FileConfiguration getTreasuresConfig() {
if (treasuresConfig == null) {
reloadTreasuresConfig();
}
return treasuresConfig;
}
/**
* Save the Treasures config informtion.
*/
public void saveTreasuresConfig() {
if (treasuresConfig == null || treasuresConfigFile == null) {
return;
}
try {
treasuresConfig.save(treasuresConfigFile);
}
catch (IOException ex) {
getLogger().severe("Could not save config to " + treasuresConfigFile + ex.toString());
}
}
/*
* Boilerplate Custom Config Stuff (Tools)
*/
private FileConfiguration toolsConfig = null;
private File toolsConfigFile = null;
/**
* Reload the Tools.yml file.
*/
public void reloadToolsConfig() {
if (toolsConfigFile == null) {
toolsConfigFile = new File(modDirectory, "tools.yml");
}
toolsConfig = YamlConfiguration.loadConfiguration(toolsConfigFile);
if (isInJar("tools.yml")) {
InputStream defConfigStream = getResource("tools.yml");
YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
toolsConfig.setDefaults(defConfig);
}
}
/**
* Get the Tools config information.
*
* @return the configuration object for tools.yml
*/
public FileConfiguration getToolsConfig() {
if (toolsConfig == null) {
reloadToolsConfig();
}
return toolsConfig;
}
/**
* Save the Tools config informtion.
*/
public void saveToolsConfig() {
if (toolsConfig == null || toolsConfigFile == null) {
return;
}
try {
toolsConfig.save(toolsConfigFile);
}
catch (IOException ex) {
getLogger().severe("Could not save config to " + toolsConfigFile + ex.toString());
}
}
/*
* Boilerplate Custom Config Stuff (Armor)
*/
private FileConfiguration armorConfig = null;
private File armorConfigFile = null;
/**
* Reload the Armor.yml file.
*/
public void reloadArmorConfig() {
if (armorConfigFile == null) {
armorConfigFile = new File(modDirectory, "armor.yml");
}
armorConfig = YamlConfiguration.loadConfiguration(armorConfigFile);
if (isInJar("armor.yml")) {
InputStream defConfigStream = getResource("armor.yml");
YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
armorConfig.setDefaults(defConfig);
}
}
/**
* Get the Armor config information.
*
* @return the configuration object for armor.yml
*/
public FileConfiguration getArmorConfig() {
if (armorConfig == null) {
reloadArmorConfig();
}
return armorConfig;
}
/**
* Save the Armor config informtion.
*/
public void saveArmorConfig() {
if (armorConfig == null || armorConfigFile == null) {
return;
}
try {
armorConfig.save(armorConfigFile);
}
catch (IOException ex) {
getLogger().severe("Could not save config to " + armorConfigFile + ex.toString());
}
}
/*
* Boilerplate Custom Config Stuff (Blocks)
*/
private FileConfiguration blocksConfig = null;
private File blocksConfigFile = null;
/**
* Reload the Blocks.yml file.
*/
public void reloadBlocksConfig() {
if (blocksConfigFile == null) {
blocksConfigFile = new File(modDirectory, "blocks.yml");
}
blocksConfig = YamlConfiguration.loadConfiguration(blocksConfigFile);
if (isInJar("blocks.yml")) {
InputStream defConfigStream = getResource("blocks.yml");
YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
blocksConfig.setDefaults(defConfig);
}
}
/**
* Get the Blocks config information.
*
* @return the configuration object for blocks.yml
*/
public FileConfiguration getBlocksConfig() {
if (blocksConfig == null) {
reloadBlocksConfig();
}
return blocksConfig;
}
/**
* Save the Blocks config informtion.
*/
public void saveBlocksConfig() {
if (blocksConfig == null || blocksConfigFile == null) {
return;
}
try {
blocksConfig.save(blocksConfigFile);
}
catch (IOException ex) {
getLogger().severe("Could not save config to " + blocksConfigFile + ex.toString());
}
}
/*
* Boilerplate Custom Config Stuff (Spout)
*/
private FileConfiguration spoutConfig = null;
private File spoutConfigFile = null;
/**
* Reload the Spout.yml file.
*/
public void reloadSpoutConfig() {
if (spoutConfigFile == null) {
spoutConfigFile = new File(modDirectory, "spout.yml");
}
spoutConfig = YamlConfiguration.loadConfiguration(spoutConfigFile);
if (isInJar("spout.yml")) {
InputStream defConfigStream = getResource("spout.yml");
YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
spoutConfig.setDefaults(defConfig);
}
}
/**
* Get the Spout config information.
*
* @return the configuration object for spout.yml
*/
public FileConfiguration getSpoutConfig() {
if (spoutConfig == null) {
reloadSpoutConfig();
}
return spoutConfig;
}
/**
* Save the Spout config informtion.
*/
public void saveSpoutConfig() {
if (spoutConfig == null || spoutConfigFile == null) {
return;
}
try {
spoutConfig.save(spoutConfigFile);
}
catch (IOException ex) {
getLogger().severe("Could not save config to " + spoutConfigFile + ex.toString());
}
}
public boolean isInJar(String resource) {
InputStream iStream = getResource(resource);
return iStream != null;
}
} }

View File

@ -21,6 +21,7 @@ import org.getspout.spoutapi.keyboard.Keyboard;
import org.getspout.spoutapi.player.SpoutPlayer; import org.getspout.spoutapi.player.SpoutPlayer;
import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.config.SpoutConfig;
import com.gmail.nossr50.datatypes.HUDmmo; import com.gmail.nossr50.datatypes.HUDmmo;
import com.gmail.nossr50.datatypes.PlayerProfile; import com.gmail.nossr50.datatypes.PlayerProfile;
import com.gmail.nossr50.datatypes.popups.PopupMMO; import com.gmail.nossr50.datatypes.popups.PopupMMO;
@ -134,7 +135,7 @@ public class SpoutStuff {
* Setup Spout config options * Setup Spout config options
*/ */
public static void setupSpoutConfigs() { public static void setupSpoutConfigs() {
String temp = plugin.getSpoutConfig().getString("Menu.Key", "KEY_M"); String temp = SpoutConfig.getInstance().getMenuKey();
for (Keyboard x : Keyboard.values()) { for (Keyboard x : Keyboard.values()) {
if (x.toString().equalsIgnoreCase(temp)) { if (x.toString().equalsIgnoreCase(temp)) {