From b80a29ca04598525fb19a15804f683db31802d49 Mon Sep 17 00:00:00 2001 From: bm01 Date: Tue, 3 Jul 2012 21:57:24 +0200 Subject: [PATCH] Configs rework, fixed mod config files not loading, fixed comment blocks not being copied --- .../java/com/gmail/nossr50/config/Config.java | 30 ++--- .../gmail/nossr50/config/ConfigLoader.java | 114 +++++++++--------- .../gmail/nossr50/config/HiddenConfig.java | 26 ++-- .../com/gmail/nossr50/config/SpoutConfig.java | 46 +++---- .../gmail/nossr50/config/TreasuresConfig.java | 26 ++-- .../config/mods/CustomArmorConfig.java | 34 +++--- .../config/mods/CustomBlocksConfig.java | 39 +++--- .../config/mods/CustomToolsConfig.java | 28 ++--- .../nossr50/config/mods/ModConfigLoader.java | 13 -- .../nossr50/config/repair/RepairConfig.java | 10 +- .../config/repair/RepairConfigManager.java | 12 +- src/main/java/com/gmail/nossr50/mcMMO.java | 4 +- 12 files changed, 157 insertions(+), 225 deletions(-) delete mode 100644 src/main/java/com/gmail/nossr50/config/mods/ModConfigLoader.java diff --git a/src/main/java/com/gmail/nossr50/config/Config.java b/src/main/java/com/gmail/nossr50/config/Config.java index 5fdf2035f..8f2cfd84e 100644 --- a/src/main/java/com/gmail/nossr50/config/Config.java +++ b/src/main/java/com/gmail/nossr50/config/Config.java @@ -5,21 +5,26 @@ import java.util.Set; import org.bukkit.configuration.ConfigurationSection; -import com.gmail.nossr50.mcMMO; - public class Config extends ConfigLoader { - public int xpGainMultiplier = 1; private static Config instance; + public int xpGainMultiplier = 1; + + private Config() { + super("config.yml"); + xpGainMultiplier = getExperienceGainsGlobalMultiplier(); + } public static Config getInstance() { if (instance == null) { - instance = new Config(mcMMO.p); - instance.load(); + instance = new Config(); } return instance; } + @Override + protected void loadKeys() {} + /* * GENERAL SETTINGS */ @@ -370,19 +375,4 @@ public class Config extends ConfigLoader { public double getFormulaMultiplierAxes() { return config.getDouble("Experience.Formula.Multiplier.Axes", 1.0); } public double getFormulaMultiplierAcrobatics() { return config.getDouble("Experience.Formula.Multiplier.Acrobatics", 1.0); } public double getFormulaMultiplierFishing() { return config.getDouble("Experience.Formula.Multiplier.Fishing", 1.0); } - - /* - * CONFIG LOADING - */ - - private Config(mcMMO plugin) { - super(plugin, "config.yml"); - saveIfNotExist(); - xpGainMultiplier = getExperienceGainsGlobalMultiplier(); - } - - @Override - protected void loadKeys() { - plugin.getLogger().info("Loading mcMMO config.yml File..."); - } } diff --git a/src/main/java/com/gmail/nossr50/config/ConfigLoader.java b/src/main/java/com/gmail/nossr50/config/ConfigLoader.java index 97865fa02..b5bf6f98f 100644 --- a/src/main/java/com/gmail/nossr50/config/ConfigLoader.java +++ b/src/main/java/com/gmail/nossr50/config/ConfigLoader.java @@ -1,7 +1,9 @@ package com.gmail.nossr50.config; import java.io.File; -import java.io.IOException; +import java.io.FileOutputStream; +import java.io.InputStream; +import java.io.OutputStream; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; @@ -9,69 +11,69 @@ import org.bukkit.configuration.file.YamlConfiguration; import com.gmail.nossr50.mcMMO; public abstract class ConfigLoader { + protected static final mcMMO plugin = mcMMO.p; protected String fileName; protected File configFile; - protected File dataFolder; - protected final mcMMO plugin; protected FileConfiguration config; - public ConfigLoader(mcMMO plugin, String fileName){ - this.plugin = plugin; + public ConfigLoader(String relativePath, String fileName){ this.fileName = fileName; - dataFolder = plugin.getDataFolder(); - configFile = new File(dataFolder, File.separator + fileName); + configFile = new File(plugin.getDataFolder(), relativePath + File.separator + fileName); + load(); + } + + public ConfigLoader(String fileName){ + this.fileName = fileName; + configFile = new File(plugin.getDataFolder(), fileName); + load(); + } + + protected void load() { + if (!configFile.exists()) { + plugin.getLogger().info("Creating mcMMO " + fileName + " File..."); + createFile(); + } + else { + plugin.getLogger().info("Loading mcMMO " + fileName + " File..."); + } + config = YamlConfiguration.loadConfiguration(configFile); } - /** - * Load this config file. - */ - public void load() { - if (!configFile.exists()) { - dataFolder.mkdir(); - saveConfig(); - } - - addDefaults(); - loadKeys(); - } - - /** - * Save this config file. - */ - private void saveConfig() { - try { - config.save(configFile); - } - catch (IOException ex) { - plugin.getLogger().severe("Could not save config to " + configFile + ex); - } - } - - protected void saveIfNotExist() { - if (!configFile.exists()) { - if (plugin.getResource(fileName) != null) { - plugin.saveResource(fileName, false); - } - } - rereadFromDisk(); - } - - protected void rereadFromDisk() { - config = YamlConfiguration.loadConfiguration(configFile); - } - - /** - * Add the defaults to this config file. - */ - protected void addDefaults() { - config.options().copyDefaults(true); - saveConfig(); - } - - /** - * Load the keys from this config file. - */ protected abstract void loadKeys(); + protected void createFile() { + if (configFile.exists()) { + return; + } + + configFile.getParentFile().mkdirs(); + + InputStream inputStream = plugin.getResource(fileName); + + if (inputStream != null) { + try { + copyStreamToFile(inputStream, configFile); + } catch (Exception e) { + e.printStackTrace(); + } + } + else { + plugin.getLogger().severe("Missing ressource file: '" + configFile.getName() + "' please notify the plugin authors"); + } + } + + private static void copyStreamToFile(InputStream inputStream, File file) throws Exception { + OutputStream outputStream = new FileOutputStream(file); + + int read = 0; + byte[] bytes = new byte[1024]; + + while ((read = inputStream.read(bytes)) != -1) { + outputStream.write(bytes, 0, read); + } + + inputStream.close(); + outputStream.close(); + } } diff --git a/src/main/java/com/gmail/nossr50/config/HiddenConfig.java b/src/main/java/com/gmail/nossr50/config/HiddenConfig.java index bf908205f..e450e06cf 100644 --- a/src/main/java/com/gmail/nossr50/config/HiddenConfig.java +++ b/src/main/java/com/gmail/nossr50/config/HiddenConfig.java @@ -4,42 +4,32 @@ import org.bukkit.configuration.file.YamlConfiguration; import com.gmail.nossr50.mcMMO; -public class HiddenConfig extends ConfigLoader { - private static String fileName; +public class HiddenConfig { private static HiddenConfig instance; + private static String fileName; private static YamlConfiguration config; - private static boolean chunkletsEnabled; - public HiddenConfig(mcMMO plugin, String fileName) { - super(plugin, fileName); + public HiddenConfig(String fileName) { HiddenConfig.fileName = fileName; + load(); } - public static HiddenConfig getInstance() { if (instance == null) { - instance = new HiddenConfig(mcMMO.p, "hidden.yml"); - instance.load(); + instance = new HiddenConfig("hidden.yml"); } return instance; } - @Override public void load() { - if (plugin.getResource(fileName) != null) { - loadKeys(); + if (mcMMO.p.getResource(fileName) != null) { + config = YamlConfiguration.loadConfiguration(mcMMO.p.getResource(fileName)); + chunkletsEnabled = config.getBoolean("Options.Chunklets", true); } } - @Override - protected void loadKeys() { - config = YamlConfiguration.loadConfiguration(plugin.getResource(fileName)); - - chunkletsEnabled = config.getBoolean("Options.Chunklets", true); - } - public boolean getChunkletsEnabled() { return chunkletsEnabled; } diff --git a/src/main/java/com/gmail/nossr50/config/SpoutConfig.java b/src/main/java/com/gmail/nossr50/config/SpoutConfig.java index 721a15c17..e76689bd8 100644 --- a/src/main/java/com/gmail/nossr50/config/SpoutConfig.java +++ b/src/main/java/com/gmail/nossr50/config/SpoutConfig.java @@ -1,25 +1,38 @@ package com.gmail.nossr50.config; -import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.datatypes.HudType; public class SpoutConfig extends ConfigLoader { private static SpoutConfig instance; + public HudType defaultHudType; + + private SpoutConfig() { + super("spout.yml"); + } public static SpoutConfig getInstance() { if (instance == null) { - instance = new SpoutConfig(mcMMO.p); - instance.load(); + instance = new SpoutConfig(); } return instance; } - public HudType defaultHudType; + @Override + protected void loadKeys() { + // Setup default HUD + String temp = config.getString("Spout.HUD.Default", "STANDARD"); - private SpoutConfig(mcMMO plugin) { - super(plugin, "spout.yml"); - saveIfNotExist(); + for (HudType hudType : HudType.values()) { + if (hudType.toString().equalsIgnoreCase(temp.toString())) { + defaultHudType = hudType; + break; + } + } + + if (defaultHudType == null) { + defaultHudType = HudType.STANDARD; + } } public boolean getShowPowerLevel() { return config.getBoolean("HUD.Show_Power_Level", true); } @@ -77,23 +90,4 @@ public class SpoutConfig extends ConfigLoader { public double getRetroHUDFishingRed() { return config.getDouble("HUD.Retro.Colors.Fishing.RED", 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); } - - @Override - protected void loadKeys() { - plugin.getLogger().info("Loading mcMMO spout.yml File..."); - - // Setup default HUD - String temp = config.getString("Spout.HUD.Default", "STANDARD"); - - for (HudType hudType : HudType.values()) { - if (hudType.toString().equalsIgnoreCase(temp.toString())) { - defaultHudType = hudType; - break; - } - } - - if (defaultHudType == null) { - defaultHudType = HudType.STANDARD; - } - } } diff --git a/src/main/java/com/gmail/nossr50/config/TreasuresConfig.java b/src/main/java/com/gmail/nossr50/config/TreasuresConfig.java index bb4e4179c..63a2ba706 100644 --- a/src/main/java/com/gmail/nossr50/config/TreasuresConfig.java +++ b/src/main/java/com/gmail/nossr50/config/TreasuresConfig.java @@ -11,23 +11,12 @@ import org.bukkit.Material; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.inventory.ItemStack; -import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.datatypes.treasure.ExcavationTreasure; import com.gmail.nossr50.datatypes.treasure.FishingTreasure; import com.gmail.nossr50.datatypes.treasure.Treasure; public class TreasuresConfig extends ConfigLoader{ private static TreasuresConfig instance; - - public static TreasuresConfig getInstance() { - if (instance == null) { - instance = new TreasuresConfig(mcMMO.p); - instance.load(); - } - - return instance; - } - public List excavationFromDirt = new ArrayList(); public List excavationFromGrass = new ArrayList(); public List excavationFromSand = new ArrayList(); @@ -41,15 +30,20 @@ public class TreasuresConfig extends ConfigLoader{ public List fishingRewardsTier4 = new ArrayList(); public List fishingRewardsTier5 = new ArrayList(); - private TreasuresConfig(mcMMO plugin) { - super(plugin, "treasures.yml"); - saveIfNotExist(); + private TreasuresConfig() { + super("treasures.yml"); + } + + public static TreasuresConfig getInstance() { + if (instance == null) { + instance = new TreasuresConfig(); + } + + return instance; } @Override protected void loadKeys() { - plugin.getLogger().info("Loading mcMMO treasures.yml File..."); - Map treasures = new HashMap(); ConfigurationSection treasureSection = config.getConfigurationSection("Treasures"); Set treasureConfigSet = treasureSection.getKeys(false); diff --git a/src/main/java/com/gmail/nossr50/config/mods/CustomArmorConfig.java b/src/main/java/com/gmail/nossr50/config/mods/CustomArmorConfig.java index 783a25e83..407801211 100644 --- a/src/main/java/com/gmail/nossr50/config/mods/CustomArmorConfig.java +++ b/src/main/java/com/gmail/nossr50/config/mods/CustomArmorConfig.java @@ -8,40 +8,36 @@ import java.util.Set; import org.bukkit.configuration.ConfigurationSection; -import com.gmail.nossr50.mcMMO; +import com.gmail.nossr50.config.ConfigLoader; import com.gmail.nossr50.datatypes.mods.CustomItem; import com.gmail.nossr50.skills.repair.Repairable; import com.gmail.nossr50.skills.repair.RepairableFactory; -public class CustomArmorConfig extends ModConfigLoader{ +public class CustomArmorConfig extends ConfigLoader{ private static CustomArmorConfig instance; + private List repairables; + public List customBootIDs = new ArrayList(); + public List customChestplateIDs = new ArrayList(); + public List customHelmetIDs = new ArrayList(); + public List customLeggingIDs = new ArrayList(); + public List customIDs = new ArrayList(); + public List customArmorList = new ArrayList(); + public HashMap customArmor = new HashMap(); + + public CustomArmorConfig() { + super("ModConfigs", "armor.yml"); + } public static CustomArmorConfig getInstance() { if (instance == null) { - instance = new CustomArmorConfig(mcMMO.p); + instance = new CustomArmorConfig(); } return instance; } - private List repairables; - - public List customBootIDs = new ArrayList(); - public List customChestplateIDs = new ArrayList(); - public List customHelmetIDs = new ArrayList(); - public List customLeggingIDs = new ArrayList(); - - public List customIDs = new ArrayList(); - public List customArmorList = new ArrayList(); - public HashMap customArmor = new HashMap(); - - public CustomArmorConfig(mcMMO plugin) { - super(plugin, "armor.yml"); - } - @Override protected void loadKeys() { - plugin.getLogger().info("Loading mcMMO armor.yml File..."); repairables = new ArrayList(); loadArmor("Boots", customBootIDs); diff --git a/src/main/java/com/gmail/nossr50/config/mods/CustomBlocksConfig.java b/src/main/java/com/gmail/nossr50/config/mods/CustomBlocksConfig.java index c86b1761d..2bf3f6d2d 100644 --- a/src/main/java/com/gmail/nossr50/config/mods/CustomBlocksConfig.java +++ b/src/main/java/com/gmail/nossr50/config/mods/CustomBlocksConfig.java @@ -8,41 +8,36 @@ import java.util.Set; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.inventory.ItemStack; -import com.gmail.nossr50.mcMMO; +import com.gmail.nossr50.config.ConfigLoader; import com.gmail.nossr50.datatypes.mods.CustomBlock; -public class CustomBlocksConfig extends ModConfigLoader{ +public class CustomBlocksConfig extends ConfigLoader { private static CustomBlocksConfig instance; + public List customExcavationBlocks = new ArrayList(); + public List customHerbalismBlocks = new ArrayList(); + public List customMiningBlocks = new ArrayList(); + public List customWoodcuttingBlocks = new ArrayList(); + public List customOres = new ArrayList(); + public List customLogs = new ArrayList(); + public List customLeaves = new ArrayList(); + public List customAbilityBlocks = new ArrayList(); + public List customItems = new ArrayList(); + public List customBlocks = new ArrayList(); + + public CustomBlocksConfig() { + super("ModConfigs", "blocks.yml"); + } public static CustomBlocksConfig getInstance() { if (instance == null) { - instance = new CustomBlocksConfig(mcMMO.p); + instance = new CustomBlocksConfig(); } return instance; } - public List customExcavationBlocks = new ArrayList(); - public List customHerbalismBlocks = new ArrayList(); - public List customMiningBlocks = new ArrayList(); - public List customWoodcuttingBlocks = new ArrayList(); - - public List customOres = new ArrayList(); - public List customLogs = new ArrayList(); - public List customLeaves = new ArrayList(); - public List customAbilityBlocks = new ArrayList(); - - public List customItems = new ArrayList(); - public List customBlocks = new ArrayList(); - - public CustomBlocksConfig(mcMMO plugin) { - super(plugin, "blocks.yml"); - } - @Override protected void loadKeys() { - plugin.getLogger().info("Loading mcMMO blocks.yml File..."); - loadBlocks("Excavation", customExcavationBlocks); loadBlocks("Herbalism", customHerbalismBlocks); loadBlocks("Mining", customMiningBlocks); diff --git a/src/main/java/com/gmail/nossr50/config/mods/CustomToolsConfig.java b/src/main/java/com/gmail/nossr50/config/mods/CustomToolsConfig.java index cfcab287a..4a53ec0fb 100644 --- a/src/main/java/com/gmail/nossr50/config/mods/CustomToolsConfig.java +++ b/src/main/java/com/gmail/nossr50/config/mods/CustomToolsConfig.java @@ -8,42 +8,38 @@ import java.util.Set; import org.bukkit.configuration.ConfigurationSection; -import com.gmail.nossr50.mcMMO; +import com.gmail.nossr50.config.ConfigLoader; import com.gmail.nossr50.datatypes.mods.CustomTool; import com.gmail.nossr50.skills.repair.Repairable; import com.gmail.nossr50.skills.repair.RepairableFactory; -public class CustomToolsConfig extends ModConfigLoader { +public class CustomToolsConfig extends ConfigLoader { private static CustomToolsConfig instance; - - public static CustomToolsConfig getInstance() { - if (instance == null) { - instance = new CustomToolsConfig(mcMMO.p); - } - - return instance; - } - private List repairables; - public List customAxeIDs = new ArrayList(); public List customBowIDs = new ArrayList(); public List customHoeIDs = new ArrayList(); public List customPickaxeIDs = new ArrayList(); public List customShovelIDs = new ArrayList(); public List customSwordIDs = new ArrayList(); - public List customIDs = new ArrayList(); public List customToolList = new ArrayList(); public HashMap customTools = new HashMap(); - private CustomToolsConfig(mcMMO plugin) { - super(plugin, "tools.yml"); + private CustomToolsConfig() { + super("ModConfigs", "tools.yml"); + } + + public static CustomToolsConfig getInstance() { + if (instance == null) { + instance = new CustomToolsConfig(); + } + + return instance; } @Override protected void loadKeys() { - plugin.getLogger().info("Loading mcMMO tools.yml File..."); repairables = new ArrayList(); loadTool("Axes", customAxeIDs); diff --git a/src/main/java/com/gmail/nossr50/config/mods/ModConfigLoader.java b/src/main/java/com/gmail/nossr50/config/mods/ModConfigLoader.java deleted file mode 100644 index 08ad6afa2..000000000 --- a/src/main/java/com/gmail/nossr50/config/mods/ModConfigLoader.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.gmail.nossr50.config.mods; - -import java.io.File; - -import com.gmail.nossr50.mcMMO; -import com.gmail.nossr50.config.ConfigLoader; - -public abstract class ModConfigLoader extends ConfigLoader{ - - public ModConfigLoader(mcMMO plugin, String fileName) { - super(plugin, "ModConfigs" + File.separator + fileName); - } -} diff --git a/src/main/java/com/gmail/nossr50/config/repair/RepairConfig.java b/src/main/java/com/gmail/nossr50/config/repair/RepairConfig.java index 572bb1d38..c750f7e3d 100644 --- a/src/main/java/com/gmail/nossr50/config/repair/RepairConfig.java +++ b/src/main/java/com/gmail/nossr50/config/repair/RepairConfig.java @@ -7,7 +7,6 @@ import java.util.Set; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.file.YamlConfiguration; -import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.config.ConfigLoader; import com.gmail.nossr50.skills.repair.RepairItemType; import com.gmail.nossr50.skills.repair.RepairMaterialType; @@ -17,16 +16,11 @@ import com.gmail.nossr50.skills.repair.RepairableFactory; public class RepairConfig extends ConfigLoader { private List repairables; - public RepairConfig(mcMMO plugin, String fileName) { - super(plugin, fileName); + public RepairConfig(String fileName) { + super(fileName); this.config = YamlConfiguration.loadConfiguration(this.configFile); } - @Override - public void load() { - loadKeys(); - } - @Override protected void loadKeys() { repairables = new ArrayList(); diff --git a/src/main/java/com/gmail/nossr50/config/repair/RepairConfigManager.java b/src/main/java/com/gmail/nossr50/config/repair/RepairConfigManager.java index 88927d6e1..e4422328b 100644 --- a/src/main/java/com/gmail/nossr50/config/repair/RepairConfigManager.java +++ b/src/main/java/com/gmail/nossr50/config/repair/RepairConfigManager.java @@ -22,22 +22,18 @@ public class RepairConfigManager { plugin.saveResource("repair.vanilla.yml", false); } - for (String location : dataFolder.list()) { - if (!pattern.matcher(location).matches()) { + for (String fileName : dataFolder.list()) { + if (!pattern.matcher(fileName).matches()) { continue; } - plugin.getLogger().info("Loading " + location + " repair config file..."); - - File file = new File(dataFolder, location); + File file = new File(dataFolder, fileName); if (file.isDirectory()) { continue; } - RepairConfig rConfig = new RepairConfig(plugin, location); - rConfig.load(); - + RepairConfig rConfig = new RepairConfig(fileName); List rConfigRepairables = rConfig.getLoadedRepairables(); if (rConfigRepairables != null) { diff --git a/src/main/java/com/gmail/nossr50/mcMMO.java b/src/main/java/com/gmail/nossr50/mcMMO.java index a8f32d212..17cfeed46 100644 --- a/src/main/java/com/gmail/nossr50/mcMMO.java +++ b/src/main/java/com/gmail/nossr50/mcMMO.java @@ -127,17 +127,15 @@ public class mcMMO extends JavaPlugin { List repairables = new ArrayList(); if (configInstance.getToolModsEnabled()) { - CustomToolsConfig.getInstance().load(); repairables.addAll(CustomToolsConfig.getInstance().getLoadedRepairables()); } if (configInstance.getArmorModsEnabled()) { - CustomArmorConfig.getInstance().load(); repairables.addAll(CustomArmorConfig.getInstance().getLoadedRepairables()); } if (configInstance.getBlockModsEnabled()) { - CustomBlocksConfig.getInstance().load(); + CustomBlocksConfig.getInstance(); } //Load repair configs, make manager, and register them at this time