Config in wrong folder?

Discussion in 'Plugin Development' started by recon88, Nov 24, 2012.

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

    recon88

    This
    Code:java
    1.  
    2.  
    3. File friends = new File(this.getDataFolder(),"friends.yml");
    4. FileConfiguration cfg = YamlConfiguration.loadConfiguration(friends);
    5.  
    6. cfg.save(friends);
    7.  

    is creating the friends.yml in the main folder of the sever instead of the plugin folder.
    If I print the folder with
    Code:
    SystemOutprintln(this.getDataFolder());
    it returns the full plugin path... But if I try something like
    Code:
    File friends = new File(this.getDataFolder() + "friends.yml");
    it returns null + friends.yml instead of the folder + friends.yml
    Why?
     
  2. when you do at your exampl
    Code:java
    1. File friends = new File(this.getDataFolder() , "friends.yml");

    instead of
    Code:java
    1. File friends = new File(this.getDataFolder() + "friends.yml");

    what is the result then?
     
  3. Offline

    recon88

    See first post?
     
  4. Offline

    fireblast709

    Code:java
    1.  
    2. if(!this.getDataFolder().exists() || !this.getDataFolder().isDirectory())
    3. {
    4. if(this.getDataFolder().mkdir())
    5. {
    6. this.getLogger().info("data folder made");
    7. }
    8. else
    9. {
    10. this.getLogger().warning("Failed to create data folder ");
    11. }
    12. }
    13. File friends = new File(this.getDataFolder(), "friends.yml");
    14. YamlConfiguration cfg = YamlConfiguration.loadConfiguration(friends);
    15. if(cfg == null)
    16. {
    17. cfg = new YamlConfiguration();
    18. }
    19. cfg.save(friends);

    Correct way of creating your own files
     
  5. Offline

    Sagacious_Zed Bukkit Docs

    You never need to check if the datafolder exists and manually create it. YamlConfiguraiton.loadConfiuration and YamlConfiguration.save can handle those details correctly. One returning a empty, and the other saving into the correct place overwriting the file of the same name if there.

    It would seem you are calling getDataFolder before your plugin has been initialized. move those calls into onLoad or onEnable, or methods that are called from those.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
Thread Status:
Not open for further replies.

Share This Page