[Util | Tutorial] FileUtil Create per Player Stats with Ease!

Discussion in 'Resources' started by DevRosemberg, Sep 25, 2013.

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

    DevRosemberg

    Hello bukkit users, i have been looking around in the forums for a while and i have seen a lot of people in need of a FileUtil so im just gona give you one i created a while ago for a minigame, it simply creates a file each time a player joins (if he dosent have a file) and inputs data.

    This is the actual fileUtil Class:


    Code:java
    1. public void createFile(Player p) {
    2. File pFileDir = new File(this.plugin.getDataFolder(), "Players");
    3. if (!pFileDir.exists()) {
    4. pFileDir.mkdir();
    5. }
    6. File pFile = new File(this.plugin.getDataFolder(), "Players/" + p.getName().toLowerCase() + ".yml");
    7. if (!pFile.exists())
    8. try {
    9. pFile.createNewFile();
    10. FileConfiguration pConfig = YamlConfiguration.loadConfiguration(pFile);
    11. pConfig.set("User", p.getName());
    12. pConfig.set("Kills", Integer.valueOf(0));
    13. pConfig.set("Deaths", Integer.valueOf(0));
    14. pConfig.save(pFile);
    15. }
    16. catch (Exception e) {
    17. }
    18. }
    19.  
    20. //****************************//
    21.  
    22. public String getPlayerName(String p) {
    23. File pFile = new File(this.plugin.getDataFolder(), "Players/" + p.toLowerCase() + ".yml");
    24. FileConfiguration pConfig = YamlConfiguration.loadConfiguration(pFile);
    25. String name = pConfig.getString("User");
    26. return name;
    27. }
    28.  
    29. //****************************//
    30.  
    31. public boolean fileExists(String p) {
    32. File pFile = new File(this.plugin.getDataFolder(), "Players/" + p.toLowerCase() + ".yml");
    33. if (pFile.exists()) {
    34. return true;
    35. }
    36. return false;
    37. }
    38.  
    39. //****************************//
    40.  
    41. public Integer getKills(String p)
    42. {
    43. File pFile = new File(this.plugin.getDataFolder(), "Players/" + p.toLowerCase() + ".yml");
    44. FileConfiguration pConfig = YamlConfiguration.loadConfiguration(pFile);
    45. int kills = pConfig.getInt("Kills");
    46. return Integer.valueOf(kills);
    47. }
    48.  
    49. //****************************//
    50.  
    51. public void addKill(String p) {
    52. File pFile = new File(this.plugin.getDataFolder(), "Players/" + p.toLowerCase() + ".yml");
    53. FileConfiguration pConfig = YamlConfiguration.loadConfiguration(pFile);
    54. pConfig.set("Kills", Integer.valueOf(pConfig.getInt("Kills") + 1));
    55. try {
    56. pConfig.save(pFile);
    57. } catch (Exception e) {
    58. }
    59. }
    60.  
    61. //****************************//
    62.  
    63. public Integer getDeaths(String p) {
    64. File pFile = new File(this.plugin.getDataFolder(), "Players/" + p.toLowerCase() + ".yml");
    65. FileConfiguration pConfig = YamlConfiguration.loadConfiguration(pFile);
    66. int deaths = pConfig.getInt("Deaths");
    67. return Integer.valueOf(deaths);
    68. }
    69.  
    70. //****************************//
    71.  
    72. public void addDeath(String p) {
    73. File pFile = new File(this.plugin.getDataFolder(), "Players/" + p.toLowerCase() + ".yml");
    74. FileConfiguration pConfig = YamlConfiguration.loadConfiguration(pFile);
    75. pConfig.set("Deaths", Integer.valueOf(pConfig.getInt("Deaths") + 1));
    76. try {
    77. pConfig.save(pFile);
    78. } catch (Exception e) {
    79. }
    80. }
    81.  
    82. //****************************//
    83.  
    84. public Integer getPoints(String p) {
    85. File pFile = new File(this.plugin.getDataFolder(), "Players/" + p.toLowerCase() + ".yml");
    86. FileConfiguration pConfig = YamlConfiguration.loadConfiguration(pFile);
    87. int Points = pConfig.getInt("Points");
    88. return Integer.valueOf(Points);
    89. }
    90.  
    91. //****************************//
    92.  
    93. public void setPoints(String p, int newAmount) {
    94. File pFile = new File(this.plugin.getDataFolder(), "Players/" + p.toLowerCase() + ".yml");
    95. FileConfiguration pConfig = YamlConfiguration.loadConfiguration(pFile);
    96. pConfig.set("Points", Integer.valueOf(newAmount));
    97. try {
    98. pConfig.save(pFile);
    99. } catch (Exception e) {
    100. }
    101. }
    102.  
    103. //****************************//
    104.  
    105. public void addPoints(String p, int amountAdded) {
    106. File pFile = new File(this.plugin.getDataFolder(), "Players/" + p.toLowerCase() + ".yml");
    107. FileConfiguration pConfig = YamlConfiguration.loadConfiguration(pFile);
    108. pConfig.set("Points", Integer.valueOf(pConfig.getInt("Points") + amountAdded));
    109. try {
    110. pConfig.save(pFile);
    111. } catch (Exception e) {
    112. }
    113. }
    114.  
    115. //****************************//
    116.  
    117. public void takePoints(String p, int amountTaken) {
    118. File pFile = new File(this.plugin.getDataFolder(), "Players/" + p.toLowerCase() + ".yml");
    119. FileConfiguration pConfig = YamlConfiguration.loadConfiguration(pFile);
    120. int PointsCurrent = pConfig.getInt("Points");
    121. if (PointsCurrent - amountTaken >= 0) {
    122. int newAmount = PointsCurrent - amountTaken;
    123. pConfig.set("Points", Integer.valueOf(newAmount));
    124. }
    125. try {
    126. pConfig.save(pFile);
    127. } catch (Exception e) {
    128. }
    129. }
    130.  
    131. //****************************//


    So now whena player actually joins you would need to check if he has a file using this:

    Code:java
    1. this.plugin.fileUtil.createFile(player);


    And then when for example a player dies you would do something like this in the actual PlayerDeathEvent

    Code:java
    1. Player victim = (Player) e.getEntity();
    2. Player damager = victim.getKiller();
    3. if (damager instanceof Player && victim instanceof Player) {
    4. this.plugin.fileUtil.addDeath(victim);
    5. this.plugin.fileUtil.addKill(damager);
    6. }


    When you want to retrieve the actual stats of a player you would do:

    Code:java
    1. String name = this.plugin.fileUtil.getPlayerName(p.getName());
    2. int kills = this.plugin.fileUtil.getKills(p.getName()).intValue();
    3. int deaths = this.plugin.fileUtil.getDeaths(p.getName()).intValue();
    4. p.sendMessage("Your stats");
    5. p.sendMessage(ChatColor.GOLD + "Kills: " + kills);
    6. p.sendMessage(ChatColor.GOLD + "Deaths: " + deaths);


    Thanks and i hope this helped you guys.
    DevRo
    Additional Information:
    Every User in Bukkit Forums except for one ChronicNinjaz are allowed to use this FileUtil. Moderators please check the code for KitPvP created by him and if it contains a small part of my fileUtil or something dont let the project be Accepted please. He is not allowed to use it and i will do something if i found
     
    XvBaseballkidvX likes this.
  2. Offline

    user_43347

    Why do you recreate the YamlConfiguration object every load? You're wasting resources. You're also not even notifying the plugin of any exceptions that occur.
     
    desht, ferrybig and Chinwe like this.
  3. Offline

    Chinwe

    ^ And the file object every time too. It would be a lot better if this was in its own class, and each player had their own object, to which you can get/set stats from, eg:
    Code:
    PlayerData data = mapOfPlayerData.get(playerName);
    int kills = data.getKills();
    int deaths = data.getDeaths();
    data.setKills(1337);
    // etc
     
  4. Offline

    filoghost

    Why you're using Integer.valueOf(0)?
     
  5. Offline

    DrTURTLE2

    Code:java
    1. @EventHandler
    2. public void playerDeathMsg(PlayerDeathEvent e) {
    3. Player player = (Player) e.getEntity();
    4. Player killer = player.getKiller();
    5. if (player.getKiller() != null) {
    6. Bukkit.broadcastMessage(ChatColor.GREEN + killer.getName() + ChatColor.DARK_GRAY + " has killed " + ChatColor.GREEN + player.getName());
    7. for (PotionEffect effect : player.getActivePotionEffects())
    8. player.removePotionEffect(effect.getType());
    9.  
    10.  
    11. this.plugin.stats.addDeath(player);
    12. this.plugin.stats.addKill(killer);
    13. }
    14. }
    15.  

    This is my DeathEvent, I'm getting an error under addDeath and addKill.

    Its telling me to change methods or something.

    I don't quite understand.

    DevRosemberg
     
  6. Offline

    CeramicTitan

    This resource was disappointing...

    As well as "this.plugin..." What are you doing? Change the methods to static, and access them statically or create a new instance of the class and call them like that.
     
  7. Offline

    DevRosemberg

    DrTURTLE2 it shouldnt be causing an error. Ill check it later.
     
  8. Offline

    filoghost

    @DrTURTLE Maybe you have forgot to create the profile? Before adding stats, you should use createFile(Player p)
     
  9. Offline

    DevRosemberg

  10. Offline

    se1by

    Why save it in a yml file anyway?
    I'd prefer sqlite or any other db.
     
  11. Offline

    bloodless2010

    This seems pretty good; it would be much better if you stored it into a hashmap then saved every x seconds; which reduce a lot of lag I think
     
  12. Offline

    DevRosemberg

    bloodless2010 Why if it saves it just when you need it?

    se1by It is just to create a Per Player Stats in YML with ease. With a database is also easy (mongoDB).
     
  13. Offline

    bloodless2010

    DevRosemberg Because isn't saving/getting it over and over bad? I may be wrong
     
  14. Offline

    DevRosemberg

  15. Offline

    bloodless2010

    DevRosemberg let me explain again; say if you set this up to record the amount of blocks a player has broke, wouldn't reading the config and saving it over and over frequently bad?
     
  16. Offline

    se1by

    bloodless2010 I would save it in something like a hashmap, and save to the file in the onDisable method.
    Maybe some saving from time to time (e.g. every 10 minutes) would be good in case of a server crash.
     
  17. Offline

    DevRosemberg

  18. Offline

    DrTURTLE2

  19. Offline

    user_43347

    I cannot recommend against using this library enough, it's performance is horrible.

    I don't want to be rude, or disapproving (heck even necroposting on a month old post) but I think this is a half hearted tutorial, one that does not require effort to create nor reason. Please choose a better topic next time?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Sep 21, 2019
  20. Offline

    DevRosemberg

    DrTURTLE2 Are you doing this:

    Code:
    public FileUtil fileUtil;
     
    public void onEnable() {
    this.fileUtil = new FileUtil(this);
    }
    xTrollxDudex

    I created it in around 5 minutes just to help some people around to create simple stats. I mean the util Works correctly but i wouldnt recommend using it.
     
  21. Offline

    DrTURTLE2

  22. Offline

    DevRosemberg

    DrTURTLE2 It works with Strings so it would be: addDeath(victim.getName());
     
  23. Offline

    fls1234

    Thank you so much!
     
  24. Offline

    DevRosemberg

Thread Status:
Not open for further replies.

Share This Page