Interface EconomyService
- All Superinterfaces:
Service
Overview
This service provides a complete economy API including:
- Account management (create, retrieve, update, delete)
- Balance operations (get, deposit, withdraw, transfer)
- Transaction support with context tracking
- Asynchronous operations for all database interactions
Asynchronous Operations
All account and balance operations return CompletableFuture for non-blocking execution.
Transactions are created synchronously but execute asynchronously when committed.
Usage Example
// Get the service
EconomyService economy = Mint.ECONOMY_SERVICE.get();
// Create an account for a player
Account account = economy.createAccount(playerId, "Steve").join();
// Deposit funds
economy.deposit(playerId, new BigDecimal("100.0"))
.thenAccept(actualDeposit -> {
// Handle completion
});
// Transfer with transaction context
TransactionContext ctx = TransactionContext.builder()
.actor(Actor.player(adminId))
.reason("Payment for goods")
.build();
economy.transfer(fromId, toId, amount, ctx)
.thenAccept(transferred -> {
// Handle completion
});
- Since:
- 1.0
-
Method Summary
Modifier and TypeMethodDescriptiondefault TransactionbeginTransaction(UUID accountId) Begins a new transaction against the specified account with empty context.beginTransaction(UUID accountId, TransactionContext ctx) Begins a new transaction against the specified account with context.createAccount(UUID holderId, String name) Creates a new account for the specified holder.deleteAccount(UUID accountId) Deletes the specified account.deposit(UUID holderId, BigDecimal amount) Deposits funds into the specified holder's account.getAccount(UUID accountId) Retrieves an account by its identifier.getBalanceByAccount(UUID accountId) Gets the balance of the specified account.getBalanceByHolder(UUID holderId) Gets the balance for the specified holder's primary account.getBalances(List<UUID> accountIds) Gets balances for multiple accounts in a single batch operation.intGets the maximum number of fractional digits allowed for currency amounts.getTopBalances(int limit) Gets the top balances sorted by balance in descending order.hasAccount(UUID holderId) Checks if an account exists for the specified holder.transfer(UUID fromHolderId, UUID toHolderId, BigDecimal amount) Transfers funds from one holder's account to another holder's account.updateAccountName(UUID accountId, String name) Updates the display displayName of the specified account.withdraw(UUID holderId, BigDecimal amount) Withdraws funds from the specified holder's account.
-
Method Details
-
getMaxFractionalDigits
@AvailableSince("1.0") int getMaxFractionalDigits()Gets the maximum number of fractional digits allowed for currency amounts.This value determines the precision of monetary values. For example:
2= Standard currency (cents, e.g., $10.99)0= Whole numbers only (e.g., 10 gold)4= High precision (e.g., gas prices)
- Returns:
- maximum decimal places for monetary values
-
beginTransaction
Begins a new transaction against the specified account with empty context.Creates a transaction in
Transaction.State.ACTIVEstate. Operations can be chained before callingTransaction.commit().Equivalent to:
beginTransaction(accountId, TransactionContext.empty())- Parameters:
accountId- the account to transact against- Returns:
- a new active transaction
-
beginTransaction
Begins a new transaction against the specified account with context.Transactions track who performed the operation and why, providing an audit trail for economy changes. The context is persisted with the transaction record.
- Parameters:
accountId- the account to transact againstctx- context containing actor and optional reason- Returns:
- a new active transaction
-
getBalanceByAccount
Gets the balance of the specified account.- Parameters:
accountId- the account identifier- Returns:
- future completing with the account balance
-
getBalanceByHolder
Gets the balance for the specified holder's primary account.This is a convenience method that first resolves the holder's account ID and then retrieves the balance.
- Parameters:
holderId- the holder's unique identifier- Returns:
- future completing with the account balance
-
getBalances
Gets balances for multiple accounts in a single batch operation.More efficient than calling
getBalanceByAccount(UUID)multiple times. The returned list contains balances in the same order as the input account IDs.- Parameters:
accountIds- list of account identifiers- Returns:
- future completing with list of balances in matching order
-
hasAccount
Checks if an account exists for the specified holder.- Parameters:
holderId- the holder's unique identifier- Returns:
- future completing with
trueif account exists
-
createAccount
Creates a new account for the specified holder.If an account already exists for this holder, the future will complete exceptionally. Use
hasAccount(UUID)to check existence first if needed.- Parameters:
holderId- the holder's unique identifiername- the display displayName for the account- Returns:
- future completing with the newly created account
-
getAccount
Retrieves an account by its identifier.- Parameters:
accountId- the account identifier- Returns:
- future completing with optional containing the account if found
-
deleteAccount
Deletes the specified account.This operation is irreversible. All associated transaction history will be removed.
- Parameters:
accountId- the account identifier- Returns:
- future completing with
trueif account was deleted,falseif not found
-
updateAccountName
Updates the display displayName of the specified account.- Parameters:
accountId- the account identifiername- the new display displayName for the account- Returns:
- future completing with
trueif updated,falseif account not found
-
transfer
@AvailableSince("1.0") CompletableFuture<BigDecimal> transfer(UUID fromHolderId, UUID toHolderId, BigDecimal amount) Transfers funds from one holder's account to another holder's account.If the source account has insufficient funds, the transfer will fail exceptionally.
- Parameters:
fromHolderId- the holder transferring fundstoHolderId- the holder receiving fundsamount- the amount to transfer- Returns:
- future completing with the amount actually transferred
-
deposit
Deposits funds into the specified holder's account.- Parameters:
holderId- the holder's unique identifieramount- the amount to deposit (must be positive)- Returns:
- future completing with the amount actually deposited
-
withdraw
Withdraws funds from the specified holder's account.If the account has insufficient funds, the withdrawal will fail exceptionally.
- Parameters:
holderId- the holder's unique identifieramount- the amount to withdraw (must be positive)- Returns:
- future completing with the amount actually withdrawn
-
getTopBalances
Gets the top balances sorted by balance in descending order.Useful for leaderboards and "baltop" commands. Returns a map of holder IDs to their balances, sorted highest first.
- Parameters:
limit- the maximum number of entries to return- Returns:
- future completing with a map of holder IDs to balances, sorted by balance (highest first)
-