UserManager.java 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package com.gmail.nossr50.util.player;
  2. import java.util.Collection;
  3. import java.util.HashMap;
  4. import java.util.Map;
  5. import java.util.Set;
  6. import org.bukkit.OfflinePlayer;
  7. import org.bukkit.entity.Player;
  8. import com.gmail.nossr50.mcMMO;
  9. import com.gmail.nossr50.datatypes.player.McMMOPlayer;
  10. public final class UserManager {
  11. private final static Map<String, McMMOPlayer> players = new HashMap<String, McMMOPlayer>();
  12. private UserManager() {};
  13. /**
  14. * Add a new user.
  15. *
  16. * @param player The player to create a user record for
  17. * @return the player's {@link McMMOPlayer} object
  18. */
  19. public static McMMOPlayer addUser(Player player) {
  20. String playerName = player.getName();
  21. McMMOPlayer mcMMOPlayer = players.get(playerName);
  22. if (mcMMOPlayer != null) {
  23. mcMMOPlayer.setPlayer(player); // The player object is different on each reconnection and must be updated
  24. }
  25. else {
  26. mcMMOPlayer = new McMMOPlayer(player);
  27. players.put(playerName, mcMMOPlayer);
  28. }
  29. return mcMMOPlayer;
  30. }
  31. /**
  32. * Remove a user.
  33. *
  34. * @param playerName The name of the player to remove
  35. */
  36. public static void remove(String playerName) {
  37. players.remove(playerName);
  38. }
  39. /**
  40. * Clear all users.
  41. */
  42. public static void clearAll() {
  43. players.clear();
  44. }
  45. /**
  46. * Save all users.
  47. */
  48. public static void saveAll() {
  49. for (McMMOPlayer mcMMOPlayer : players.values()) {
  50. mcMMOPlayer.getProfile().save();
  51. }
  52. }
  53. public static Set<String> getPlayerNames() {
  54. return players.keySet();
  55. }
  56. public static Collection<McMMOPlayer> getPlayers() {
  57. return players.values();
  58. }
  59. /**
  60. * Get the McMMOPlayer of a player by name.
  61. *
  62. * @param playerName The name of the player whose McMMOPlayer to retrieve
  63. * @return the player's McMMOPlayer object
  64. */
  65. public static McMMOPlayer getPlayer(String playerName) {
  66. return retrieveMcMMOPlayer(playerName, false);
  67. }
  68. /**
  69. * Get the McMMOPlayer of a player.
  70. *
  71. * @param player The player whose McMMOPlayer to retrieve
  72. * @return the player's McMMOPlayer object
  73. */
  74. public static McMMOPlayer getPlayer(OfflinePlayer player) {
  75. return retrieveMcMMOPlayer(player.getName(), false);
  76. }
  77. public static McMMOPlayer getPlayer(String playerName, boolean offlineValid) {
  78. return retrieveMcMMOPlayer(playerName, offlineValid);
  79. }
  80. private static McMMOPlayer retrieveMcMMOPlayer(String playerName, boolean offlineValid) {
  81. McMMOPlayer mcMMOPlayer = players.get(playerName);
  82. if (mcMMOPlayer == null) {
  83. Player player = mcMMO.p.getServer().getPlayerExact(playerName);
  84. if (player == null) {
  85. if (!offlineValid) {
  86. mcMMO.p.getLogger().warning("A valid mcMMOPlayer object could not be found for " + playerName + ".");
  87. }
  88. return null;
  89. }
  90. mcMMOPlayer = new McMMOPlayer(player);
  91. players.put(playerName, mcMMOPlayer);
  92. }
  93. return mcMMOPlayer;
  94. }
  95. }