Users.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package com.gmail.nossr50;
  2. import java.io.File;
  3. import java.io.FileWriter;
  4. import java.io.IOException;
  5. import java.util.HashMap;
  6. import org.bukkit.entity.Player;
  7. import com.gmail.nossr50.datatypes.PlayerProfile;
  8. public class Users {
  9. private static volatile Users instance;
  10. String location = "plugins/mcMMO/FlatFileStuff/mcmmo.users";
  11. String directory = "plugins/mcMMO/FlatFileStuff/";
  12. String directoryb = "plugins/mcMMO/FlatFileStuff/Leaderboards/";
  13. public static HashMap<String, PlayerProfile> players = new HashMap<String, PlayerProfile>();
  14. /**
  15. * Load users.
  16. */
  17. public void loadUsers() {
  18. new File(directory).mkdir();
  19. new File(directoryb).mkdir();
  20. File theDir = new File(location);
  21. if (!theDir.exists()) {
  22. try {
  23. FileWriter writer = new FileWriter(theDir);
  24. writer.close();
  25. }
  26. catch (IOException e) {
  27. e.printStackTrace();
  28. }
  29. }
  30. }
  31. /**
  32. * Add a new user.
  33. *
  34. * @param player The player to create a user record for
  35. */
  36. public static void addUser(Player player) {
  37. if (!players.containsKey(player.getName().toLowerCase())) {
  38. players.put(player.getName().toLowerCase(), new PlayerProfile(player.getName()));
  39. }
  40. }
  41. /**
  42. * Clear all users.
  43. */
  44. public static void clearUsers() {
  45. players.clear();
  46. }
  47. /**
  48. * Get all PlayerProfiles.
  49. *
  50. * @return a HashMap containing the PlayerProfile of everyone in the database
  51. */
  52. public static HashMap<String, PlayerProfile> getProfiles() {
  53. return players;
  54. }
  55. /**
  56. * Remove a user from the database.
  57. *
  58. * @param player The player to remove
  59. */
  60. public static void removeUser(Player player) {
  61. //Only remove PlayerProfile if user is offline and we have it in memory
  62. if (!player.isOnline() && players.containsKey(player.getName().toLowerCase())) {
  63. players.get(player.getName().toLowerCase()).save();
  64. players.remove(player.getName().toLowerCase());
  65. }
  66. }
  67. /**
  68. * Remove a user from the DB by name.
  69. *
  70. * @param playerName The name of the player to remove
  71. */
  72. public static void removeUserByName(String playerName) {
  73. players.remove(playerName.toLowerCase());
  74. }
  75. /**
  76. * Get the profile of an online player.
  77. *
  78. * @param player The player whose profile to retrieve
  79. * @return the player's profile
  80. */
  81. public static PlayerProfile getProfile(Player player) {
  82. if(players.get(player.getName().toLowerCase()) != null) {
  83. return players.get(player.getName().toLowerCase());
  84. }
  85. else {
  86. players.put(player.getName().toLowerCase(), new PlayerProfile(player.getName()));
  87. return players.get(player.getName().toLowerCase());
  88. }
  89. }
  90. /**
  91. * Get the profile of an online player.
  92. *
  93. * @param player The player whose profile to retrieve
  94. * @return the player's profile
  95. */
  96. public static PlayerProfile getProfile(String playerName) {
  97. if(players.get(playerName.toLowerCase()) != null) {
  98. return players.get(playerName.toLowerCase());
  99. }
  100. else {
  101. players.put(playerName.toLowerCase(), new PlayerProfile(playerName));
  102. return players.get(playerName.toLowerCase());
  103. }
  104. }
  105. /**
  106. * Get the profile of an offline player.
  107. *
  108. * @param playerName Name of the player whose profile to retrieve
  109. * @return the player's profile
  110. */
  111. public static PlayerProfile getOfflineProfile(String playerName) {
  112. return new PlayerProfile(playerName, false);
  113. }
  114. /**
  115. * Get an instance of this class.
  116. *
  117. * @return an instance of this class
  118. */
  119. public static Users getInstance() {
  120. if (instance == null) {
  121. instance = new Users();
  122. }
  123. return instance;
  124. }
  125. }