Solved Need help with writing it down to a file

Discussion in 'Plugin Development' started by Catchet, Oct 21, 2013.

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

    Catchet

    Hello, I need some help with writing in to this file. Every player on the server has their own file (playerName.yml). It works fine when creating files and getting numbers/letters from them, but for some reason I can't write anything in to them. I can do it manually but there is not really a point in doing so. I have another file called "data.yml" but it works perfectly fine to write stuff in to it. Here's my code for getting and saving the file: (and writing in to it if I uncomment the stuff)
    Code:java
    1.  
    2. FileConfiguration udata;
    3. File ufile;
    4. File udatafolder;
    5. udatafolder = new File(p.getDataFolder() + File.separator + "userdata");
    6. public FileConfiguration getUData(Player player) {
    7. ufile = new File(udatafolder + File.separator + player.getName() + ".yml");
    8. udata = YamlConfiguration.loadConfiguration(ufile);
    9. //Bukkit.broadcastMessage("§6§n" + udata.getInt(player.getName() + ".copper"));
    10. //udata.set(player.getName() + ".copper", player.getName() + ".copper" + 1);
    11. //saveUData(player);
    12. return udata;
    13. }
    14. public void saveUData(Player player) {
    15. ufile = new File(udatafolder + File.separator + player.getName() + ".yml");
    16. udata = YamlConfiguration.loadConfiguration(ufile);
    17. try {
    18. udata.save(ufile);
    19. Bukkit.getServer().getLogger().info("Saved " + player.getName() + ".yml");
    20. }
    21. catch (IOException e) {
    22. Bukkit.getServer().getLogger().severe(ChatColor.RED + "Could not save "
    23. + player.getName() + ".yml in 'userdata'");
    24. }
    25. }

    It doesn't save anything I write in to the file (even if I use saveUData(player)). So if I would set the variable copper to 5 manually then set it to copper + copper and then print out copper it would still print out 5 even if I saved between.

    Sorry if this was too long. I really appreciate help or advise, thank you.
     
  2. Offline

    waicool20

    So your saving a bunch of players with their values? Why make so many files when you can store all of them in one file
     
  3. Offline

    Catchet

    waicool20
    I think this is easier to work with and I originally had a good reason, because else I wouldn't have changed the way I'm storing the userdata.
     
  4. Offline

    waicool20

    Actually I think you should just use a bufferedwriter and write to the files directly
     
  5. Offline

    Catchet

    waicool20
    Do you mind telling me how to
    waicool20
    Nevermind, I found out how, now I have two questions:
    1. Does the BufferedWriter work with ints and longs too?
    2. Isn't it easier to just use udata.get(...) and udata.set(...)?

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

    waicool20

    1.Yes bufferedwriter can write anything you can put into a string, then you can retrieve it with a bufferedreader and Integer.parseInt or Long.parseLong

    2.Yes it is easier but its always more customizable if you write something yourself,not to mention you know it'll work if you know what your coding

    Look at my tutorial on saving locations, you can easily change it to player data if your smart
    https://forums.bukkit.org/threads/t...ormation-in-a-csv-like-database-style.185420/
     
  7. Offline

    Catchet

    waicool20
    Thank you, I got it to work with the bufferedwriters and bufferedreaders.
     
Thread Status:
Not open for further replies.

Share This Page