DatabaseManager.java 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package com.gmail.nossr50.database;
  2. import com.gmail.nossr50.api.exceptions.InvalidSkillException;
  3. import com.gmail.nossr50.config.Config;
  4. import com.gmail.nossr50.datatypes.database.DatabaseType;
  5. import com.gmail.nossr50.datatypes.database.PlayerStat;
  6. import com.gmail.nossr50.datatypes.player.PlayerData;
  7. import com.gmail.nossr50.datatypes.player.PlayerProfile;
  8. import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
  9. import org.bukkit.entity.Player;
  10. import org.jetbrains.annotations.NotNull;
  11. import org.jetbrains.annotations.Nullable;
  12. import java.util.List;
  13. import java.util.Map;
  14. import java.util.UUID;
  15. public interface DatabaseManager {
  16. // One month in milliseconds
  17. long PURGE_TIME = 2630000000L * Config.getInstance().getOldUsersCutoff();
  18. // During convertUsers, how often to output a status
  19. int progressInterval = 200;
  20. /**
  21. * Purge users with 0 power level from the database.
  22. */
  23. void purgePowerlessUsers();
  24. /**
  25. * Purge users who haven't logged on in over a certain time frame from the database.
  26. */
  27. void purgeOldUsers();
  28. /**
  29. * Remove a user from the database.
  30. *
  31. * @param playerName The name of the user to remove
  32. * @param uuid player UUID, can be null
  33. * @return true if the user was successfully removed, false otherwise
  34. */
  35. boolean removeUser(String playerName, UUID uuid);
  36. /**
  37. * Removes any cache used for faster lookups
  38. * Currently only used for SQL
  39. * @param uuid target UUID to cleanup
  40. */
  41. void cleanupUser(UUID uuid);
  42. /**
  43. * Save a user to the database.
  44. *
  45. * @param playerData target player data
  46. * @return true if successful, false on failure
  47. */
  48. boolean saveUser(@NotNull PlayerData playerData);
  49. /**
  50. * Retrieve leaderboard info.
  51. * Will never be null but it may be empty
  52. *
  53. * @param skill The skill to retrieve info on
  54. * @param pageNumber Which page in the leaderboards to retrieve
  55. * @param statsPerPage The number of stats per page
  56. * @return the requested leaderboard information
  57. */
  58. @NotNull List<PlayerStat> readLeaderboard(@Nullable PrimarySkillType skill, int pageNumber, int statsPerPage) throws InvalidSkillException;
  59. /**
  60. * Retrieve rank info into a HashMap from PrimarySkillType to the rank.
  61. * <p>
  62. * The special value <code>null</code> is used to represent the Power
  63. * Level rank (the combination of all skill levels).
  64. *
  65. * @param playerName The name of the user to retrieve the rankings for
  66. * @return the requested rank information
  67. */
  68. Map<PrimarySkillType, Integer> readRank(String playerName);
  69. /**
  70. * Add a new user to the database.
  71. *
  72. * @param playerName The name of the player to be added to the database
  73. * @param uuid The uuid of the player to be added to the database
  74. */
  75. void newUser(String playerName, UUID uuid);
  76. @NotNull PlayerProfile newUser(@NotNull Player player);
  77. /**
  78. * Load a player from the database.
  79. *
  80. * @param playerName The name of the player to load from the database
  81. * @return The player's data, or an unloaded PlayerProfile if not found
  82. * and createNew is false
  83. */
  84. @NotNull PlayerProfile loadPlayerProfile(@NotNull String playerName);
  85. /**
  86. * Load a player from the database.
  87. *
  88. * @param uuid The uuid of the player to load from the database
  89. * @return The player's data, or an unloaded PlayerProfile if not found
  90. */
  91. @NotNull PlayerProfile loadPlayerProfile(@NotNull UUID uuid, @Nullable String playerName);
  92. /**
  93. * Get all users currently stored in the database.
  94. *
  95. * @return list of playernames
  96. */
  97. List<String> getStoredUsers();
  98. /**
  99. * Convert all users from this database to the provided database using
  100. * {@link #saveUser(PlayerProfile)}.
  101. *
  102. * @param destination The DatabaseManager to save to
  103. */
  104. void convertUsers(DatabaseManager destination);
  105. boolean saveUserUUID(String userName, UUID uuid);
  106. boolean saveUserUUIDs(Map<String, UUID> fetchedUUIDs);
  107. /**
  108. * Retrieve the type of database in use. Custom databases should return CUSTOM.
  109. *
  110. * @return The type of database
  111. */
  112. DatabaseType getDatabaseType();
  113. /**
  114. * Called when the plugin disables
  115. */
  116. void onDisable();
  117. }