Users.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 final static mcMMO plugin = mcMMO.p;
  12. public static HashMap<String, PlayerProfile> players = new HashMap<String, PlayerProfile>();
  13. /**
  14. * Load users.
  15. */
  16. public static void loadUsers() {
  17. new File(plugin.flatFileDirectory).mkdir();
  18. new File(plugin.leaderboardDirectory).mkdir();
  19. File theDir = new File(plugin.usersFile);
  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. */
  35. public static void addUser(Player player) {
  36. if (!players.containsKey(player.getName().toLowerCase())) {
  37. players.put(player.getName().toLowerCase(), new PlayerProfile(player.getName(), true));
  38. }
  39. }
  40. /**
  41. * Clear all users.
  42. */
  43. public static void clearUsers() {
  44. players.clear();
  45. }
  46. /**
  47. * Get all PlayerProfiles.
  48. *
  49. * @return a HashMap containing the PlayerProfile of everyone in the database
  50. */
  51. public static HashMap<String, PlayerProfile> getProfiles() {
  52. return players;
  53. }
  54. /**
  55. * Remove a user from the database.
  56. *
  57. * @param player The player to remove
  58. */
  59. public static void removeUser(Player player) {
  60. //Only remove PlayerProfile if user is offline and we have it in memory
  61. if (!player.isOnline() && players.containsKey(player.getName().toLowerCase())) {
  62. players.get(player.getName().toLowerCase()).save();
  63. players.remove(player.getName().toLowerCase());
  64. }
  65. }
  66. /**
  67. * Remove a user from the DB by name.
  68. *
  69. * @param playerName The name of the player to remove
  70. */
  71. public static void removeUserByName(String playerName) {
  72. players.remove(playerName.toLowerCase());
  73. }
  74. /**
  75. * Get the profile of a player.
  76. *
  77. * @param player The player whose profile to retrieve
  78. * @return the player's profile
  79. */
  80. public static PlayerProfile getProfile(OfflinePlayer player) {
  81. return getProfileByName(player.getName());
  82. }
  83. /**
  84. * Get the profile of a player by name.
  85. *
  86. * @param player The name of the player whose profile to retrieve
  87. * @return the player's profile
  88. */
  89. public static PlayerProfile getProfileByName(String playerName) {
  90. if (plugin.getServer().getOfflinePlayer(playerName).isOnline() || players.containsKey(playerName.toLowerCase())) {
  91. if (players.containsKey(playerName.toLowerCase())) {
  92. return players.get(playerName.toLowerCase());
  93. }
  94. else {
  95. players.put(playerName.toLowerCase(), new PlayerProfile(playerName, true));
  96. return players.get(playerName.toLowerCase());
  97. }
  98. }
  99. else {
  100. return new PlayerProfile(playerName, false);
  101. }
  102. }
  103. }