Load problems with a FileConfiguration Wrapper

Discussion in 'Plugin Development' started by Cryptite, Jun 5, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    Cryptite

    I'm in the process of trying to develop a simple wrapper for FileConfiguration that allows you to essentially read and write config file elements in one-line of code, making (hopefully) life easier for both me and anyone else interested in these kinds of things. My apologies if this already exists somewhere, but here's what I essentially have at the moment.

    Code:java
    1. public class ConfigFile {
    2. protected MyPlugin plugin;
    3. protected FileConfiguration config;
    4. private File configFile = null;
    5. public String configFilename;
    6.  
    7. public ConfigFile(MyPlugin plugin, String configFilename) {
    8. this.plugin = plugin;
    9. this.configFilename = configFilename;
    10. }
    11.  
    12. public String get(String key, Object defaultValue) {
    13. ConfigurationSection section = config.getConfigurationSection(key);
    14. if (section == null) {
    15. //No section here, best make it, then.
    16. section = config.createSection(key);
    17. }
    18.  
    19. if (section.get(key) != null) {
    20. //Return our lovely section data.
    21. return section.get(key).toString();
    22. } else {
    23. //Oh dear, let's return our variable's default value.
    24. if (defaultValue == null) {
    25. //Because I want strings back, I can't return a string called null, so manually doing it.
    26. return null;
    27. } else {
    28. //Return the variable default value as string.
    29. return defaultValue.toString();
    30. }
    31. }
    32. }
    33.  
    34. public void set(String key, Object value) {
    35. ConfigurationSection section = config.getConfigurationSection(key);
    36. if (section == null) {
    37. //The section doesn't exist, so let's make it first.
    38. config.createSection(key);
    39. }
    40. if (value != null) {
    41. config.set(key, value);
    42. }
    43. }
    44.  
    45. public FileConfiguration getConfig() {
    46. if (config == null) {
    47. load();
    48. }
    49. return config;
    50. }
    51.  
    52. public void load() {
    53. if (configFile == null) {
    54. //No config file exists. Gotta make it.
    55. configFile = new File(plugin.getDataFolder(), configFilename);
    56. }
    57. config = YamlConfiguration.loadConfiguration(configFile);
    58. }
    59.  
    60. public void save() {
    61. if (config == null || configFile == null) {
    62. return;
    63. }
    64. try {
    65. config.save(configFile);
    66. } catch (IOException ex) {
    67. log.info("Could not save " + configFile);
    68. ex.printStackTrace();
    69. }
    70. }
    71. }

    Now, the idea is that, after creating a new ConfigFile class after providing the plugin and the filename you want to use, you can use one-liners like the following:

    Code:java
    1. someInteger= Integer.parseInt(config.get("path.to.integer", someInteger));
    2. someString= config.get("path.to.string", someString);


    Provided you've setup your variables with an initial value (0 for an integer, for example), the idea is that that one line of code will either return the value of the variable in the config file or, if it doesn't exist, return the default value of that integer (hence passing it as the second argument in config.get)

    Now, the saving of data works, but the loading of it doesn't. I'm sure the solution is simple, but I'm missing it somehow. Essentially I can have an example situation like:

    Code:java
    1.  
    2. public void load() {
    3. ConfigFile config = new ConfigFile(plugin, "config.yml");
    4. config.getConfig();
    5. someInteger= Integer.parseInt(config.get("path.to.integer", someInteger));
    6. someString= config.get("path.to.string", someString);
    7. }
    8.  


    The integer and string both return their default values and I'm stuck at this point. Any ideas guys? Thanks in advance!
     
Thread Status:
Not open for further replies.

Share This Page