Solved Creating per Player config in new map

Discussion in 'Plugin Development' started by Unica, Sep 14, 2014.

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

    Unica

    Hi,

    Im trying to create a .yml file for every player to easier read data.
    I tried alot, but nothing seems to be working. If one could help me / give me a good link to a recourse / tutorial that would be great.

    What I need:
    • Reloading per player .yml
    • Reloading entire PlayerData folder (all .yml's)
    • Saving per player .yml file
    • Create a folder PlayerData in plugin.getDataFolder() [Solved by myself]
    • Getting per player .yml file
    Current Code
    Code:java
    1. import java.io.File;
    2. import java.io.IOException;
    3.  
    4. import org.bukkit.configuration.file.YamlConfiguration;
    5. import org.bukkit.entity.Player;
    6.  
    7. public class PlayerData {
    8.  
    9. File file;
    10. YamlConfiguration playerData;
    11.  
    12. private Strikerz m;
    13.  
    14. public PlayerData(Strikerz instance) {
    15. m = instance;
    16. }
    17.  
    18. public void saveData(Player p) {
    19. try {
    20. playerData.save(file);
    21. } catch (IOException e) {
    22. e.printStackTrace();
    23. }
    24. }
    25.  
    26. @SuppressWarnings("static-access")
    27. public void reloadData(Player p) {
    28. playerData.loadConfiguration(file);
    29. }
    30.  
    31. @SuppressWarnings("static-access")
    32. public void createData(Player p) {
    33. File dir = new File("PlayerData");
    34. if (!dir.exists()) {
    35. System.out.println("Error: Could not find PlayerData folder");
    36. dir.mkdir();
    37. } else {
    38. file = new File(m.getDataFolder()+ "/" + dir + File.separator, p.getUniqueId().toString()
    39. + ".yml");
    40. try {
    41. file.createNewFile();
    42. } catch (IOException e) {
    43. e.printStackTrace();
    44. }
    45. playerData = new YamlConfiguration();
    46. playerData.loadConfiguration(file);
    47. }
    48. }
    49.  
    50. @SuppressWarnings("static-access")
    51. public YamlConfiguration getPlayerData(Player p) {
    52. if (file == null) {
    53. createData(p);
    54. }
    55. return playerData.loadConfiguration(file);
    56. }
    57.  
    58. }


    Obvious error (I understand the error)
    Code:
    [15:21:07 WARN]: java.io.IOException: The system cannot find the path specified
    [15:21:07 WARN]:        at java.io.WinNTFileSystem.createFileExclusively(Native
    Method)
    [15:21:07 WARN]:        at java.io.File.createNewFile(Unknown Source)
    [15:21:07 WARN]:        at me.dubehh.Main.PlayerData.createData(PlayerData.java:
    43)
    [15:21:07 WARN]:        at me.dubehh.Listeners.PlayerChange.onJoin(PlayerChange.
    java:20)
    [15:21:07 WARN]:        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native M
    ethod)
    [15:21:07 WARN]:        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown S
    ource)
    [15:21:07 WARN]:        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unkno
    wn Source)
    [15:21:07 WARN]:        at java.lang.reflect.Method.invoke(Unknown Source)
    [15:21:07 WARN]:        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(Jav
    aPluginLoader.java:292)
    [15:21:07 WARN]:        at org.bukkit.plugin.RegisteredListener.callEvent(Regist
    eredListener.java:62)
    [15:21:07 WARN]:        at org.bukkit.plugin.SimplePluginManager.fireEvent(Simpl
    ePluginManager.java:501)
    [15:21:07 WARN]:        at org.bukkit.plugin.SimplePluginManager.callEvent(Simpl
    ePluginManager.java:486)
    [15:21:07 WARN]:        at net.minecraft.server.v1_7_R3.PlayerList.c(PlayerList.
    java:251)
    [15:21:07 WARN]:        at net.minecraft.server.v1_7_R3.PlayerList.a(PlayerList.
    java:138)
    [15:21:07 WARN]:        at net.minecraft.server.v1_7_R3.LoginListener.c(LoginLis
    tener.java:76)
    [15:21:07 WARN]:        at net.minecraft.server.v1_7_R3.LoginListener.a(LoginLis
    tener.java:42)
    [15:21:07 WARN]:        at net.minecraft.server.v1_7_R3.NetworkManager.a(Network
    Manager.java:160)
    [15:21:07 WARN]:        at net.minecraft.server.v1_7_R3.ServerConnection.c(Sourc
    eFile:134)
    [15:21:07 WARN]:        at net.minecraft.server.v1_7_R3.MinecraftServer.v(Minecr
    aftServer.java:667)
    [15:21:07 WARN]:        at net.minecraft.server.v1_7_R3.DedicatedServer.v(Dedica
    tedServer.java:260)
    [15:21:07 WARN]:        at net.minecraft.server.v1_7_R3.MinecraftServer.u(Minecr
    aftServer.java:558)
    [15:21:07 WARN]:        at net.minecraft.server.v1_7_R3.MinecraftServer.run(Mine
    craftServer.java:469)
    [15:21:07 WARN]:        at net.minecraft.server.v1_7_R3.ThreadServerApplication.
    run(SourceFile:628)
     
  2. Offline

    mrCookieSlime

    Unica
    You have to create all Folders first before creating the File.
    Example:
    File folder = new File("plugins/folder/players");
    folder.mkdirs();
    File file = new File("plugins/folder/players/Moewe1.yml");
    file.createNewFile();
     
    mine-care likes this.
  3. Offline

    mine-care

    its easy to fix, also why there is static access warning?
     
  4. Offline

    Unica

    mrCookieSlime mine-care

    Hi, I created the folder + the config.
    How would I
    • Save the config (for one player only)
    • Reload the config (for one player only)
    • Get the config (I already have that I believe)
     
  5. Offline

    mrCookieSlime

    Unica
    config.save(file);
    just load a new config Object from that File
    ? FileConfiguration config = YamlConfiguration.loadConfiguration(file);
     
    mine-care likes this.
  6. Offline

    Unica

    Hey,
    I tried that. I made this, and now it doesn't even make the folder anymore, what am I doing wrong?

    Code:java
    1.  
    2. public class PlayerData {
    3.  
    4. YamlConfiguration playerData;
    5.  
    6. private Strikerz m;
    7.  
    8. public PlayerData(Strikerz instance) {
    9. m = instance;
    10. }
    11.  
    12. public void saveData(Player p) {
    13. try {
    14. playerData.save(getPlayerFile(p));
    15. } catch (IOException e) {
    16. e.printStackTrace();
    17. }
    18. }
    19.  
    20. @SuppressWarnings("static-access")
    21. public void reloadData(Player p) {
    22. playerData.loadConfiguration(getPlayerFile(p));
    23. saveData(p);
    24. }
    25.  
    26. @SuppressWarnings("static-access")
    27. private void createData(Player p) {
    28. File dir = new File(m.getDataFolder().getAbsolutePath() + File.separator + "PlayerData");
    29. if (!dir.exists()) {
    30. System.out.println("Error: Could not find PlayerData folder");
    31. System.out.println("Creating one for you.");
    32. dir.mkdir();
    33. createData(p);
    34. } else {
    35. File file = new File(dir, p.getUniqueId().toString()
    36. + ".yml");
    37. try {
    38. file.createNewFile();
    39. } catch (IOException e) {
    40. e.printStackTrace();
    41. }
    42. playerData = new YamlConfiguration();
    43. playerData.loadConfiguration(file);
    44. playerData.set("Exp", 0);
    45. playerData.set("Kills", 0);
    46. playerData.set("Deaths", 0);
    47. playerData.set("CruxHits", 0);
    48. try {
    49. playerData.save(file);
    50. } catch (IOException e) {
    51. e.printStackTrace();
    52. }
    53. }
    54. }
    55.  
    56. @SuppressWarnings("static-access")
    57. public YamlConfiguration getPlayerData(Player p) {
    58. if (getPlayerFile(p).exists()) {
    59. createData(p);
    60. }
    61. return playerData.loadConfiguration(getPlayerFile(p));
    62. }
    63.  
    64. private File getPlayerFile(Player p){
    65. File folder = new File(m.getDataFolder().getAbsolutePath() + File.separator + "PlayerData");
    66. return new File(folder, p.getUniqueId().toString());
    67. }
    68.  
    69. }

    mrCookieSlime
     
  7. Offline

    4thegame3

    To reload a file you need to reassign the variable
    File file = new File(dir, "playername.yml");
     
  8. Offline

    mrCookieSlime

  9. Offline

    4thegame3

    ups i said variable D:
     
  10. Offline

    mrCookieSlime

    4thegame3
    Ups I said Object :O

    It is a Variable after all...
    Actually, an Object is something else, I just dont know why I always call Variables Objects...
     
  11. Offline

    Unica

    mrCookieSlime 4thegame3

    I did that right?
    Code:java
    1. return new File(folder, p.getUniqueId().toString());
    2.  


    So I returned a 'new file'
    and I reloaded that?
     
  12. Offline

    mrCookieSlime

    Unica
    First of all you probably want it like p.getUniqueId().toString + ".yml" or something along those lines...
    Then just create a brand new Config Object of of it.
     
  13. Offline

    Unica

    mrCookieSlime

    What do you mean with new Config object ?
    I have to return a file right?
    Could u give me a pseudo code?
     
  14. Offline

    mrCookieSlime

    Unica
    File file = new File(path);
    FileConfiguration config = YamlConfiguration.loadConfiguration(file);

    Use that on reloading the Config, also what do you need a reloading method for anyway??
     
  15. Offline

    Unica

    mrCookieSlime

    Okay, the folder is creating now.
    Now I need to know how to
    • Save
    • Reload
    • Get
    The configuration

    EDIT:
    Ninja'd, let me test
     
  16. Offline

    mrCookieSlime

    Unica
    0_0 I already posted you how to save/get/reload the Config before...

    Save:

    Reload:

    Get:
     
  17. Offline

    Unica

    mrCookieSlime
    Code:java
    1. public class PlayerData {
    2.  
    3. private Strikerz m;
    4.  
    5. public PlayerData(Strikerz instance) {
    6. m = instance;
    7. }
    8.  
    9. public void saveData(Player p) {
    10. File file = new File(getDirectory(), p.getUniqueId().toString()+".yml");
    11. YamlConfiguration config = YamlConfiguration.loadConfiguration(file);
    12. try {
    13. config.save(file);
    14. } catch (IOException e) {
    15. e.printStackTrace();
    16. }
    17. }
    18.  
    19. @SuppressWarnings("static-access")
    20. private void createData(Player p) {
    21. File file = new File(getDirectory(), p.getUniqueId().toString() + ".yml");
    22. try {
    23. file.createNewFile();
    24. } catch (IOException e) {
    25. e.printStackTrace();
    26. }
    27. YamlConfiguration playerData = new YamlConfiguration();
    28. playerData.loadConfiguration(file);
    29. playerData.set("Exp", 0);
    30. playerData.set("Kills", 0);
    31. playerData.set("Deaths", 0);
    32. playerData.set("CruxHits", 0);
    33. try {
    34. playerData.save(file);
    35. } catch (IOException e) {
    36. e.printStackTrace();
    37. }
    38. }
    39.  
    40. public YamlConfiguration getPlayerData(Player p) {
    41. File file = new File(getDirectory(), p.getUniqueId().toString()+".yml");
    42. if(!file.exists()){
    43. createData(p);
    44. }
    45. YamlConfiguration config = YamlConfiguration.loadConfiguration(file);
    46. return config;
    47. }
    48.  
    49. private File getDirectory(){
    50. File dir = new File(m.getDataFolder().getAbsolutePath() + File.separator + "PlayerData");
    51. if(!dir.exists()){
    52. dir.mkdir();
    53. }
    54. return dir;
    55. }
    56.  
    57. }
    58.  


    What am I missing??
     
  18. Offline

    mrCookieSlime

    Unica
    A Lot...

    First of all your saveData() method is completely useless...
    Your basically getting the data from a file and saving the exact same data to that file again ?.?

    But createData() and getPlayerData() are just fine. Only saveData() is useless...
     
  19. Offline

    Unica

    mrCookieSlime
    Code:java
    1. public void saveData(Player p) {
    2. File file = new File(getDirectory(), p.getUniqueId().toString()+".yml");
    3. try {
    4. getPlayerData(p).save(file);
    5. } catch (IOException e) {
    6. e.printStackTrace();
    7. }
    8. }


    Im really confused. Sorry if I am being 'dumb', but I've never used this kind of config.
    What is the correct form?
     
  20. Offline

    mrCookieSlime

    Unica
    You do not need such a saveData() method at all. Those are the Methods you probably want to have:

    createData(Player p);
    getData(Player p);
    setValue(Player p, String key, Object value);
    getValue(Player p, String key);
    and maybe:
    containsValue(Player p, String key);

    reloading/saving is just useless. Just save it whenever you change something.
     
  21. Offline

    Unica

    mrCookieSlime
    The saving doesn't even work.
    Code:java
    1. m.player_data.getPlayerData(p).set("Test", "Test");
    2. YamlConfiguration.loadConfiguration(m.player_data.getPlayerFile(p));

    Doesn't do anything.

    Code:java
    1. public File getPlayerFile(Player p){
    2. return new File(getDirectory(), p.getUniqueId().toString()+".yml"); //Return new File(blabla)
    3. }


    Why doesn't this save? "reloading/saving is just useless. Just save it whenever you change something."
    That's what I am trying to do. For setting values I need to save too
     
  22. Offline

    mrCookieSlime

    Unica
    You didn't read what I wrote...

    You probably want a setValue() method which includes saving...

    EDIT: Here's a Config Class which I'm using for my Plugins:

    Code:java
    1. public class Config {
    2.  
    3. File file;
    4. FileConfiguration config;
    5.  
    6. public Config(File configFile) {
    7. this.file = configFile;
    8. this.config = YamlConfiguration.loadConfiguration(this.file);
    9. }
    10.  
    11. public File getFile() {
    12. return this.file;
    13. }
    14.  
    15. public FileConfiguration getConfiguration() {
    16. return this.config;
    17. }
    18.  
    19. public void setValue(String path, Object value) {
    20. config.set(path, value);
    21. try {
    22. config.save(file);
    23. } catch (IOException e) {
    24. }
    25. }
    26.  
    27. public boolean setDefaultValue(String path, Object value) {
    28. if (!contains(path)) {
    29. config.set(path, value);
    30. try {
    31. config.save(file);
    32. return true;
    33. } catch (IOException e) {
    34. return false;
    35. }
    36. }
    37. else {
    38. return false;
    39. }
    40. }
    41.  
    42. public boolean contains(String path) {
    43. return config.contains(path);
    44. }
    45.  
    46. public Object getValue(String path) {
    47. return config.get(path);
    48. }
    49.  
    50. public ItemStack getItem(String path) {
    51. return config.getItemStack(path);
    52. }
    53.  
    54. public String getRandomStringfromList(String path) {
    55. return getStringList(path).get(new Random().nextInt(getStringList(path).size()));
    56. }
    57.  
    58. public int getRandomIntfromList(String path) {
    59. return getIntList(path).get(new Random().nextInt(getIntList(path).size()));
    60. }
    61.  
    62. public String getString(String path) {
    63. return config.getString(path);
    64. }
    65.  
    66. public int getInt(String path) {
    67. return config.getInt(path);
    68. }
    69.  
    70. public boolean getBoolean(String path) {
    71. return config.getBoolean(path);
    72. }
    73.  
    74. public List<String> getStringList(String path) {
    75. return config.getStringList(path);
    76. }
    77.  
    78. public List<Integer> getIntList(String path) {
    79. List<Integer> ints = new ArrayList<Integer>();
    80. for (String string: config.getStringList(path)) {
    81. ints.add(Integer.parseInt(string));
    82. }
    83. return ints;
    84. }
    85.  
    86. public void createFile() {
    87. try {
    88. this.file.createNewFile();
    89. } catch (IOException e) {
    90. }
    91. }
    92.  
    93. public Location getLocation(String path) {
    94. return new Location(
    95. Bukkit.getWorld(
    96. config.getString(path + ".world")),
    97. config.getDouble(path + ".x"),
    98. config.getDouble(path + ".y"),
    99. config.getDouble(path + ".z")
    100. );
    101. }
    102.  
    103. }


    just do Config cfg = new Config(file);
    cfg.setValue("Test", "Test");

    It will automatically save it for you.
     
  23. Offline

    Unica

    mrCookieSlime

    I did read what u wrote, my point is. I have to save the data anyway..
    Code:java
    1. public void setValue(Player p, String path, Object o){
    2. getPlayerData(p).set(path, o); //Set values
    3. File file = new File(getDirectory(), p.getUniqueId().toString()+".yml"); //Get the file from the directory
    4. try {
    5. getPlayerData(p).save(file); //save the file
    6. } catch (IOException e) {
    7. e.printStackTrace();
    8. }
    9.  
    10. }


    The file doesn't add up.
    If you say the getter is right, than
    getPlayerData(p) should return the config, and then I could save the file right?
     
  24. Offline

    mrCookieSlime

    Unica
    Correct. Your setValue method should work.
     
  25. Offline

    Unica

    mrCookieSlime

    Doesn't work.
    Nothing get's saved.

    mrCookieSlime

    I got it to working.
    YamlConfiguration#loadConfiguration was needed in order to save everything.
    Thanks for your help

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 14, 2016
Thread Status:
Not open for further replies.

Share This Page