[Util] Per- Player Data

Discussion in 'Resources' started by Miclebrick, Jan 23, 2014.

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

    Miclebrick

    For those of you who need a simple way of creating file saved data per-player, this is for you.
    Requirements:
    -Knowledge of simple Java
    -Knowledge of how to make a plugin
    First, add this class file to your plugin:
    Code:java
    1. package me.miclebrick;
    2.  
    3. import java.io.File;
    4. import java.io.IOException;
    5. import java.util.*;
    6.  
    7. import org.bukkit.configuration.file.FileConfiguration;
    8. import org.bukkit.configuration.file.YamlConfiguration;
    9.  
    10. public class PlayerHelper {
    11. private static String name;
    12. public PlayerHelper(String par1PluginName)
    13. {
    14. this.name = par1PluginName;
    15. }
    16. public static String getPlayerPath(String par1Name)
    17. {
    18. return "plugins/"+name+"/userdata/" +par1Name.toLowerCase()+".yml";
    19. }
    20. public static void createNewData(String par1Name)
    21. {
    22. File f = new File(getPlayerPath(par1Name));
    23. if(f.exists())
    24. {
    25. try {
    26. f.createNewFile();
    27. FileConfiguration pDat = YamlConfiguration.loadConfiguration(f);
    28. //Save data for the player. Example:
    29. //pDat.set("path.to.object", value);
    30. pDat.save(f);
    31. } catch (IOException e) {
    32. e.printStackTrace();
    33. }
    34. }
    35. }
    36. public static FileConfiguration getPlayerConfigAsYAML(String par1Name)
    37. {
    38. File f = new File(getPlayerPath(par1Name));
    39. createNewData(par1Name);
    40. return YamlConfiguration.loadConfiguration(f);
    41. }
    42. public static void setPlayerObject(String par1Name, String par2Path, Object par3Value)
    43. {
    44. try
    45. {
    46. File f = new File(getPlayerPath(par1Name));
    47. createNewData(par1Name);
    48. FileConfiguration pDat = YamlConfiguration.loadConfiguration(f);
    49. pDat.set(par2Path, par3Value);
    50. pDat.save(f);
    51. } catch (IOException e) {
    52. e.printStackTrace();
    53. }
    54. }
    55. public static Object getPlayerObject(String par1Name, String par2Path)
    56. {
    57. File f = new File(getPlayerPath(par1Name));
    58. createNewData(par1Name);
    59. FileConfiguration pDat = YamlConfiguration.loadConfiguration(f);
    60. return pDat.get(par2Path);
    61. }
    62. public static boolean checkIfPlayerHasData(String par1Name)
    63. {
    64. File f = new File(getPlayerPath(par1Name));
    65. if(!f.exists()) return false;
    66. else return true;
    67. }
    68. }

    To give players their own data, do something like this.
    Code:java
    1.  
    2. @EventHandler
    3. public void createNewData(PlayerLoginEvent event)
    4. {
    5. PlayerHelper ph = new PlayerHelper("YourPluginName");
    6. ph.createNewData(event.getPlayer().getName());
    7. }

    That would make it so that whenever a player logs in, it checks if the directory "plugins/YourPluginName/userdata/PlayerName.yml" exists. If it doesn't, then it creates that file, and adds your specified defaults to it. To get and set a player value, use ph.setPlayerObject() and ph.getPlayerObject().
    Example:
    Code:java
    1.  
    2. PlayerHelper ph = new PlayerHelper("YourPluginName");
    3. boolean doSomething = (boolean) ph.getPlayerObject("PlayerName", "do.something");
    4. if(doSomething)
    5. {
    6. ph.setPlayerObject("PlayerName", "do.something", false);
    7. }
    8. ItemStack is = new ItemStack(Material.DIAMOND, 1);
    9. ph.setPlayerObject("PlayerName", "DIAMOND.item", is);

    Feel free to add custom methods and mess around with the class!
    Also, if you find any errors, please tell me and I'll fix it.
     
  2. Offline

    macguy8

    You probably should cache instead of reloading the file every time...
     
    DevRosemberg and bobacadodl like this.
  3. Offline

    Miclebrick

    macguy8
    True, but then you wouldn't be able to change the config and make the effects go into the game without reloading or restarting ... could you?
     
  4. Offline

    DevRosemberg

    Miclebrick What do you mean? If you are trying to create a per player stats, ive already done this. Find it in my started threads.
     
  5. Offline

    macguy8

    DevRosemberg You do the same thing as he does In the fact of not caching it.

    Miclebrick Correct. However, most plugins don't allow you to live-edit, and in my opinion that lack of live-editing is something worth losing for the efficiency you'd gain by doing that

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

Share This Page