API eCrud - The configuration API everyone can enjoy!

Discussion in 'Resources' started by Dudemister1999, Oct 26, 2014.

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

    Dudemister1999

    Hello! I re-created my old config API, but I made it better. It's now...

    - More efficient
    - Twice as user-friendly
    - Supports a lot of file types
    - Speedy
    - Easy saving, setting, getting, and checking of data

    I call it eCrud! (eCrud Creates Robust and Useful Datafiles)

    Need I say more? It currently uses two classes, the config itself and the config manager.

    ConfigManager.java (Pastebin: <Click>)
    Show Spoiler

    Code:java
    1.  
    2. import java.io.File;
    3. import java.util.HashMap;
    4.  
    5. public class ConfigManager
    6. {
    7. private static final HashMap<String, Config> configFiles = new HashMap<>();
    8.  
    9. /**
    10.   * Create a configuration file. Creates the file, adds it to the registry, and returns the new instance
    11.   * @param folder Folder to store the config file in
    12.   * @param name Name of file (NOTE: Include file extension!)
    13.   * @return The new Config instance.
    14.   */
    15. public static Config createConfig(File folder, String name)
    16. {
    17. Config cfg = new Config(folder, name);
    18.  
    19. if(!configFiles.containsKey(name))
    20. {
    21. if(cfg.create())
    22. {
    23. configFiles.put(name, cfg);
    24. return cfg;
    25. }
    26. else
    27. {
    28. System.err.println("Error: Could not create file.");
    29. return null;
    30. }
    31. }
    32. else
    33. {
    34. return getConfig(name);
    35. }
    36. }
    37.  
    38. /**
    39.   * Gets the config from the registry.
    40.   * @param name Name of config (Checks through config database, throws an InvalidConfigException if the inputted config isn't registered)
    41.   * @return The config selected from the registry.
    42.   */
    43. public static Config getConfig(String name)
    44. {
    45. if(configFiles.containsKey(name))
    46. {
    47. return configFiles.get(name);
    48. }
    49. else
    50. {
    51. throw new InvalidConfigException("Could not locate file " + name + "! (Was not registered)");
    52. }
    53. }
    54.  
    55. /**
    56.   * Deletes config's file and registry.
    57.   * @param name Name of config (Checks through config database, throws an InvalidConfigException if the inputted config isn't registered)
    58.   * @return True if deleted successfully, false if something went wrong.
    59.   */
    60. public static boolean deleteConfig(String name)
    61. {
    62. if(configFiles.containsKey(name))
    63. {
    64. Config cfg = configFiles.get(name);
    65.  
    66. if(cfg.delete())
    67. {
    68. configFiles.remove(name);
    69. return true;
    70. }
    71. else
    72. {
    73. return false;
    74. }
    75. }
    76. else
    77. {
    78. throw new InvalidConfigException("Could not delete file " + name + "! (Was not registered)");
    79. }
    80. }
    81.  
    82. /**
    83.   * Recreate a config (In the event of data corruption, updated cfgs, etc.)
    84.   * @param name Name of config (Checks through config database, throws an InvalidConfigException if the inputted config isn't registered)
    85.   * @return True if recreated, false if something went wrong.
    86.   */
    87. public static boolean recreateConfig(String name)
    88. {
    89. if(configFiles.containsKey(name))
    90. {
    91. Config cfg = configFiles.get(name);
    92.  
    93. return cfg.recreate();
    94. }
    95. else
    96. {
    97. throw new InvalidConfigException("Could not find file " + name + "! (Was not registered)");
    98. }
    99. }
    100.  
    101. /**
    102.   * Save all currently registered configurations.
    103.   */
    104. public static void saveAllConfigs()
    105. {
    106. for(Config cfg : configFiles.values())
    107. {
    108. cfg.save();
    109. }
    110. }
    111.  
    112. /**
    113.   * Use in your plugin's onDisable method. Just a precaution, to save all the files.
    114.   * Actually, don't. Ignore that top part. Save it if you want, it can cause issues if someone edits the file then reloads (It deletes the changes)
    115.   */
    116. public static void onDisable()
    117. {
    118. for(Config cfg : configFiles.values())
    119. {
    120. cfg.save();
    121. }
    122. configFiles.clear();
    123. }
    124. }
    125.  
    126. /**
    127. * Exception that's thrown in the event an invalid configuration is used/found.
    128. * @author Hydroxocobalamin
    129. */
    130. class InvalidConfigException extends RuntimeException
    131. {
    132. public InvalidConfigException(String ex)
    133. {
    134. super(ex);
    135. }
    136. }
    137.  



    Config.java (Pastebin: <Click>)
    Show Spoiler

    Code:java
    1.  
    2. import java.io.File;
    3. import java.io.IOException;
    4. import org.bukkit.configuration.file.YamlConfiguration;
    5.  
    6. /**
    7. * @author Hydroxocobalamin. All rights reserved.
    8. * License: Nobody cares... honestly.
    9. */
    10. public final class Config
    11. {
    12. private File configFile;
    13. private YamlConfiguration config;
    14. private String fileName;
    15.  
    16. /**
    17.   * Default constructor for configurations.
    18.   * @param folder Folder to store the config in
    19.   * @param name Name of the file (Specify extension!)
    20.   */
    21. public Config(File folder, String name)
    22. {
    23. fileName = name;
    24. configFile = new File(folder, name);
    25. }
    26.  
    27. /**
    28.   * Get the config file
    29.   * @return config file
    30.   */
    31. public File getFile()
    32. {
    33. return configFile;
    34. }
    35.  
    36. /**
    37.   * Get the YamlConfiguration paired with the config
    38.   * @return The config
    39.   */
    40. public YamlConfiguration getYaml()
    41. {
    42. return config;
    43. }
    44.  
    45. /**
    46.   * Get the name of the file
    47.   * @return File name
    48.   */
    49. public String getFileName()
    50. {
    51. return fileName;
    52. }
    53.  
    54. /**
    55.   * Get the folder the config is stored in
    56.   * @return The config's folder
    57.   */
    58. public File getFolder()
    59. {
    60. return configFile.getParentFile();
    61. }
    62.  
    63. /**
    64.   * Create a configuration section
    65.   * @param path Section to create
    66.   */
    67. public void createSection(String path)
    68. {
    69. if(!config.contains(path))
    70. {
    71. config.createSection(path);
    72. }
    73. }
    74.  
    75. /**
    76.   * Set the value to the config file.
    77.   * @param path Path to store the variable
    78.   * @param value Value to store on the path
    79.   * @return The object specified
    80.   */
    81. public Object setValue(String path, Object value)
    82. {
    83. if(hasValue(path, true))
    84. {
    85. config.set(path, value);
    86. }
    87. else
    88. {
    89. config.set(path, value);
    90. }
    91. return value;
    92. }
    93.  
    94. /**
    95.   * Check the config for a value
    96.   * @param path Path to find the variable
    97.   * @return Does the config have the value?
    98.   */
    99. public boolean hasValue(String path)
    100. {
    101. if(config.contains(path))
    102. {
    103. return true;
    104. }
    105. else
    106. {
    107. return false;
    108. }
    109. }
    110.  
    111. /**
    112.   * Check the config for a value
    113.   * @param path Path to find the variable
    114.   * @param createIfNot If it can't find the path, create it?
    115.   * @return Does the config have the value?
    116.   */
    117. public boolean hasValue(String path, boolean createIfNot)
    118. {
    119. if(config.contains(path))
    120. {
    121. return true;
    122. }
    123. else
    124. {
    125. if(createIfNot)
    126. {
    127. config.createSection(path);
    128. }
    129. return false;
    130. }
    131. }
    132.  
    133. /**
    134.   * Get an object from config, return null if it couldn't find the value
    135.   * @param path Path to find the value
    136.   * @return Config collected from file
    137.   */
    138. public Object getValue(String path)
    139. {
    140. if(config.contains(path))
    141. {
    142. return config.get(path);
    143. }
    144. else
    145. {
    146. config.createSection(path);
    147. }
    148. return null;
    149. }
    150.  
    151. /**
    152.   * Get an object from config, return default if it couldn't find the path
    153.   * @param path Path to find the value
    154.   * @param def Default value, if it couldn't find one in the config
    155.   * @return Config collected from file
    156.   */
    157. public Object getValue(String path, Object def)
    158. {
    159. if(config.contains(path))
    160. {
    161. return config.get(path);
    162. }
    163. else
    164. {
    165. config.createSection(path);
    166. config.set(path, def);
    167. return def;
    168. }
    169. }
    170.  
    171. /**
    172.   * Create the config file, and folder.
    173.   * @return True if it was created, false if an exception was thrown.
    174.   */
    175. public boolean create()
    176. {
    177. try
    178. {
    179. if(!getFolder().exists())
    180. {
    181. getFolder().mkdir();
    182. }
    183.  
    184. configFile.createNewFile();
    185. config = YamlConfiguration.loadConfiguration(configFile);
    186. return true;
    187. }
    188. catch(IOException ex)
    189. {
    190. System.err.println("Error while creating file (IOException): ");
    191. ex.printStackTrace();
    192. return false;
    193. }
    194. }
    195.  
    196. /**
    197.   * Delete the config, then re-create it.
    198.   * @return True if it worked, false if an exception was thrown.
    199.   */
    200. public boolean recreate()
    201. {
    202. configFile.delete();
    203. config = null;
    204.  
    205. try
    206. {
    207. if(!getFolder().exists())
    208. {
    209. getFolder().mkdir();
    210. }
    211.  
    212. configFile.createNewFile();
    213. config = YamlConfiguration.loadConfiguration(configFile);
    214. return true;
    215. }
    216. catch (IOException ex)
    217. {
    218. System.err.println("Error while creating file (IOException): ");
    219. ex.printStackTrace();
    220. return false;
    221. }
    222. }
    223.  
    224. /**
    225.   * Delete the config file.
    226.   * @return True or false, depending on if it was deleted or not.
    227.   */
    228. public boolean delete()
    229. {
    230. return configFile.delete();
    231. }
    232.  
    233. /**
    234.   * Delete the folder the config is stored in.
    235.   * @return True or false, depending on if it was deleted or not.
    236.   */
    237. public boolean deleteFolder()
    238. {
    239. return getFolder().delete();
    240. }
    241.  
    242.  
    243. /**
    244.   * Save the config to disk.
    245.   * @return True if it saved, false if an exception was thrown
    246.   */
    247. public boolean save()
    248. {
    249. try
    250. {
    251. config.save(configFile);
    252. return true;
    253. }
    254. catch(IOException ex)
    255. {
    256. return false;
    257. }
    258. }
    259. }
    260.  



    Example Plugin (Pastebin: <Click>)
    Show Spoiler

    Code:java
    1.  
    2. import org.bukkit.Bukkit;
    3. import org.bukkit.entity.Player;
    4. import org.bukkit.event.EventHandler;
    5. import org.bukkit.event.Listener;
    6. import org.bukkit.event.player.PlayerJoinEvent;
    7. import org.bukkit.plugin.java.JavaPlugin;
    8.  
    9. public class ExamplePlugin extends JavaPlugin implements Listener
    10. {
    11. private Config cfg;
    12.  
    13. public static String joinMessage = "&cWelcome, &6{player}!";
    14. public static boolean replaceJoinMessage = true;
    15.  
    16. @Override
    17. public void onEnable()
    18. {
    19. Bukkit.getPluginManager().registerEvents(this, this);
    20. cfg = ConfigManager.createConfig(getDataFolder(), "config.yml");
    21.  
    22. if(cfg.getYaml().contains("join.message") && cfg.getYaml().contains("join.replacemessage"))
    23. {
    24. replaceJoinMessage = (boolean)cfg.getValue("join.replacemessage", true);
    25. joinMessage = (String)cfg.getValue("join.message");
    26. }
    27. else
    28. {
    29. cfg.setValue("join.message", joinMessage);
    30. cfg.setValue("join.replacemessage", true);
    31. cfg.save();
    32. }
    33. }
    34.  
    35. @Override
    36. public void onDisable()
    37. {
    38. ConfigManager.onDisable();
    39. }
    40.  
    41. @EventHandler
    42. public void join(PlayerJoinEvent event)
    43. {
    44. Player player = event.getPlayer();
    45.  
    46. if(replaceJoinMessage)
    47. {
    48. event.setJoinMessage(joinMessage);
    49. }
    50. }
    51. }
    52.  



    So, what do you think? Should I add more, take away some, change something?

    Thanks for reading!
    -Hydroxocobalamin (OCHbl)

    Note: This software is protected by the HDC license. Read it here:
    Show Spoiler

    HDC = Hydroxocobalamin doesn't care.
    Just lemme see your project when you release it! <3
     
    Last edited: Jan 25, 2015
    jjssman likes this.
  2. Offline

    jjssman

    Very useful, Thanks! :D
     
  3. Offline

    Dudemister1999

  4. Offline

    XxZHALO13Xx

    Could you explain the methods more? And make a bigger example.. like saving diff things. for example. making when a player goes they go under a Player: path. and saving a hashmap using a player and int would be helpful too. nice api tho
     
    Dudemister1999 and ChipDev like this.
  5. Offline

    Dudemister1999

    @XxZHALO13Xx Well, I'll write some code tonight and update the post! Thanks for providing feedback, that's what makes products better you know!
     
  6. Offline

    Dmrtje

    @Dudemister1999 You should change your create method to:
    Code:
       public static Config createConfig(File folder, String name) {
            Config cfg = new Config(folder, name);
            if (!folder.exists()) {
                folder.mkdirs();
            }
            if (!configFiles.containsKey(name)) {
                if (cfg.create()) {
                    configFiles.put(name, cfg);
                    return cfg;
                } else {
                    System.err.println("Error: Could not create file " + name + ".");
                    return null;
                }
            } else {
                return getConfig(name);
            }
        }
    so we can also create files in a subfolder
    for example:
    Code:
    Config config = (new File(getDataFolder() + File.seperator + "submap name");
    before it did gave an error that it couldn't find the path but now if it can't find it it will create it
     
    Dudemister1999 likes this.
  7. Offline

    Dudemister1999

    @Dmrtje I'm in the works of a complete configuration control suite. When I finish it, I'll update the OP. Your suggestion will be implemented, thanks!
     
  8. Offline

    Dmrtje

    @Dudemister1999 no problem

    EDIT:

    its a really nice easy to use API. But the saving could be a little easier cause now you need to do ConfigManager.getConfig(String configname).save();
    But i think it wil be nicer if you just could do config.save(); and config.reload(); oow and maybe add a remove method cause now you need to do config.getYaml().set(value, null);
     
  9. Offline

    Dudemister1999

    @Dmrtje I've actually made a lot of adjustments. Instead of using ConfigManager to get to your config, try storing an instance (Sort of how JavaPlugin has a getConfig() method). New features include config.set(String path, Object value) (Auto saves), <T> T config.get(String path) & <T> T config.get(String path, Class type), and a whole lot more. ConfigManager has gotten an update as well, to include changing the .yml of the config at will, reloading, clearing, saving copies to other places, and more! I'm making a site to store my plugin code, and I'll reset the OP to link to it.
     
  10. Offline

    xTrollxDudex

    It only does anything if you decide to take action upon the infringement of that license, of which you may or may not be notified of.
     
  11. Offline

    teej107

    Just like any law. It would be useless if no one is enforcing it!

    How is this more efficient? It just looks like a wrapper class to me. It uses the same YML library that Bukkit uses. Also you can create any file with any type but the format will still be in YML since you are using a YML library.
     
    xTrollxDudex likes this.
  12. Offline

    Dudemister1999

    @xTrollxDudex
    I really don't intend to enforce it, I just wanted to feel special. I removed it in the new version. :p

    @teej107
    I meant efficient as in it allows you to do what you would with a YamlConfiguration faster. (Creating, saving, reloading, etc.)
    Soon (By which I mean when I get back), I'm adding a JSON config.
     
  13. Offline

    Totom3

    Wait.. what? Is it me or you are saving the config file each time a value is set or a section is created?
     
  14. Offline

    Dudemister1999

    @Totom3 This version, yes. Back when I was naive, and thought saving every time was a smart usage of disc. It's since been changed.
     
  15. Offline

    Totom3

  16. Offline

    Dudemister1999

    @Totom3 I will. Trying to get the site up, so people can get all my code from there.
     
  17. Offline

    Dudemister1999

    @bwfcwalshy I haven't seen his, I should check it out! It was my first useful resource. Also, if you're questioning the "More efficient" bit, that's comparing it to the older version of it.

    Soon (By which I mean when my website is fully functional), I'm adding a JSON config as well. Maybe I'll go farther, we'll just see where things go.
     
Thread Status:
Not open for further replies.

Share This Page