Users.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 org.bukkit.OfflinePlayer;
  7. import org.bukkit.entity.Player;
  8. import com.gmail.nossr50.McMMO;
  9. import com.gmail.nossr50.datatypes.PlayerProfile;
  10. public class Users {
  11. private static HashMap<Player, PlayerProfile> players = new HashMap<Player, PlayerProfile>();
  12. /**
  13. * Load users.
  14. */
  15. public static void loadUsers() {
  16. new File(McMMO.flatFileDirectory).mkdir();
  17. new File(McMMO.leaderboardDirectory).mkdir();
  18. File theDir = new File(McMMO.usersFile);
  19. if (!theDir.exists()) {
  20. try {
  21. FileWriter writer = new FileWriter(theDir);
  22. writer.close();
  23. }
  24. catch (IOException e) {
  25. e.printStackTrace();
  26. }
  27. }
  28. }
  29. /**
  30. * Add a new user.
  31. *
  32. * @param player The player to create a user record for
  33. */
  34. public static void addUser(Player player) {
  35. if (!players.containsKey(player)) {
  36. players.put(player, new PlayerProfile(player, true));
  37. }
  38. }
  39. /**
  40. * Clear all users.
  41. */
  42. public static void clearUsers() {
  43. players.clear();
  44. }
  45. /**
  46. * Get all PlayerProfiles.
  47. *
  48. * @return a HashMap containing the PlayerProfile of everyone in the database
  49. */
  50. public static HashMap<Player, PlayerProfile> getProfiles() {
  51. return players;
  52. }
  53. /**
  54. * Remove a user from the database.
  55. *
  56. * @param player The player to remove
  57. */
  58. public static void removeUser(Player player) {
  59. //Only remove PlayerProfile if user is offline and we have it in memory
  60. if (!player.isOnline() && players.containsKey(player)) {
  61. players.remove(player);
  62. }
  63. }
  64. /**
  65. * Remove a user from the DB by name.
  66. *
  67. * @param playerName The name of the player to remove
  68. */
  69. public static void removeUserByName(String playerName) {
  70. players.remove(McMMO.p.getServer().getOfflinePlayer(playerName));
  71. }
  72. /**
  73. * Get the profile of a player.
  74. *
  75. * @param player The player whose profile to retrieve
  76. * @return the player's profile
  77. */
  78. public static PlayerProfile getProfile(OfflinePlayer player) {
  79. return players.get(player);
  80. }
  81. /**
  82. * Get the profile of a player by name.
  83. *
  84. * @param player The name of the player whose profile to retrieve
  85. * @return the player's profile
  86. */
  87. public static PlayerProfile getProfileByName(String playerName) {
  88. Player player = McMMO.p.getServer().getPlayer(playerName);
  89. PlayerProfile profile = players.get(player);
  90. if (profile == null) {
  91. if (player != null) {
  92. PlayerProfile newProfile = new PlayerProfile(player, true);
  93. players.put(player, newProfile);
  94. return newProfile;
  95. }
  96. else {
  97. McMMO.p.getLogger().severe("getProfileByName(" + playerName + ") just returned null :(");
  98. for (StackTraceElement ste : new Throwable().getStackTrace()) {
  99. System.out.println(ste);
  100. }
  101. return null;
  102. }
  103. }
  104. else {
  105. return profile;
  106. }
  107. }
  108. }