User Data files not correctly saving

Discussion in 'Spigot Plugin Development' started by ryu, May 12, 2017.

  1. ryu

    ryu

    Problem:
    The user data is either not saving or is not changing in the user's files.

    Extra info:
    Let me put it like this: I have a class file where I am creating a custom user ban, and I'm trying to save the file with the specified information after said a user gets banned on the server. But currently, I'm running into the issue of not knowing whether or not the items are changing in the user's file or the file isn't correctly saving for some apparent reason.

    Code:
    Code (Text):
    Main instance = Main.getInstance();


        public FileConfiguration getUserdata(Player player) {
            File file = new File(instance.getDataFolder() + File.separator + "userdata" + File.separator, player.getUniqueId() + ".yml");
            return YamlConfiguration.loadConfiguration(file);
        }

        private ArrayList<File> getAllUserData() {
            ArrayList<File> files = new ArrayList<>();
            for (String name : dir.list()) {
                if(!name.endsWith(".yml")) {
                    continue;
                }
                files.add(new File(dir, name));
            }
            return files;
        }

        public void saveUserData(Player player) {
            File file = new File(instance.getDataFolder() + File.separator + "userdata" + File.separator, player.getUniqueId() + ".yml");
            FileConfiguration fileConfiguration = YamlConfiguration.loadConfiguration(file);
            try {
                fileConfiguration.save(file);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        public void reloadAll() {
            for (File u : getAllUserData()) {
                YamlConfiguration.loadConfiguration(u);
            }
        }

    }
    Code (Text):
                    UserData.getInstance().getUserdata(player.getPlayer()).set("settings.banned.enable", true);

                    UserData.getInstance().getUserdata(player.getPlayer()).set("settings.banned.reason", "The Ban Hammer has spoken!");
                    UserData.getInstance().getUserdata(player.getPlayer()).set("settings.banned.banned-by", sender.getName());
                    UserData.getInstance().saveUserData(player.getPlayer());
     
  2. Why are you making a brand new file in your save method? You should just be adding a File parameter to your method and calling the save method on that,

    Make a public File, and a public FileConfiguration, and then use those instead of making new ones throughout your code.

    (They can be private though if you don't wish for other classes to access them)

    Also what is File.separator? My bad if it is just my lack of understanding but to make a new section inside a configuration section I just usually place a (.) where I want the new section to go.

    EDIT:

    Just figured out that the separator method gets the default separator for the filesystem. Interesting, I've never seen that being used before in configs though.
     
    #2 BourneDev, May 12, 2017
    Last edited: May 12, 2017
  3. ryu

    ryu

    The reason towards me creating a new file method is towards the fact that I'm creating a per user.yml file like shown here:
    [​IMG]
    I would love to use the method you're talking about but I can't seem to figure out a way to be able to get the specified users UUID and their .yml file without doing so. I currently have an idea I can try from what you said but I'm not 100% certain this will work.
     
  4. You'd have to store the files in a hashmap with the player's UUID as the key. Then when saving, just get the file with the player's uuid as the key.

    What are you doing to edit the values in the player configs? Are you doing it in code, or just editing files manually with notepad or something?
     
  5. Ooooor, instead of all this hassle. Just create an object which will represent the user data. When getUserdata(player) is called, check if they are contained in a map where you store each player's object with their UUID. If they aren't, call a private method which will grab the data from the file, and create a user data object from the data gotten from the file, add this to the map and return it. However, if their uuid is contained in the map, just return the object associated with that uuid. Ez pz.