[LIB] WootConfig - Easy config and data saving for your plugin, without yml!

Discussion in 'Resources' started by woutwoot, Mar 24, 2014.

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

    woutwoot

    Hello everyone! A day ago I started working on my own configuration class. Why? I didn't like the Configuration method bukkit provided. That's just my opinion, no hating please :)

    This configuration class is very easy to use and allows you to save the most important things you will most likely need to save for Bukkit plugins. It uses the .properties file format, just like the standard server.properties.

    How to use it?
    1. Create an instance of my WootConfig class.
      Code:java
      1. WootConfig config = new WootConfig(new File("config.properties"));
    2. Try to load the current config file, in case it exists.
      Code:java
      1. try
      2. {
      3. config.loadFile();
      4. } catch (IOException e) {
      5. //Something went wrong while loading the file.
      6. }
    3. Save some settings to memory.
      Code:java
      1. config.setString("test", "name");
      2. config.setInteger("int", 1548748);
      3. config.setDouble("double", 4984.468);
    4. Save the settings in memory to the file.
      Code:java
      1. try {
      2. config.saveFile("Test config file by WootConfig");
      3. } catch (IOException e){
      4. //Something went wrong while saving the file.
      5. }
    5. Your file is now saved and ready to be loaded!
    6. To load the file into memory again use the code from step 1 again.
    7. Retrieve some settings from the config using:
      Code:java
      1. int oldInt = config.getInteger("int");
      2. Location savedLocation = config.getLocation("spawnloc");
    Completed code:
    Code:java
    1. WootConfig config = new WootConfig(new File("config.properties"));
    2. try {
    3. config.loadFile();
    4. } catch (IOException e) {
    5. }
    6. config.setString("name", "test");
    7. config.setString("test", "name");
    8. config.setInteger("int", 1548748);
    9. config.setDouble("double", 4984.468);
    10. try {
    11. config.saveFile("Test config file by WootConfig");
    12. } catch (IOException e) {
    13. }
    14. int oldInt = config.getInteger("int");
    15. Location savedLocation = config.getLocation("spawnloc");
    16. //Do stuff using the settings you just loaded.

    Features
    • Save double, int, String, boolean
    • Save arrays, lists and maps
    • Save ItemStacks, Locations, Inventorys
    • more soon!
    Download
    Regular download: http://woutwoot.com/download/
    GitHub gist: https://gist.github.com/woutwoot/9758674
    If you use my class, it would be nice if you give me credit somewhere, and like this post if you want to :)

    Note: I know the methods don't have to return booleans right now, but I might be using that later.
     
  2. woutwoot Looks cool, any change to github?
     
  3. Offline

    woutwoot

    Thanks! I'll put it on gitHub once I can. I'm currently using my mobile phone, so probably later today.
     
    Someone_Like_You likes this.
  4. woutwoot Okay :)
    EDIT: Thanks! , this question is kinda out of topic, but do you think I should create 1 file per player? or 1 file for all? (I will have to store quite a bit of data)...
     
  5. Offline

    woutwoot

    Sure, just create a WootConfig instance for every player using their name or something unique. Pseudo-code example:
    Code:java
    1. List<WootConfig> configs = new ArrayList<WootConfig>();
    2. for(Player p : playersToSave){
    3. configs.add(new WootConfig(new File(this.getDataFolder() + File.separator + p.getName())));
    4. }

    After that you can loop through the configs and access the one with the right file.

    But, if the player doen't have to edit the config, and it's only saving data, I'd use one file.
     
  6. woutwoot Will multiple configs cause lags/memory leaks? (theres gonna be LOTS of players,) the config itself will save 1 inventory for the player, and few integers..
     
  7. Offline

    woutwoot

    Well, there is going to be some lag, whatever you do, but If you really need it very very optimized, I suggest you make your own config class that just saves it to text files with as least byes as possible. Memory leaks shouldn't happen with my class.
     
    Someone_Like_You likes this.
  8. woutwoot How can I check if X config alredy exists?
     
  9. Offline

    woutwoot

    If you're having problems with stuff like this, you should ask in the plugin development forum. But, just because I'm a nice guy :p :
    Code:java
    1. if(file.exists()){
    2. //File exists, do stuff
    3. }else{
    4. //File does not exist, create one or show message
    5. }

    Note that my class already checks if a file exists when you call the load method, if it does not exist, it doesn't do anything.
     
    Someone_Like_You likes this.
  10. Offline

    L33m4n123

    Something along

    File file = new File("MyFile.properties");

    if (file.exist() {
    // your file exists
    } else {
    new WoolConfig(file);
    }

    edit ninja'd
     
    Someone_Like_You and woutwoot like this.
  11. woutwoot I have yet to use configs since I do not like bukkit config system (looks too complicated for me.), therefore I had to store the data on the player inventory itself (like maybe item lore) :p, thanks tho
    Edit: well, again, havent really used configs... how can I set something to something? so like when player login, Il set the first line of the config to player name and integer..
     
  12. Offline

    woutwoot

    You could make one config per var you're storing and use the player name as the key. For example:
    Code:java
    1. WootConfig confNumberOfJoins = new WootConfig(new File(this.getDataFolder() + File.separator + "confNumberOfJoins.properties"));
    2. confNumberOfJoins.loadFile();
    3. @EventHandler
    4. public void onPlayerJoin(PlayerJoinEvent event){
    5. confNumberOfJoins.setInteger(event.getPlayer.getName(), confNumberOfJoins.getInteger(event.getPlayer.getName())+1);
    6. }
     
  13. Offline

    Goblom

    Quite interesting
     
    woutwoot likes this.
  14. Offline

    woutwoot

    Thanks! I'll be uploading an improved version soon.

    Update! You can now also save hashmaps, and load the entire config as a hashmap!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  15. Offline

    ToastHelmi

    how to do defaultConfigfiles
     
  16. Offline

    woutwoot

    How do you mean? Having settings already present? I might be implementing some code for that, but for now, I tink you should just save the defaults to the config file if the file does not exist yet.
     
  17. Offline

    Surfdudeboy

    I love this! However, I was having massive issues using this with file paths, much like the other Java noobs such as myself. I learned some stuff and then I edited the constructor of your awesome library to correctly create file paths and the file that goes inside!
    Code:java
    1. package com.woutwoot.wootconf;
    2.  
    3. import java.io.ByteArrayInputStream;
    4. import java.io.ByteArrayOutputStream;
    5. import java.io.File;
    6. import java.io.FileReader;
    7. import java.io.FileWriter;
    8. import java.io.IOException;
    9. import java.util.ArrayList;
    10. import java.util.HashMap;
    11. import java.util.List;
    12. import java.util.Map;
    13. import java.util.Properties;
    14.  
    15. import org.bukkit.Bukkit;
    16. import org.bukkit.Location;
    17. import org.bukkit.inventory.Inventory;
    18. import org.bukkit.inventory.ItemStack;
    19. import org.bukkit.util.io.BukkitObjectInputStream;
    20. import org.bukkit.util.io.BukkitObjectOutputStream;
    21. import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder;
    22.  
    23. /**
    24. * Configuration class that uses .properties file format to store various settings and data.
    25. *
    26. * @author woutwoot
    27. */
    28. public class WootConfig {
    29.  
    30. private File configFile;
    31. private Properties props = new Properties();
    32.  
    33. /**
    34.   * @param configFile
    35.   */
    36. public WootConfig(File file) {
    37. this.configFile = file;
    38. this.setConfigFile(configFile);
    39.  
    40. String s = file.getPath();
    41. int index = s.lastIndexOf(File.separator);
    42. String path = s.substring(0, index);
    43. File folder = new File(path);
    44.  
    45. if(!folder.exists()){
    46. folder.mkdir();
    47. }
    48. if(!file.exists()){
    49. try {
    50. file.createNewFile();
    51. } catch (IOException e) {
    52. }
    53. }
    54. }
    55.  
    56. /**
    57.   * Loads the actual config file in memory.
    58.   *
    59.   * @throws IOException
    60.   */
    61. public void loadFile() throws IOException {
    62. ....
    63. ........
    64. ............ rest of class

    After replacing your original constructor with mine... to use this to make a new fire path/folders and a file inside, do the following:
    Code:java
    1. File file = new File("plugins" + File.separator + "AwesomePluginName" + File.separator + "data" + File.separator + "data.txt");
    2. WootConfig config = new WootConfig(file); //Creates the file ".../plugins/AwesomePluginName/data/data.txt"

    And then you just use your wootconfig as you outlined before :D
     
  18. Offline

    woutwoot

    Nice addition :) However, I do like to keep a little more control over files and folders being created. I'll consider updating my code! (You could have also used folder.mkdirs() )
     
Thread Status:
Not open for further replies.

Share This Page