Interface CurrencyService
Use it to read player balances, currency configuration, and reward values, and since 1.8.0
to change balances with deposit(java.util.UUID, double, java.lang.String), withdraw(java.util.UUID, double, java.lang.String), setBalance(java.util.UUID, double, java.lang.String) and
resetBalance(java.util.UUID, java.lang.String). Built-in rewards can be adjusted or withheld through
CurrencyRewardEvent, and every committed
change is reported by CurrencyTransactionEvent.
Example
CurrencyService currencyService = ZentrixProvider.get().getCurrencyService();
// Get the player's balance
double balance = currencyService.getCachedBalance(player);
// Format the balance
String formatted = currencyService.formatBalance(balance);
// Result: "⛃ 150"
// Check an event reward
double killReward = currencyService.getEventReward(CurrencyEventType.PLAYER_KILL);
- Since:
- 1.0.0
-
Method Summary
Modifier and TypeMethodDescriptiondefault @NotNull CompletableFuture<CurrencyTransaction> Adds currency to a player's balance.@NotNull StringformatAmount(double amount) Formats a currency amount for display.@NotNull StringformatBalance(double balance) Formats a balance with the currency symbol.@NotNull CompletableFuture<Double> getBalance(@NotNull UUID playerId) Gets a player's balance asynchronously.@NotNull CompletableFuture<Double> getBalance(@NotNull org.bukkit.entity.Player player) Gets a player's balance asynchronously.doublegetCachedBalance(@NotNull UUID playerId) Gets a player's cached balance for immediate access.doublegetCachedBalance(@NotNull org.bukkit.entity.Player player) Gets a player's cached balance for immediate access.@NotNull StringGets the currency display name.doublegetEventReward(@NotNull CurrencyEventType eventType) Gets the reward amount for a specific event type.default doublegetRewardMultiplier(org.bukkit.entity.Player player) The multiplier applied to the match rewards ofgameplay/economy.ymlthis player earns, from their permission nodes such aszentrix.multiplier.1.5.doubleGets the starting balance for new players.@NotNull StringGets the currency symbol.booleanisEventEnabled(@NotNull CurrencyEventType eventType) Checks if a specific event type has rewards enabled.voidrefreshCache(@NotNull UUID playerId) Refreshes the cached balance for a player.voidrefreshCache(@NotNull org.bukkit.entity.Player player) Refreshes the cached balance for a player.default @NotNull CompletableFuture<CurrencyTransaction> resetBalance(@NotNull UUID playerId, @NotNull String source) Sets a player's balance back togetStartingBalance().default @NotNull CompletableFuture<CurrencyTransaction> setBalance(@NotNull UUID playerId, double balance, @NotNull String source) Sets a player's balance outright.default @NotNull CompletableFuture<CurrencyTransaction> Takes currency from a player's balance, only when they have at least that much.
-
Method Details
-
getBalance
Gets a player's balance asynchronously.If the balance is not cached, this loads it from storage. Use
getCachedBalance(UUID)for synchronous access.- Parameters:
playerId- The player's UUID- Returns:
- CompletableFuture containing the balance
-
getBalance
@NotNull @NotNull CompletableFuture<Double> getBalance(@NotNull @NotNull org.bukkit.entity.Player player) Gets a player's balance asynchronously.- Parameters:
player- The player- Returns:
- CompletableFuture containing the balance
-
getCachedBalance
Gets a player's cached balance for immediate access.Returns the cached value when available, or the starting balance otherwise. Use this for scoreboards and GUIs that cannot wait for an async result.
- Parameters:
playerId- The player's UUID- Returns:
- The cached balance (may be slightly outdated)
-
getCachedBalance
double getCachedBalance(@NotNull @NotNull org.bukkit.entity.Player player) Gets a player's cached balance for immediate access.- Parameters:
player- The player- Returns:
- The cached balance (may be slightly outdated)
-
getDisplayName
Gets the currency display name.Example:
&6Coinsor&#FFD700Gold- Returns:
- The display name with color codes (never null)
-
getSymbol
Gets the currency symbol.Example: "⛃" or "$"
- Returns:
- The currency symbol (never null)
-
getStartingBalance
double getStartingBalance()Gets the starting balance for new players.- Returns:
- The starting balance
-
isEventEnabled
Checks if a specific event type has rewards enabled.- Parameters:
eventType- The event type to check- Returns:
trueif the event awards currency
-
getEventReward
Gets the reward amount for a specific event type.Returns 0 if the event is not enabled. Negative values indicate penalties (currency deduction).
- Parameters:
eventType- The event type- Returns:
- The reward amount (can be negative for penalties)
-
formatAmount
Formats a currency amount for display.Example: formatAmount(10.0) returns "10" Example: formatAmount(10.5) returns "10.5"
- Parameters:
amount- The amount to format- Returns:
- Formatted string (never null)
-
formatBalance
Formats a balance with the currency symbol.Example: formatBalance(150.0) returns "⛃ 150"
- Parameters:
balance- The balance to format- Returns:
- Formatted string with symbol (never null)
-
refreshCache
Refreshes the cached balance for a player.Fetches the latest balance from storage and updates the cache.
- Parameters:
playerId- The player's UUID
-
refreshCache
void refreshCache(@NotNull @NotNull org.bukkit.entity.Player player) Refreshes the cached balance for a player.- Parameters:
player- The player
-
deposit
@NotNull default @NotNull CompletableFuture<CurrencyTransaction> deposit(@NotNull @NotNull UUID playerId, double amount, @NotNull @NotNull String source) Adds currency to a player's balance.The change goes through the same pipeline as Zentrix's own: it is queued behind the player's other changes,
CurrencyChangeEventfires before it is applied with reasonADDONand the given source, and the future completes with what was really committed. No message is sent to the player. Any thread; the future may complete on any thread.- Parameters:
playerId- The player's UUID; the player does not need to be onlineamount- The amount to add; must be positive and finitesource- What the change is for, such as"myaddon:quest-reward"- Returns:
- The committed change
- Since:
- 1.8.0
-
withdraw
@NotNull default @NotNull CompletableFuture<CurrencyTransaction> withdraw(@NotNull @NotNull UUID playerId, double amount, @NotNull @NotNull String source) Takes currency from a player's balance, only when they have at least that much.The balance is checked and changed as one step of the player's queue, so two withdrawals racing each other on this server can never take more than the player holds: one completes with
CurrencyTransaction.Status.INSUFFICIENT_FUNDSand changes nothing. Otherwise it behaves likedeposit(UUID, double, String).- Parameters:
playerId- The player's UUIDamount- The amount to take; must be positive and finitesource- What the change is for, such as"myaddon:shop"- Returns:
- The committed change
- Since:
- 1.8.0
-
setBalance
@NotNull default @NotNull CompletableFuture<CurrencyTransaction> setBalance(@NotNull @NotNull UUID playerId, double balance, @NotNull @NotNull String source) Sets a player's balance outright. Otherwise behaves likedeposit(UUID, double, String).- Parameters:
playerId- The player's UUIDbalance- The new balance; must be zero or more and finitesource- What the change is for- Returns:
- The committed change
- Since:
- 1.8.0
-
resetBalance
@NotNull default @NotNull CompletableFuture<CurrencyTransaction> resetBalance(@NotNull @NotNull UUID playerId, @NotNull @NotNull String source) Sets a player's balance back togetStartingBalance(). Otherwise behaves likedeposit(UUID, double, String).- Parameters:
playerId- The player's UUIDsource- What the change is for- Returns:
- The committed change
- Since:
- 1.8.0
-
getRewardMultiplier
default double getRewardMultiplier(@NotNull org.bukkit.entity.Player player) The multiplier applied to the match rewards ofgameplay/economy.ymlthis player earns, from their permission nodes such aszentrix.multiplier.1.5. The highest node applies; nodes do not stack.Only positive match rewards are multiplied, before
CurrencyRewardEventsees them. Penalties, administrator commands and addon transactions such asdeposit(UUID, double, String)never are.- Parameters:
player- The player- Returns:
- 1.0 when the player has no multiplier node or multipliers are switched off
- Since:
- 1.9.0
-