Saving HashSet<String> commandused = new HashSet<String>();

Discussion in 'Plugin Development' started by thomas12000, Sep 2, 2014.

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

    thomas12000

    Hello !

    I'm kinda new on creating files and I want to know how to save and load this following hashset

    Code:java
    1. HashSet<String> commandused = new HashSet<String>();


    I don't know nearly anything about saving files and I need some help to do this cause my commands work perfectly if the server is online all the time but when I reload I don't have it saved so basically lose everything when I /reload ..

    Please I need someone to help me make smth like these

    Code:java
    1. public void onEnable(){
    2. loadConfig();
    3. }
    4.  
    5. public void onDisable(){
    6. saveConfig();
    7. }
    8.  
    9. public void saveConfig(){
    10. // help here
    11. }
    12.  
    13. public void loadConfig(){
    14. // help here
    15. }
     
  2. thomas12000
    http://wiki.bukkit.org/Configuration_API_Reference

    First of all, you should create the configuration file using saveDefaultConfig() in the onEnable() method in your main class.
    Code:
    public void onEnable() {
        saveDefaultConfig();
    }
    this will create a file called "config.yml" in the plugin directory, if it doesn't already exist.

    Now grab the server's default FileConfiguration by calling getConfig(). You would preferably do this in the onEnable() method too.
    Code:
    private FileConfiguration config;
     
    public void onEnable() {
        saveDefaultConfig();
        config = getConfig();
    }
    you can now use the 'config' object to read and modify the config file.

    To save the HashSet, you would probably want to create a StringList in the file, then fill that up with the contents of the HashSet.
    Code:
    config.set("path.to.stringlist", commandused);
    saveConfig(); // save the changes
    Although I'm not sure if it will accept a HashSet, so you might need to convert that to an ArrayList.

    The file would look something like this now
    Code:
    path:
      to:
        list:
        - 'command'
        - 'anothercommand'
    To load the contents back from the file, use the getStringList() method. This will return a new List<String>.
    Code:
    List<String> fromConfig = config.getStringList("path.to.stringlist");
     
  3. Offline

    thomas12000

    Thank you for the reply !

    I will try it !

    Arigato ^_^
     
  4. Offline

    Zupsub

    That's not such a good idea, since it seems like your Set isn't really to configurat your plugin. You just want to save/load some data.
    So there are two ways:
    - Use the postet way, but instead of using #getConfig() create your own file. The class YamlConfiguratation might help.
    - Second (my preferred) way: create a new file object with the name of your file. Then use a BufferedReader with a FileReader and a FileWriter to save/read all your strings splittet by line feed (\n). This assume that your strings doesn't contain \n themself, but the name of the variable look so..
     
  5. Zupsub
    But if the default configuration isn't used for anything else, then might aswell. However it would be a better idea to create a new yml file rather than using the config file, as it can cause some confusion. Using your preferred way would be unnecessary work because Bukkit already provides a good file management system.
     
Thread Status:
Not open for further replies.

Share This Page