Solved YAML Issue, just can't figure out what's wrong myself..

Discussion in 'Plugin Development' started by MrMag518, Mar 12, 2013.

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

    MrMag518

    So I've gone around in my code the past two days debugging my code to figure out what's wrong with my code, but I just can't figure it out.

    I'm trying to make multi-world YAML config handling. It works creating the world folders and creating the file inside those folders, it also works saving the files.

    However, when I'm trying to write into the files, it won't work at all, nothing is being written, neither with file.addDefault() and file.set().

    Here's my class:

    Code:java
    1.  
    2.  
    3. package com.mrmag518.iSafe.Files;
    4.  
    5. import com.mrmag518.iSafe.Util.Log;
    6.  
    7. import java.io.File;
    8. import java.io.IOException;
    9. import java.util.logging.Level;
    10. import java.util.logging.Logger;
    11.  
    12. import org.bukkit.Bukkit;
    13. import org.bukkit.World;
    14. import org.bukkit.configuration.file.FileConfiguration;
    15. import org.bukkit.configuration.file.YamlConfiguration;
    16.  
    17. public class Blacklist {
    18. public static void manageBlacklistDir() {
    19. for(World world : Bukkit.getWorlds()) {
    20. String worldname = world.getName();
    21.  
    22. if(worldname == null) {
    23. Log.debug("Could not load worldname: " + worldname + ", because it is null.");
    24. return;
    25. }
    26.  
    27. File blacklistDir = new File("plugins/iSafe/Blacklists");
    28. File worldDir = new File(blacklistDir + "/" + worldname);
    29. File blacklistFile = new File(worldDir + "/blacklist.yml");
    30.  
    31. if(!blacklistDir.exists()) {
    32. blacklistDir.mkdir();
    33. }
    34.  
    35. if(!worldDir.exists()) {
    36. worldDir.mkdir();
    37. }
    38.  
    39. if(!blacklistFile.exists()) {
    40. try {
    41. blacklistFile.createNewFile();
    42. } catch (IOException ex) {
    43. Logger.getLogger(Blacklist.class.getName()).log(Level.SEVERE, "Error creating " + blacklistFile + " :(", ex);
    44. }
    45. }
    46. }
    47. }
    48.  
    49. public static void loadBlacklists() {
    50. for(World world : Bukkit.getWorlds()) {
    51. String worldname = world.getName();
    52.  
    53. getBlacklist(worldname).options().header("test"); // Wont' write
    54.  
    55. getBlacklist(worldname).addDefault("test", true); // Wont' write
    56. getBlacklist(worldname).set("set-test", false); // Wont' write
    57.  
    58. getBlacklist(worldname).options().copyDefaults(true);
    59. saveBlacklist(worldname);
    60. }
    61. //saveBlacklists();
    62. }
    63.  
    64. public static FileConfiguration getBlacklist(String worldname) {
    65. File blacklistFile = new File("plugins/iSafe/Blacklists/" + worldname + "/blacklist.yml");
    66. FileConfiguration blacklist = null;
    67.  
    68. if(blacklistFile.exists() || blacklistFile != null) {
    69. blacklist = YamlConfiguration.loadConfiguration(blacklistFile);
    70. }
    71.  
    72. if(blacklist == null) {
    73. return null;
    74. }
    75.  
    76. return blacklist;
    77. }
    78.  
    79. public static void saveBlacklist(String worldname) {
    80. File blacklistFile = new File("plugins/iSafe/Blacklists/" + worldname + "/blacklist.yml");
    81.  
    82. if(!blacklistFile.exists() || blacklistFile == null) {
    83. return;
    84. }
    85.  
    86. try {
    87. getBlacklist(worldname).save(blacklistFile);
    88. } catch (IOException ex) {
    89. Logger.getLogger(Blacklist.class.getName()).log(Level.SEVERE, "Error saving blacklist file " + blacklistFile, ex);
    90. }
    91. }
    92.  
    93. public static void saveBlacklists() {
    94. for(World world : Bukkit.getWorlds()) {
    95. File blacklistFile = new File("plugins/iSafe/Blacklists/" + world.getName() + "/blacklist.yml");
    96.  
    97. if(!blacklistFile.exists() || blacklistFile == null) {
    98. return;
    99. }
    100.  
    101. try {
    102. getBlacklist(world.getName()).save(blacklistFile);
    103. } catch (IOException ex) {
    104. Logger.getLogger(Blacklist.class.getName()).log(Level.SEVERE, "Error saving blacklist file " + blacklistFile, ex);
    105. }
    106. }
    107. }
    108. }
    109.  
    110.  
     
  2. Offline

    evilmidget38

    MrMag518 Your check to see if the world name is null would throw a NPE if the World was actually null, you should probably change that.

    As for why your BlackList isn't saving, it's actually rather simple. Every time you call getBlackList(String) you're reloading the FileConfiguration object. You need to keep a reference to the FileConfiguration and use the same object, rather than creating a new one every time you change it.

    Basically:
    Code:
    // Rather than
    getBlackList(world).set("stuff", null);
    // Do something like
    FileConfiguration blackList = getBlackList(world);
    // Do some stuff with it.
    blackList.set("stuff", null);
    // Then save it, making sure to use the same object(this means you need to change your saveBlackList(String) method as well.
    blackList.save(blackListFile);
     
    MrMag518 likes this.
  3. Offline

    MrMag518

    Omg thank you so much, I can't belive it was that simple!

    /solved
     
Thread Status:
Not open for further replies.

Share This Page