Solved Can't Load YamlConfigurationFile

Discussion in 'Plugin Development' started by McStormify, Apr 7, 2013.

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

    McStormify

    Here is my code. When I enable the plugin, the console says "[Unicraft] Couldn't load file."

    What's going aahnn?

    Code:java
    1. package me.McStormify.Unicraft;
    2.  
    3. import java.io.File;
    4. import java.io.IOException;
    5.  
    6. import org.bukkit.configuration.file.FileConfiguration;
    7. import org.bukkit.configuration.file.YamlConfiguration;
    8.  
    9. public class PlayerData {
    10. private Unicraft uc;
    11.  
    12. private File folder;
    13. private File file;
    14. private FileConfiguration playerData;
    15.  
    16. public PlayerData(Unicraft uc) {
    17. this.uc = uc;
    18. }
    19.  
    20. public FileConfiguration load() {
    21. folder = new File("plugins/Unicraft/");
    22. file = new File(folder, "playerdata.yml");
    23.  
    24. playerData = new YamlConfiguration();
    25.  
    26. if (folder.exists() == false) {
    27. try {
    28. folder.mkdir();
    29. } catch (Exception e) {
    30. uc.l.info("[Unicraft] Couldn't create folder.");
    31. }
    32. }
    33.  
    34. if (file.exists() == false) {
    35. try {
    36. file.createNewFile();
    37. } catch (Exception e) {
    38. uc.l.info("[Unicraft] Couldn't create file.");
    39. }
    40. }
    41.  
    42. try {
    43. playerData.load(file);
    44. } catch (Exception e) {
    45. uc.l.info("[Unicraft] Couldn't load file.");
    46. }
    47.  
    48. return playerData;
    49. }
    50.  
    51. public void save() {
    52. try {
    53. playerData.save(file);
    54. uc.l.info("[Unicraft] Successfully saved playerdata file.");
    55. } catch (IOException e) {
    56. uc.l.info("[Unicraft] Couldn't save playerdata file.");
    57. }
    58. }
    59. }


    Sorry for the double post, but I don't want to edit my original one, editing screws up the code formatting :p

    I tried folder = uc.getDataFolder(); but it returns null.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  2. Offline

    spywhere

    You don't have to create any file or folder. Just check the existing of the file and load it. If you want to save any changes just use save method, the file will created for you.

    Code:
    FileConfiguration yml=new YamlConfiguration();
    File file = new File(uc.getDataFolder().toString(), "config.yml");
    if(file.exists()){
        yml.load(file);
        //Read config
    }
    //Write config
     
    yml.save(file);
    Make sure that your uc field is your plugin that currently running (not a new instance)
     
Thread Status:
Not open for further replies.

Share This Page