Class LifestealerUserManager
java.lang.Object
dev.chicoferreira.lifestealer.user.LifestealerUserManager
Manages the
LifestealerUser
instances and persistence of them.-
Constructor Summary
ConstructorsConstructorDescriptionLifestealerUserManager
(UserPersistentStorage persistentStorage, LifestealerExecutor executor, int startingHearts) -
Method Summary
Modifier and TypeMethodDescription@Nullable LifestealerUser
getOnlineUser
(UUID uuid) Gets theLifestealerUser
of a player that is online.@NotNull LifestealerUser
getOnlineUser
(org.bukkit.entity.Player player) Gets theLifestealerUser
of a player that is online.@NotNull LifestealerUser
getOrLoadUser
(UUID uuid) Gets or loads a user from the database synchronously.int
Gets the starting amount of hearts for new users.boolean
isDefaultUser
(LifestealerUser user) Checks if a user hasn't changed from the default values.@Nullable LifestealerUser
Loads a user from the database.saveUserAsync
(LifestealerUser user) Saves a user to the database asynchronously.void
saveUserSync
(LifestealerUser user) Saves a user to the database synchronously.void
setStartingHearts
(int startingHearts) Sets the starting amount of hearts for new users.
-
Constructor Details
-
LifestealerUserManager
public LifestealerUserManager(UserPersistentStorage persistentStorage, LifestealerExecutor executor, int startingHearts)
-
-
Method Details
-
loadUser
Loads a user from the database. This does not cache the user in memory. Thread-safety: Can be run asynchronously.- Parameters:
uuid
- the uuid of the user to load- Returns:
- the loaded user or null if the user is not saved in the database
- Throws:
Exception
- if an error occurs while loading the user
-
getOrLoadUser
Gets or loads a user from the database synchronously. If the user is not already loaded, it will be loaded from the database and cached in memory. If the user is not saved in the database, a new user will be created and cached in memory.Thread-safety: This method is thread-safe and you can call it from any thread.
- Parameters:
uuid
- the uuid of the user to get or load- Returns:
- the user if it's already loaded, otherwise loads it from the database and puts it in memory
- Throws:
Exception
- if an error occurs while loading the user
-
getOnlineUser
Gets theLifestealerUser
of a player that is online. This method is guaranteed to return a user if the player is online because ofLifestealerUserListener.onPreLogin(AsyncPlayerPreLoginEvent)
.- Parameters:
player
- the player to get the user from- Returns:
- the user of the player
-
getOnlineUser
Gets theLifestealerUser
of a player that is online. This method is guaranteed to return a user if that UUID is from a player is that online because ofLifestealerUserListener.onPreLogin(AsyncPlayerPreLoginEvent)
. This method is annotated withNullable
because the UUID might not be from a player that is online.- Parameters:
uuid
- the uuid of the user to get- Returns:
- the user of the player
-
isDefaultUser
Checks if a user hasn't changed from the default values.- Parameters:
user
- the user to check- Returns:
- true if the user is the default user, false otherwise
-
saveUserAsync
Saves a user to the database asynchronously. This method is thread-safe and can be called from any thread.- Parameters:
user
- the user to save- Returns:
- a
CompletableFuture
that will be completed when the user is saved
-
saveUserSync
Saves a user to the database synchronously. This method is thread-safe and can be called from any thread, but it will block the current thread.- Parameters:
user
- the user to save- Throws:
Exception
- if an error occurs while saving the user
-
getStartingHearts
public int getStartingHearts()Gets the starting amount of hearts for new users.- Returns:
- the starting amount of hearts for new users
-
setStartingHearts
public void setStartingHearts(int startingHearts) Sets the starting amount of hearts for new users. Used when reloading the configuration.- Parameters:
startingHearts
- the starting amount of hearts for new users
-