Solved Unable to create a method to save data

Discussion in 'Plugin Development' started by sniddunc, Apr 23, 2016.

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

    sniddunc

    Hello,

    I am having trouble saving player files with a method that I created. Each player has a file created for them on their first join, but this does not actually save.

    I have a method to get the FileConfiguration of a player, a method to get their data file, and another to take both of these and save it. However, the problem I'm having is the data files won't save.

    PHP:
    public static File getDataFile(Player player) {
            return new 
    File(plugin.getDataFolder() + File.separator "userdata"player.getUniqueId() + ".yml");
        }

        public static 
    FileConfiguration getData(Player player) {
            return 
    YamlConfiguration.loadConfiguration(getDataFile(player));
        }

        public static 
    boolean saveData(Player player) {
            
    FileConfiguration data Data.getData(player);
            
    File dataFile Data.getDataFile(player);

            try {
                
    data.save(dataFile);
                return 
    true;
            } catch (
    IOException e) {
                
    Bukkit.broadcastMessage(ChatColor.RED "Could not save " player.getUniqueId() + ".yml!");
                
    e.printStackTrace();
                return 
    false;
            }
        }
    Both getData and getDataFile work appropriately, but to save them I need to do a save from scratch. For example, in my player listener class I need to do this:

    PHP:
    if (!pdataFile.exists()) {
                try {
                    
    pdataFile.createNewFile();
                   
                    
    pdata YamlConfiguration.loadConfiguration(pdataFile);
                   
                    
    Data.setDefaults(pdataplayer);
                   
                    
    pdata.save(pdataFile);
                    
    // For all other uses, this would need to be surrounded by it's own try/catch making it redundant.
                   
                
    } catch (IOException e) {
                    
    Bukkit.broadcastMessage(ChatColor.RED "Failed to create player data file");
                    
    e.printStackTrace();
                }
               
            }
    But I would rather just use:
    Code:
    Data.saveData(player);
    But for some reason, no matter where it's used this method does not work and I am getting very confused as to why. I have spent over 6 hours trying to fix this to no avail, so I could use a hand.

    Any help is greatly appreciated.

    Thank you!
     
  2. you write something to the FileConfiguration from getData(Player)

    all that information is stored in exactly this FileConfiguration object.
    but in the saveData method you get a new FileConfiguration that doesnt contain all those informations.

    How to solve it? Just add a FileConfiguration parameter to saveData
     
  3. Offline

    sniddunc

    Wow, thanks! That was such a simple fix yet I failed to notice it. Thank you!
     
Thread Status:
Not open for further replies.

Share This Page