Solved Can't save custom config

Discussion in 'Plugin Development' started by NonameSL, Sep 7, 2014.

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

    NonameSL

    I am trying to make a config for each player, and save stuff in each config. But for some reason, when I save something in the config, nothing happens. My code:
    Code:java
    1. Main.getPlayerConfig(p.getName()).set(/*my path*/, /*my object*/);
    2. Main.saveConfig(p.getName());


    The methods are:
    Code:java
    1. public static File getPlayerFile(String str){
    2. File file = new File(Main.getInstance().getDataFolder(), "users");
    3. for(File yml : file.listFiles()){
    4. if(yml.getName().equals(str.toLowerCase()+".yml")){
    5. return yml;
    6. }
    7. }
    8. File playerFile = new File(file.getPath(), str+".yml");
    9. if(!playerFile.exists()){
    10. try {
    11. playerFile.createNewFile();
    12. } catch (IOException e) {
    13. e.printStackTrace();
    14. }
    15. }
    16. return playerFile;
    17. }
    18. public static final void saveConfig(String config){
    19. File file = getPlayerFile(config);
    20. YamlConfiguration cfg = getPlayerConfig(config);
    21. try{
    22. cfg.load(file);
    23. cfg.save(file);
    24. }catch(Exception e){
    25. e.printStackTrace();
    26. }
    27. }
    28. public static YamlConfiguration getConfig(File file){
    29. if(!file.exists()){
    30. try {
    31. file.createNewFile();
    32. } catch (IOException e) {
    33. Main.getInstance().getLogger().warning("Failed to create file "+file.getName()+"!");
    34. }
    35. }
    36. return YamlConfiguration.loadConfiguration(file);
    37.  
    38. }
    39. public static final YamlConfiguration getPlayerConfig(String str){
    40. return getConfig(getPlayerFile(str));
    41. }

    I also have a PlayerJoinEvent where I create the configs for each players:
    Code:java
    1. Player p = e.getPlayer();
    2. YamlConfiguration config = Main.getPlayerConfig(p.getName());
    3. config.addDefault("loadout", "");
    4. config.addDefault("level", 1);
    5. Main.saveConfig(p.getName());


    I have no idea what's wrong, please help! Thanks in advance!
     
  2. Offline

    mine-care

    My sugestion at first is use less static... Static is not only about easy access.
    Secondly, you get any errors? Have you tried debugging?
     
  3. Offline

    fireblast709

    NonameSL you create a different instance in your save method. Instead, get the YamlConfiguration object, set the data, and call save(File) on that same YamlConfiguration object
     
    Goblom likes this.
  4. Offline

    NonameSL

    mine-care I use the method in other classes so it's static.
    fireblast709 I will try that, ill reply if it worked
     
  5. Offline

    mine-care

  6. Offline

    NonameSL

    fireblast709 , But I am not. I am using a string for the method, and then calling the other method with the string. I don't understand what you're trying to say, should I have a map of all configs?
     
  7. Offline

    fireblast709

    NonameSL ok let's walk through your code, perhaps that will clear things up.
    1. You call getPlayerConfig(p.getName())
    2. getPlayerConfig(name) will find the player's File, create if it doesn't exist, and create a new YamlConfiguration object with the file
    3. In this very YamlConfiguration instance, you set the value of 'path' to 'object'
    4. Then, you call saveConfig(p.getName())
    5. saveConfig does the same as steps 2 and 3, but without the 'path' set to 'object' since you are working with a completely different YamlConfiguration reference
    6. Then it will load the file from disk (again, and wiping any data you could have set when reusing the previous config object)
    7. Lastly you save the file, which is just the clean file you loaded (without any new content)
    Now, how can we solve this.
    • Firstly, you should not load the file in the save method (referring to line 22 of the second snippet)
    • Secondly, you should store the YamlConfiguration you got from the first call to getPlayerConfig(p.getName()) in a local variable (line 1 of the first snippet). Like so
      Code:java
      1. YamlConfiguration config = getPlayerConfig(p.getName());
      2. config.set('path', object)
    • Last, but definitely not least, you should use that 'config' variable (as shown in the snippet above) to save the config to file. I would recommend:
      • Change the saveConfig(String config) to saveConfig(YamlConfiguration config, String playerName)
      • (minor change: getPlayerFile(playerName) instead of getPlayerFile(config))
      • Scrap the getPlayerConfig() from line 20, second snippet
      • Call .save(File) on the config object from the parameters
     
  8. Offline

    NonameSL

    fireblast709 Thank you, so so much! I created a set and addDefault with the save method IN them, and now everything works perfect.

    fireblast709 Now I can't get objects from the config. T_T why?
    Main.getInstance().getPlayerConfig(player).getString("loadout."+nautName); returns null

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

    fireblast709

    NonameSL Don't forget to save the YamlConfiguration after you have edited the YamlConfiguration.
     
  10. Offline

    NonameSL

    fireblast709 I am, the writing part works fine, and the config shows what I write into it. The problem is in getting the text.
     
  11. Offline

    fireblast709

    NonameSL Well in the YamlConfiguration object that getPlayerConfig() returns, "loadout."+nautName does not exist. Does it exist in the file you saved?
     
  12. Offline

    NonameSL

    fireblast709 Well, actually I already fixed the problem, apparently I was saving with nornal-case and getting with lowercase. Thanks tho!
     
Thread Status:
Not open for further replies.

Share This Page