FileConfiguration isn't working right

Discussion in 'Plugin Development' started by Diamondminer77, Apr 22, 2014.

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

    Diamondminer77

    Hi. I'm developing a plugin for a server, but I've noticed that file configuration won't work right. This will work:
    Code:java
    1. File player = new File(getDataFolder() + "/players/whatever.yml");
    2.  
    3. FileConfiguration config = new YamlConfiguration().loadConfiguration(player);
    4. config.set("Hey", "Hi");
    5. try {
    6. config.save(player);
    7. } catch (IOException e) {
    8. getLogger().info("Could not save file");
    9. }

    But this won't:
    Code:java
    1. public FileConfiguration getFile() {
    2. File player = new File(getDataFolder() + "/players/whatever.yml");
    3. @SuppressWarnings("static-access")
    4. FileConfiguration config = new YamlConfiguration().loadConfiguration(player);
    5. return config;
    6. }
    7.  
    8. public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    9. if(label.equalsIgnoreCase("Beta)) {
    10. getFile().set("Hi", "Hey");
    11. //Then save the file
    12. }
    13. }


    I think that the problem is the return statement, because it's pretty much the EXACT same code. When the config is returned, does it reset the YamlConfiguration().loadConfiguration(player) method?

    I forgot to put a quotation mark at the end of "Beta, but it's in my code.

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

    clmcd42

    The first one works while the second one doesn't because you use getFile().set(...) and then i assume you go on to use getFile().save() in order to save it. Every time you use getFile(), you are creating a new instance of the config. This means when you use getFile().save(), you are saving a blank config file. To fix the problem, make a FileConfiguration config = getFile(); and do config.set(...) and config.save() in order to save it. This will save the same instance into one variable so that you are just using that instance for the actions. I believe that is your problem.
     
  3. Offline

    Diamondminer77

    When I use getFile().set(...) I saved it using getFile().save(new File("The File Location")), so it's saving the right file, if that's what you're saying.

    I think I understand what you mean. This is the code I'm using.
    Code:
    public FileConfiguration getPlayer(String UUID) {
                        File player = new File(getDataFolder() + "/players/" + UUID + ".yml");
                       
                        if(!player.exists()) {
                            try {
                                player.createNewFile();
                            } catch (IOException e) {
                                getLogger().info("Could not create " + UUID);
                            }
                        }
                       
                        FileConfiguration config = YamlConfiguration.loadConfiguration(player);
                       
                        return config;
                    }
                   
                    public void savePlayer(String UUID) {
                        try {
                            File player = new File(getDataFolder() + "/players/" + UUID + ".yml");
                           
                            if(!player.exists()) {
                                try {
                                    player.createNewFile();
                                } catch (IOException e) {
                                    getLogger().info("Could not create " + UUID);
                                }
                            }
                           
                            getPlayer(UUID).save(new File(getDataFolder() + "/players/" + UUID + ".yml"));
                        } catch (IOException e) {
                            getLogger().info("Could not save file");
                        }
                    }
    It creates the file but nothings in it.
    Note: Before it worked half way but merged some files, so I tried to change it. I have several places where I have the getPlayer() and savePlayer() methods, so I don't want to have to rewrite every single one of them, so if this makes sense, I want to keep the "syntax" the same.

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

    clmcd42

    I see the problem. What happens is when you use getPlayer(uuid).set(String, Object) is that it only sets that to a certain instance of the config variable. Then, the instance of that config disappears without being saved, and then you use savePlayer(). You're going to have to change some stuff around to fix this. How to fix this:

    1. Change the parameter for savePlayer() to what I have in the example below.
    2. In your savePlayer() method, just user config.save(new File(blah blah blah)).
    3. Whenever you use the config, save it to a variable. I'll provide an example so you know what I mean.

    What your getPlayer() and savePlayer() methods should be changed to:
    Code:java
    1. public FileConfiguration getPlayer(String UUID) {
    2. File player = new File(getDataFolder() + "/players/" + UUID + ".yml");
    3. if(!player.exists()) {
    4. try {
    5. player.createNewFile();
    6. } catch (IOException e) {
    7. getLogger().info("Could not create " + UUID);
    8. }
    9. }
    10. FileConfiguration config = YamlConfiguration.loadConfiguration(player);
    11. return config;
    12. }
    13.  
    14. public void savePlayer(FileConfiguration config, String UUID) {
    15. config.save(new File(getDataFolder() + "/players/" + UUID + ".yml");
    16. }


    And then, to utilize this in your code:
    Code:java
    1. FileConfiguration config = getPlayer(player.getUniqueId());
    2. config.set("key", "value");
    3. savePlayer(config, player.getUniqueId());


    This way the instance of the config is never lost. You will have to change around the code you already have though to add this, but it's the only way I can see to make this work.
     
  5. Offline

    Diamondminer77

    Thanks, I'll try that out and get back to you
     
  6. Offline

    Diamondminer77

    clmcd42 Thanks! It works great!
     
Thread Status:
Not open for further replies.

Share This Page