Creating a new file on playerLogin

Discussion in 'Plugin Development' started by Gopaintman, Sep 7, 2013.

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

    Gopaintman

    Hi,
    So I need to create .yml files for each player that joins. I have my event. All I need to do is have a .yml file created with their name and then generate it using a Template in the jar.
     
  2. Offline

    sharp237

    Look at this.
     
  3. Offline

    DevRosemberg

    Take a look at this:

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


    Join Event:

    Code:java
    1. @EventHandler
    2. public void onPlayerJoin(PlayerJoinEvent e) {
    3. final Player p = e.getPlayer();
    4. this.plugin.fileUtil.createFile(p);
    5. }


    Hope it helps.
     
    Gopaintman likes this.
  4. Offline

    Gopaintman

    Yea, I know how to make custom files, just getting it to make new ones over and over again I didn't get.

    DevRosemberg Exactly what I wanted! Thank you! However would you the method that checks if the Players folder contains the player's name?

    **Edit found it :p

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

    DevRosemberg

  6. Offline

    Gopaintman

    Would adding in a setKills like this cause a problem?
    Code:java
    1. public void setKills(String p, int newAmount) {
    2. File pFile = new File(this.plugin.getDataFolder(), "Players/" + p.toLowerCase() + ".yml");
    3. FileConfiguration pConfig = YamlConfiguration.loadConfiguration(pFile);
    4. pConfig.set("Kills", Integer.valueOf(newAmount));
    5. try {
    6. pConfig.save(pFile);
    7. } catch (Exception e) {
    8. }
    9. }
     
  7. Offline

    DevRosemberg

Thread Status:
Not open for further replies.

Share This Page