Solved Custom configuration not saving?

Discussion in 'Plugin Development' started by Wizehh, Mar 17, 2014.

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

    Wizehh

    Before you ask, yes - I have spent many hours 'Googling' this.
    Anyway, on to the problem:
    I've created a singleton class to handle ".yml" files for each individual player. I can read from the files, but for some inexplicable reason, I cannot set values to the files, as shown below:
    PHP:
    PlayerDataManager pdm PlayerDataManager.getInstance();
    //'name' is my variable for the player's name, and 'p' is the player.
    pdm.getFile(name).set("warps." args[1] + ".world"p.getLocation().getWorld().getName());
    pdm.getFile(name).set("warps." args[1] + ".x"p.getLocation().getX());
    pdm.getFile(name).set("warps." args[1] + ".y"p.getLocation().getY());
    pdm.getFile(name).set("warps." args[1] + ".z"p.getLocation().getZ());
    pdm.getFile(name).set("warpnumb"pdm.getFile(name).getInt("warpnumb") + 1);
    pdm.saveFile(name);
    p.sendMessage(ChatColor.GREEN "Set warp as " args[1]);
    There is no exception thrown, at all, and it does send a message to the player. This aspect lead me to surmise that the values *do* set, but they are not actually saved. Also, after execution of the command, Npp++ does indeed tell me that the file has been modified:
    [​IMG]

    Code that may be of interest:
    PHP:
    //PlayerDataManager class
     
    // Check if the directory contains the file
    public boolean containsPlayer(String name) {
        
    File playerFile = new File(dir.getPath() + "\\" name ".yml");
        return 
    playerFile.exists();
    }
     
    // Returns the file itself
    public File getHardFile(String playerName) {
        if (!
    containsPlayer(playerName)) return null;
        for (
    File filedir.listFiles()) {
            if (
    file.getName().equalsIgnoreCase(playerName ".yml")) {
                return 
    file;
            }
        }
        return 
    null;
    }
     
    // Returns the YamlConfiguration
    public FileConfiguration getFile(String playerName) {
        
    FileConfiguration config YamlConfiguration.loadConfiguration(getHardFile(playerName));
        try {
            
    config.save(getHardFile(playerName));
        } catch (
    IOException e) {
            
    e.printStackTrace();
        }
        return 
    config;
    }
     
    //Problematic section?
    public void saveFile(String playerName) {
        try {
            
    //FileConfiguration config = getFile(playerName);
            //config.save(getHardFile(playerName));
            
    getFile(playerName).save(getHardFile(playerName));
        } catch (
    IOException e) {
            
    e.printStackTrace();
        }
    }
    I apologize for such a lengthy post, but I would appreciate any help that I could get.
     
  2. Offline

    PoweredByRage

    I wish I could possibly help you <3.
     
  3. Offline

    Brendyn Todd

    Wizehh Give me about an hour and I can solve this, I'm at school at the moment so I don't have time to work it out yet.
     
  4. Offline

    Alshain01

    Wizehh

    Don't do this:
    Code:java
    1. File playerFile = new File(dir.getPath() + "\\" + name + ".yml");


    Do this:
    Code:java
    1. File playerFile = new File(dir.getPath() + File.separator + name + ".yml");


    Or this:
    Code:java
    1. File playerFile = new File(dir.getPath(), name + ".yml");


    Your slashes are OS dependent, so it may work in Windows, but not Linux. That's not your problem, I'm still looking but that's a big one.


    EDIT: OK, I'm not really seeing anything wrong. If Notepad++ is showing that message, it means the file was written to. If you forgot to save (which you didn't) it wouldn't give you that. You might try adding some debugging.
     
  5. Offline

    Wizehh

    Maybe the file is being reset?
     
  6. Offline

    Alshain01

    Wizehh Oh wait, I see whats wrong. Your retrieving the file each time and then not saving it. Then your retrieving it again and then saving without modifying it. You need to store your file configuration. Then do your sets and save.

    Code:java
    1. FileConfiguration config = pdm.getFile(name);
    2. config.set(...);
    3. config.set(...);
    4. pdm.save(...);


    It's going to take a re-engineering of that. You should create your pdm instance by passing in the name and creating the file inside the PlayerDataManager.
     
  7. Offline

    Wizehh

    Is that really the only way?
     
  8. Offline

    Alshain01

    Wizehh Well the problem is that pdm.getFile(name) is returning a new FileConfiguration instance. Each time you invoke that method, it's a new one, not the one from the last line. Likewise, pdm.saveFile(name) in grabbing new instance as well. So each invocation is a different instance of the file and saveFile is saving an unmodified instance. You need to re-engineer it so your always using the same object. That is the only way.
     
  9. Offline

    Wizehh

    Ah. So it would be something along these lines?
    PHP:
    PlayerData = new PlayerData(name);
    And in that 'PlayerData' class, there would only be one file?
     
  10. Offline

    Alshain01

    Exactly. You can create as many of those as you like though, so multiple names won't be a problem.
     
  11. Offline

    Wizehh

    Alshain01
    -edit-
    Never mind, I fixed the problem. Thank you very much for your help!
     
Thread Status:
Not open for further replies.

Share This Page