Users.java 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package com.gmail.nossr50.util;
  2. import java.io.File;
  3. import java.io.FileWriter;
  4. import java.io.IOException;
  5. import java.util.HashMap;
  6. import java.util.Map;
  7. import org.bukkit.OfflinePlayer;
  8. import org.bukkit.entity.Player;
  9. import com.gmail.nossr50.mcMMO;
  10. import com.gmail.nossr50.datatypes.PlayerProfile;
  11. public class Users {
  12. private static Map<String, PlayerProfile> profiles = new HashMap<String, PlayerProfile>();
  13. /**
  14. * Load users.
  15. */
  16. public static void loadUsers() {
  17. new File(mcMMO.flatFileDirectory).mkdir();
  18. new File(mcMMO.leaderboardDirectory).mkdir();
  19. new File(mcMMO.usersFile).createNewFile();
  20. if (!theDir.exists()) {
  21. try {
  22. FileWriter writer = new FileWriter(theDir);
  23. writer.close();
  24. }
  25. catch (IOException e) {
  26. e.printStackTrace();
  27. }
  28. }
  29. }
  30. /**
  31. * Add a new user.
  32. *
  33. * @param player The player to create a user record for
  34. * @return the player's profile
  35. */
  36. public static PlayerProfile addUser(Player player) {
  37. String playerName = player.getName();
  38. PlayerProfile playerProfile = profiles.get(playerName);
  39. if (playerProfile != null) {
  40. //The player object is different on each reconnection and must be updated
  41. playerProfile.setPlayer(player);
  42. }
  43. else {
  44. playerProfile = new PlayerProfile(player, playerName, true);
  45. profiles.put(playerName, playerProfile);
  46. }
  47. return playerProfile;
  48. }
  49. /*
  50. * Remove a user.
  51. *
  52. * @param playerName The name of the player to remove
  53. */
  54. public static void remove(String playerName) {
  55. profiles.remove(playerName);
  56. }
  57. /**
  58. * Clear all users.
  59. */
  60. public static void clearAll() {
  61. profiles.clear();
  62. }
  63. /*
  64. * Save all users.
  65. */
  66. public static void saveAll() {
  67. for (PlayerProfile playerProfile : profiles.values()) {
  68. playerProfile.save();
  69. }
  70. }
  71. /**
  72. * Get all PlayerProfiles.
  73. *
  74. * @return a HashMap containing the PlayerProfile of everyone in the database
  75. */
  76. public static Map<String, PlayerProfile> getProfiles() {
  77. return profiles;
  78. }
  79. /**
  80. * Get the profile of a player.
  81. *
  82. * @param player The player whose profile to retrieve
  83. * @return the player's profile
  84. */
  85. public static PlayerProfile getProfile(OfflinePlayer player) {
  86. return getProfile(player.getName());
  87. }
  88. /**
  89. * Get the profile of a player by name.
  90. *
  91. * @param player The name of the player whose profile to retrieve
  92. * @return the player's profile
  93. */
  94. public static PlayerProfile getProfile(String playerName) {
  95. return profiles.get(playerName);
  96. }
  97. }