Class DatabaseManager
The caching system caches teams using TeamProgression
s and keeps a link between each online player and the
associated TeamProgression
. Two players who are part of the same team will always be associated to the same TeamProgression
object.
More formally, the object returned by getTeamProgression(Player)
is the same if and only the players are members of the same team:
By default, players are kept in cache until they quit. However, this behavior can be overridden through theTeamProgression teamP1 = getProgression(playerOne); TeamProgression teamP2 = getProgression(playerTwo); if (teamP1.contains(p2)) { // Players are members of the same team assert teamP1 == teamP2; } else { // Players are in two separate teams assert teamP1 != teamP2; }
loadOfflinePlayer(UUID, CacheFreeingOption)
method,
which forces a player to stay in cache even if they quit. If the player is not online, they'll be loaded.
There is, however, a limit on the maximum amount of requests a plugin can do.
For more information, see getLoadingRequestsAmount(Plugin, UUID, CacheFreeingOption.Option)
.
This class is thread safe.
-
Field Summary
Modifier and TypeFieldDescriptionstatic final int
Max possible loading requests a plugin can make simultaneously per offline player. -
Constructor Summary
ConstructorDescriptionCreates a newDatabaseManager
which uses an in-memory database.DatabaseManager
(@NotNull AdvancementMain main, @NotNull File dbFile) Creates a newDatabaseManager
which uses a SQLite database.DatabaseManager
(@NotNull AdvancementMain main, @NotNull String username, @NotNull String password, @NotNull String databaseName, @NotNull String host, @Range(from=1, to=Integer.MAX_VALUE) int port, @Range(from=1, to=Integer.MAX_VALUE) int poolSize, @Range(from=250L, to=Long.MAX_VALUE) long connectionTimeout) Creates a newDatabaseManager
which uses a MySQL database. -
Method Summary
Modifier and TypeMethodDescriptiongetLoadingRequestsAmount
(@NotNull Plugin plugin, @NotNull UUID uuid, CacheFreeingOption.Option type) Returns the number of currently active loading requests done by a plugin for the specified player with the providedCacheFreeingOption.Option
.getStoredPlayerName
(@NotNull UUID uuid) Gets the in-database stored name of the provided player.getTeamProgression
(@NotNull UUID uuid) Returns theTeamProgression
of the team of the provided player.getTeamProgression
(@NotNull Player player) Returns theTeamProgression
of the team of the provided player.boolean
Returns whether the provided player is loaded into the cache.boolean
isLoaded
(@NotNull OfflinePlayer player) Returns whether the provided offline player is loaded into the cache.boolean
Returns whether the provided player is loaded into the cache.boolean
isLoadedAndOnline
(UUID uuid) Returns whether the provided player is online and loaded into the cache.boolean
isLoadedAndOnline
(@NotNull Player player) Returns whether the provided player is online and loaded into the cache.boolean
isOfflinePlayerLoaded
(UUID uuid) Returns whether at least one loading request is currently active for the specified player.boolean
isOfflinePlayerLoaded
(UUID uuid, Plugin requester) Returns whether at least one loading request, done by the provided plugin, is currently active for the specified player.Returns whether the provided advancement is unredeemed for the specified team.isUnredeemed
(@NotNull AdvancementKey key, @NotNull UUID uuid) Returns whether the provided advancement is unredeemed for the specified player.loadOfflinePlayer
(@NotNull UUID uuid, @NotNull CacheFreeingOption option) Loads the provided player from the database into the caching system.movePlayerInNewTeam
(@NotNull UUID uuid) Moves the provided player into a new team.movePlayerInNewTeam
(@NotNull Player player) Moves the provided player into a new team.setUnredeemed
(@NotNull AdvancementKey key, boolean giveRewards, @NotNull TeamProgression pro) Sets an advancement unredeemed for the specified team.setUnredeemed
(@NotNull AdvancementKey key, boolean giveRewards, @NotNull UUID uuid) Sets an advancement unredeemed for the specified player.void
unloadOfflinePlayer
(@NotNull UUID uuid, @NotNull Plugin requester) Unloads the provided player from the caching system.void
Closes the connection to the database and frees the cache.Unregisters the provided player.Unregisters the provided player.Redeem the specified advancement for the provided team.unsetUnredeemed
(@NotNull AdvancementKey key, @NotNull UUID uuid) Redeem the specified advancement for the provided player.updatePlayerName
(@NotNull Player player) Updates the name of the specified player in the database.updatePlayerTeam
(@NotNull UUID playerToMove, @NotNull UUID otherTeamMember) Moves the provided player from their team to the second player's one.updatePlayerTeam
(@NotNull Player playerToMove, @NotNull TeamProgression otherTeamProgression) Moves the provided player from their team to the specified one.updatePlayerTeam
(@NotNull Player playerToMove, @NotNull Player otherTeamMember) Moves the provided player from their team to the second player's one.int
updateProgression
(@NotNull AdvancementKey key, @NotNull TeamProgression progression, @Range(from=0, to=Integer.MAX_VALUE) int newProgression) Updates the progression of the specified advancement.int
updateProgression
(@NotNull AdvancementKey key, @NotNull UUID uuid, @Range(from=0, to=Integer.MAX_VALUE) int newProgression) Updates the progression of the specified advancement.int
updateProgression
(@NotNull AdvancementKey key, @NotNull Player player, @Range(from=0, to=Integer.MAX_VALUE) int newProgression) Updates the progression of the specified advancement.updateProgressionWithCompletable
(@NotNull AdvancementKey key, @NotNull TeamProgression progression, @Range(from=0, to=Integer.MAX_VALUE) int newProgression) Updates the progression of the specified advancement.updateProgressionWithCompletable
(@NotNull AdvancementKey key, @NotNull UUID uuid, @Range(from=0, to=Integer.MAX_VALUE) int newProgression) Updates the progression of the specified advancement.updateProgressionWithCompletable
(@NotNull AdvancementKey key, @NotNull Player player, @Range(from=0, to=Integer.MAX_VALUE) int newProgression) Updates the progression of the specified advancement.
-
Field Details
-
MAX_SIMULTANEOUS_LOADING_REQUESTS
public static final int MAX_SIMULTANEOUS_LOADING_REQUESTSMax possible loading requests a plugin can make simultaneously per offline player.Limit is applied to automatic and manual requests separately and doesn't apply to requests which don't cache.
- See Also:
-
-
Constructor Details
-
DatabaseManager
Creates a newDatabaseManager
which uses an in-memory database.- Parameters:
main
- TheAdvancementMain
.- Throws:
Exception
- If anything goes wrong.
-
DatabaseManager
public DatabaseManager(@NotNull @NotNull AdvancementMain main, @NotNull @NotNull File dbFile) throws Exception Creates a newDatabaseManager
which uses a SQLite database.- Parameters:
main
- TheAdvancementMain
.dbFile
- The SQLite database file.- Throws:
Exception
- If anything goes wrong.
-
DatabaseManager
public DatabaseManager(@NotNull @NotNull AdvancementMain main, @NotNull @NotNull String username, @NotNull @NotNull String password, @NotNull @NotNull String databaseName, @NotNull @NotNull String host, @Range(from=1, to=Integer.MAX_VALUE) int port, @Range(from=1, to=Integer.MAX_VALUE) int poolSize, @Range(from=250L, to=Long.MAX_VALUE) long connectionTimeout) throws Exception Creates a newDatabaseManager
which uses a MySQL database.- Parameters:
main
- TheAdvancementMain
.username
- The username.password
- The password.databaseName
- The name of the database.host
- The MySQL host.port
- The MySQL port. Must be greater than zero.poolSize
- The pool size. Must be greater than zero.connectionTimeout
- The connection timeout. Must be greater or equal to 250.- Throws:
Exception
- If anything goes wrong.
-
-
Method Details
-
unregister
public void unregister()Closes the connection to the database and frees the cache.This method does not call
Event
s. -
updatePlayerName
@NotNull public @NotNull CompletableFuture<Result> updatePlayerName(@NotNull @NotNull Player player) Updates the name of the specified player in the database.- Parameters:
player
- The player to update.- Returns:
- A
CompletableFuture
which provides theResult
of the operation. - See Also:
-
updatePlayerTeam
@NotNull public @NotNull CompletableFuture<Result> updatePlayerTeam(@NotNull @NotNull Player playerToMove, @NotNull @NotNull Player otherTeamMember) throws UserNotLoadedException Moves the provided player from their team to the second player's one.- Parameters:
playerToMove
- The player to move.otherTeamMember
- A player of the destination team.- Returns:
- A
CompletableFuture
which provides theResult
of the operation. - Throws:
UserNotLoadedException
- If the player was not loaded into the cache.- See Also:
-
updatePlayerTeam
@NotNull public @NotNull CompletableFuture<Result> updatePlayerTeam(@NotNull @NotNull UUID playerToMove, @NotNull @NotNull UUID otherTeamMember) throws UserNotLoadedException Moves the provided player from their team to the second player's one.- Parameters:
playerToMove
- TheUUID
of the player to move.otherTeamMember
- TheUUID
of a player of the destination team.- Returns:
- A
CompletableFuture
which provides theResult
of the operation. - Throws:
UserNotLoadedException
- If the player was not loaded into the cache.- See Also:
-
updatePlayerTeam
@NotNull public @NotNull CompletableFuture<Result> updatePlayerTeam(@NotNull @NotNull Player playerToMove, @NotNull @NotNull TeamProgression otherTeamProgression) throws UserNotLoadedException Moves the provided player from their team to the specified one.- Parameters:
playerToMove
- The player to move.otherTeamProgression
- TheTeamProgression
of the target team.- Returns:
- A
CompletableFuture
which provides theResult
of the operation. - Throws:
UserNotLoadedException
- If the player was not loaded into the cache.
-
movePlayerInNewTeam
public CompletableFuture<ObjectResult<@NotNull TeamProgression>> movePlayerInNewTeam(@NotNull @NotNull Player player) throws UserNotLoadedException Moves the provided player into a new team.- Parameters:
player
- The player.- Returns:
- A
CompletableFuture
<ObjectResult
> which provides the new player team'sTeamProgression
. - Throws:
UserNotLoadedException
- If the player was not loaded into the cache.- See Also:
-
movePlayerInNewTeam
public CompletableFuture<ObjectResult<@NotNull TeamProgression>> movePlayerInNewTeam(@NotNull @NotNull UUID uuid) throws UserNotLoadedException Moves the provided player into a new team.- Parameters:
uuid
- TheUUID
of the player.- Returns:
- A
CompletableFuture
<ObjectResult
> which provides the new player team'sTeamProgression
. - Throws:
UserNotLoadedException
- If the player was not loaded into the cache.- See Also:
-
unregisterOfflinePlayer
public CompletableFuture<Result> unregisterOfflinePlayer(@NotNull @NotNull OfflinePlayer player) throws IllegalStateException Unregisters the provided player. The player must be offline and not loaded into the cache.- Parameters:
player
- The player to unregister.- Returns:
- A
CompletableFuture
which provides theResult
of the operation. - Throws:
IllegalStateException
- If the player is online or loaded into the cache.- See Also:
-
unregisterOfflinePlayer
public CompletableFuture<Result> unregisterOfflinePlayer(@NotNull @NotNull UUID uuid) throws IllegalStateException Unregisters the provided player. The player must be offline and not loaded into the cache.- Parameters:
uuid
- TheUUID
of the player to unregister.- Returns:
- A
CompletableFuture
which provides theResult
of the operation. - Throws:
IllegalStateException
- If the player is online or loaded into the cache.- See Also:
-
updateProgression
public int updateProgression(@NotNull @NotNull AdvancementKey key, @NotNull @NotNull Player player, @Range(from=0, to=Integer.MAX_VALUE) int newProgression) throws UserNotLoadedException Updates the progression of the specified advancement.- Parameters:
key
- The advancement key.player
- The player who made the advancement.newProgression
- The new progression.- Returns:
- The old progression.
- Throws:
UserNotLoadedException
- If the player was not loaded into the cache.
-
updateProgression
public int updateProgression(@NotNull @NotNull AdvancementKey key, @NotNull @NotNull UUID uuid, @Range(from=0, to=Integer.MAX_VALUE) int newProgression) throws UserNotLoadedException Updates the progression of the specified advancement.- Parameters:
key
- The advancement key.uuid
- TheUUID
of the player who made the advancement.newProgression
- The new progression.- Returns:
- The old progression.
- Throws:
UserNotLoadedException
- If the player was not loaded into the cache.
-
updateProgression
public int updateProgression(@NotNull @NotNull AdvancementKey key, @NotNull @NotNull TeamProgression progression, @Range(from=0, to=Integer.MAX_VALUE) int newProgression) Updates the progression of the specified advancement.- Parameters:
key
- The advancement key.progression
- TheTeamProgression
of the team which made the advancement.newProgression
- The new progression.- Returns:
- The old progression.
-
updateProgressionWithCompletable
@NotNull public @NotNull Map.Entry<Integer,CompletableFuture<Result>> updateProgressionWithCompletable(@NotNull @NotNull AdvancementKey key, @NotNull @NotNull Player player, @Range(from=0, to=Integer.MAX_VALUE) int newProgression) throws UserNotLoadedException Updates the progression of the specified advancement.- Parameters:
key
- The advancement key.player
- The player who made the advancement.newProgression
- The new progression.- Returns:
- A pair containing the old progression and a
CompletableFuture
which provides theResult
of the operation. - Throws:
UserNotLoadedException
- If the player was not loaded into the cache.
-
updateProgressionWithCompletable
@NotNull public @NotNull Map.Entry<Integer,CompletableFuture<Result>> updateProgressionWithCompletable(@NotNull @NotNull AdvancementKey key, @NotNull @NotNull UUID uuid, @Range(from=0, to=Integer.MAX_VALUE) int newProgression) throws UserNotLoadedException Updates the progression of the specified advancement.- Parameters:
key
- The advancement key.uuid
- TheUUID
of the player who made the advancement.newProgression
- The new progression.- Returns:
- A pair containing the old progression and a
CompletableFuture
which provides theResult
of the operation. - Throws:
UserNotLoadedException
- If the player was not loaded into the cache.
-
updateProgressionWithCompletable
@NotNull public @NotNull Map.Entry<Integer,CompletableFuture<Result>> updateProgressionWithCompletable(@NotNull @NotNull AdvancementKey key, @NotNull @NotNull TeamProgression progression, @Range(from=0, to=Integer.MAX_VALUE) int newProgression) Updates the progression of the specified advancement.- Parameters:
key
- The advancement key.progression
- TheTeamProgression
of the team which made the advancement.newProgression
- The new progression.- Returns:
- A pair containing the old progression and a
CompletableFuture
which provides theResult
of the operation.
-
getTeamProgression
@NotNull public @NotNull TeamProgression getTeamProgression(@NotNull @NotNull Player player) throws UserNotLoadedException Returns theTeamProgression
of the team of the provided player.- Parameters:
player
- The player.- Returns:
- The
TeamProgression
of the player's team. - Throws:
UserNotLoadedException
- If the player was not loaded into the cache.- See Also:
-
getTeamProgression
@NotNull public @NotNull TeamProgression getTeamProgression(@NotNull @NotNull UUID uuid) throws UserNotLoadedException Returns theTeamProgression
of the team of the provided player.- Parameters:
uuid
- TheUUID
of the player.- Returns:
- The
TeamProgression
of the player's team. - Throws:
UserNotLoadedException
- If the player was not loaded into the cache.- See Also:
-
isLoaded
Returns whether the provided player is loaded into the cache.- Parameters:
player
- The player.- Returns:
- Whether the provided player is loaded into the cache.
- See Also:
-
isLoaded
Returns whether the provided offline player is loaded into the cache.- Parameters:
player
- The player.- Returns:
- Whether the provided offline player is loaded into the cache.
- See Also:
-
isLoaded
Returns whether the provided player is loaded into the cache.- Parameters:
uuid
- TheUUID
of the player.- Returns:
- Whether the provided player is loaded into the cache.
- See Also:
-
isLoadedAndOnline
Returns whether the provided player is online and loaded into the cache.- Parameters:
player
- The player.- Returns:
- Whether the provided player is online and loaded into the cache.
-
isLoadedAndOnline
Returns whether the provided player is online and loaded into the cache.- Parameters:
uuid
- TheUUID
of the player.- Returns:
- Whether the provided player is online and loaded into the cache.
-
getLoadingRequestsAmount
@Contract(pure=true) public @Range(from=0, to=65535) int getLoadingRequestsAmount(@NotNull @NotNull Plugin plugin, @NotNull @NotNull UUID uuid, @NotNull CacheFreeingOption.Option type) Returns the number of currently active loading requests done by a plugin for the specified player with the providedCacheFreeingOption.Option
.There is a maximum per-plugin amount of requests that can be done for each player, which is
MAX_SIMULTANEOUS_LOADING_REQUESTS
.This limit is applied to
CacheFreeingOption.Option#AUTOMATIC
andCacheFreeingOption.Option#MANUAL
separately (so a plugin can do maximumMAX_SIMULTANEOUS_LOADING_REQUESTS
automatic requests andMAX_SIMULTANEOUS_LOADING_REQUESTS
manual requests simultaneously). SinceCacheFreeingOption.Option#DONT_CACHE
doesn't cache, no limit is applied to it.- Parameters:
plugin
- The plugin.uuid
- TheUUID
of the player.type
- TheCacheFreeingOption.Option
.- Returns:
- The number of the currently active player loading requests.
- See Also:
-
isUnredeemed
@NotNull public @NotNull CompletableFuture<ObjectResult<@NotNull Boolean>> isUnredeemed(@NotNull @NotNull AdvancementKey key, @NotNull @NotNull UUID uuid) throws UserNotLoadedException Returns whether the provided advancement is unredeemed for the specified player.- Parameters:
key
- The advancement key.uuid
- TheUUID
of the player.- Returns:
- A
CompletableFuture
<ObjectResult
> which provides a boolean value that istrue
if the provided advancement is unredeemed for the specified player, false otherwise. - Throws:
UserNotLoadedException
- If the player was not loaded into the cache.- See Also:
-
isUnredeemed
@NotNull public @NotNull CompletableFuture<ObjectResult<@NotNull Boolean>> isUnredeemed(@NotNull @NotNull AdvancementKey key, @NotNull @NotNull TeamProgression pro) Returns whether the provided advancement is unredeemed for the specified team.- Parameters:
key
- The advancement key.pro
- TheTeamProgression
of the team.- Returns:
- A
CompletableFuture
<ObjectResult
> which provides a boolean value that istrue
if the provided advancement is unredeemed for the specified player, false otherwise.
-
setUnredeemed
@NotNull public @NotNull CompletableFuture<Result> setUnredeemed(@NotNull @NotNull AdvancementKey key, boolean giveRewards, @NotNull @NotNull UUID uuid) throws UserNotLoadedException Sets an advancement unredeemed for the specified player.- Parameters:
key
- The advancement key.giveRewards
- Whether advancement rewards will be given on redeem.uuid
- TheUUID
of the player.- Returns:
- A
CompletableFuture
which provides theResult
of the operation. - Throws:
UserNotLoadedException
- If the player was not loaded into the cache.- See Also:
-
setUnredeemed
@NotNull public @NotNull CompletableFuture<Result> setUnredeemed(@NotNull @NotNull AdvancementKey key, boolean giveRewards, @NotNull @NotNull TeamProgression pro) Sets an advancement unredeemed for the specified team.- Parameters:
key
- The advancement key.giveRewards
- Whether advancement rewards will be given on redeem.pro
- TheTeamProgression
of the team.- Returns:
- A
CompletableFuture
which provides theResult
of the operation.
-
unsetUnredeemed
@NotNull public @NotNull CompletableFuture<Result> unsetUnredeemed(@NotNull @NotNull AdvancementKey key, @NotNull @NotNull UUID uuid) throws UserNotLoadedException Redeem the specified advancement for the provided player.- Parameters:
key
- The advancement key.uuid
- TheUUID
of the player.- Returns:
- A
CompletableFuture
which provides theResult
of the operation. - Throws:
UserNotLoadedException
- If the player was not loaded into the cache.- See Also:
-
unsetUnredeemed
@NotNull public @NotNull CompletableFuture<Result> unsetUnredeemed(@NotNull @NotNull AdvancementKey key, @NotNull @NotNull TeamProgression pro) Redeem the specified advancement for the provided team.- Parameters:
key
- The advancement key.pro
- TheTeamProgression
of the team.- Returns:
- A
CompletableFuture
which provides theResult
of the operation.
-
getStoredPlayerName
@NotNull public @NotNull CompletableFuture<ObjectResult<@NotNull String>> getStoredPlayerName(@NotNull @NotNull UUID uuid) Gets the in-database stored name of the provided player.- Parameters:
uuid
- TheUUID
of the player.- Returns:
- A
CompletableFuture
<ObjectResult
> which provides the stored name of the player. - See Also:
-
loadOfflinePlayer
@NotNull public @NotNull CompletableFuture<ObjectResult<@NotNull TeamProgression>> loadOfflinePlayer(@NotNull @NotNull UUID uuid, @NotNull @NotNull CacheFreeingOption option) Loads the provided player from the database into the caching system.Different things happens based on the specified
CacheFreeingOption
:CacheFreeingOption.DONT_CACHE()
: the player isn't loaded in the caching system, but loads and returns only the player team'sTeamProgression
;CacheFreeingOption.AUTOMATIC(Plugin, long)
: the player is loaded for a certain amount of ticks;CacheFreeingOption.MANUAL(Plugin)
: the player is loaded and kept untilunloadOfflinePlayer(UUID, Plugin)
is called.
- Parameters:
uuid
- TheUUID
of the player to load.option
- The chosenCacheFreeingOption
.- Returns:
- A
CompletableFuture
<ObjectResult
> which provides the player team'sTeamProgression
. - See Also:
-
isOfflinePlayerLoaded
Returns whether at least one loading request is currently active for the specified player.- Parameters:
uuid
- TheUUID
of the player.- Returns:
- Whether at least one loading request for the specified player is currently active.
-
isOfflinePlayerLoaded
@Contract(pure=true, value="null, null -> false; null, !null -> false; !null, null -> false") public boolean isOfflinePlayerLoaded(UUID uuid, Plugin requester) Returns whether at least one loading request, done by the provided plugin, is currently active for the specified player.- Parameters:
uuid
- TheUUID
of the player.requester
- The plugin which requested the loading.- Returns:
- Whether at least one loading request, done by the provided plugin, for the specified player is currently active.
- See Also:
-
unloadOfflinePlayer
Unloads the provided player from the caching system.Note that this method will only unload players loaded with
CacheFreeingOption.MANUAL(Plugin)
.- Parameters:
uuid
- TheUUID
of the player to unload.requester
- The plugin which requested the loading.- See Also:
-