org.spongepowered.api.service.economy.Currency Java Examples

The following examples show how to use org.spongepowered.api.service.economy.Currency. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: VirtualBalanceCommand.java    From EconomyLite with MIT License 6 votes vote down vote up
@Override
public void run(CommandSource src, CommandContext args) {
    Currency currency = currencyService.getCurrentCurrency();
    if (args.getOne("account").isPresent()) {
        if (src.hasPermission("economylite.admin.virtualbalance")) {
            String target = args.<String>getOne("account").get();
            Optional<Account> aOpt = EconomyLite.getEconomyService().getOrCreateAccount(target);
            if (aOpt.isPresent()) {
                BigDecimal bal = aOpt.get().getBalance(currency);
                Text label = currency.getPluralDisplayName();
                if (bal.equals(BigDecimal.ONE)) {
                    label = currency.getDisplayName();
                }
                src.sendMessage(messageStorage.getMessage("command.balanceother", "player", aOpt.get().getDisplayName().toPlain(), "balance",
                        String.format(Locale.ENGLISH, "%,.2f", bal), "label", label.toPlain()));
            } else {
                src.sendMessage(messageStorage.getMessage("command.error"));
            }
        } else {
            src.sendMessage(messageStorage.getMessage("command.noperm"));
        }
    } else {
        src.sendMessage(messageStorage.getMessage("command.error"));
    }
}
 
Example #2
Source File: PlayerServiceCommon.java    From EconomyLite with MIT License 6 votes vote down vote up
public boolean setBalance(UUID uuid, BigDecimal balance, Currency currency, Cause cause) {
    boolean result;
    if (accountExists(uuid, currency, cause)) {
        result = manager.executeUpdate("UPDATE economyliteplayers SET balance = ? WHERE uuid = ? AND currency = ?", balance.toString(),
                uuid.toString(), currency.getId());
        debug("playercommon: +Account Exists+ Setting balance of '" + uuid.toString() + "' to '" + balance.toPlainString() + "' with '"
                + currency.getId() + "' - " + cause.toString() + " = " + result);
    } else {
        result = manager.executeUpdate("INSERT INTO economyliteplayers (`uuid`, `balance`, `currency`) VALUES (?, ?, ?)",
                uuid.toString(), balance.toString(), currency.getId());
        debug("playercommon: +Account Does Not Exist+ Setting balance of '" + uuid.toString() + "' to '" + balance.toPlainString()
                + "' with '" + currency.getId() + "' - " + cause.toString() + " = " + result);
    }
    if (result) {
        balCache.update(formId(uuid, currency), balance);
        exCache.update(formId(uuid, currency), true);
    }
    return result;
}
 
Example #3
Source File: VirtualServiceCommon.java    From EconomyLite with MIT License 6 votes vote down vote up
public boolean setBalance(String id, BigDecimal balance, Currency currency, Cause cause) {
    boolean result;
    if (accountExists(id, currency, cause)) {
        result = manager.executeUpdate("UPDATE economylitevirts SET balance = ? WHERE id = ? AND currency = ?", balance.toString(), id,
                currency.getId());
        debug("virtcommon: +Account Exists+ Setting balance of '" + id + "' to '" + balance.toPlainString() + "' with '"
                + currency.getId() + "' - " + cause.toString() + " = " + result);
    } else {
        result = manager.executeUpdate("INSERT INTO economylitevirts (`id`, `balance`, `currency`) VALUES (?, ?, ?)", id, balance.toString(),
                currency.getId());
        debug("virtcommon: +Account Does Not Exist+ Setting balance of '" + id + "' to '" + balance.toPlainString()
                + "' with '" + currency.getId() + "' - " + cause.toString() + " = " + result);
    }
    if (result) {
        balCache.update(formId(id, currency), balance);
        exCache.update(formId(id, currency), true);
    }
    return result;
}
 
Example #4
Source File: LiteUniqueAccount.java    From EconomyLite with MIT License 6 votes vote down vote up
@Override
public TransactionResult withdraw(Currency currency, BigDecimal amount, Cause cause, Set<Context> contexts) {
    BigDecimal newBal = getBalance(currency).subtract(amount);
    // Check if the new balance is in bounds
    if (newBal.compareTo(BigDecimal.ZERO) == -1) {
        return resultAndEvent(this, amount, currency, ResultType.ACCOUNT_NO_FUNDS, TransactionTypes.WITHDRAW, cause);
    }
    if (newBal.compareTo(BigDecimal.valueOf(999999999)) == 1) {
        return resultAndEvent(this, amount, currency, ResultType.ACCOUNT_NO_SPACE, TransactionTypes.WITHDRAW, cause);
    }
    if (playerService.withdraw(uuid, amount, currency, cause)) {
        return resultAndEvent(this, amount, currency, ResultType.SUCCESS, TransactionTypes.WITHDRAW, cause);
    } else {
        return resultAndEvent(this, amount, currency, ResultType.FAILED, TransactionTypes.WITHDRAW, cause);
    }
}
 
Example #5
Source File: LiteVirtualAccount.java    From EconomyLite with MIT License 6 votes vote down vote up
@Override
public TransferResult transfer(Account to, Currency currency, BigDecimal amount, Cause cause, Set<Context> contexts) {
    BigDecimal newBal = to.getBalance(currency).add(amount);
    // Check if the new balance is in bounds
    if (newBal.compareTo(BigDecimal.ZERO) == -1 || newBal.compareTo(BigDecimal.valueOf(999999999)) == 1) {
        return resultAndEvent(this, amount, currency, ResultType.ACCOUNT_NO_SPACE, to, cause);
    }
    // Check if the account has enough funds
    if (amount.compareTo(getBalance(currency)) == 1) {
        return resultAndEvent(this, amount, currency, ResultType.ACCOUNT_NO_FUNDS, to, cause);
    }
    if (withdraw(currency, amount, cause).getResult().equals(ResultType.SUCCESS)
            && to.deposit(currency, amount, cause).getResult().equals(ResultType.SUCCESS)) {
        return resultAndEvent(this, amount, currency, ResultType.SUCCESS, to, cause);
    } else {
        return resultAndEvent(this, amount, currency, ResultType.FAILED, to, cause);
    }
}
 
Example #6
Source File: LiteVirtualAccount.java    From EconomyLite with MIT License 6 votes vote down vote up
@Override
public TransactionResult withdraw(Currency currency, BigDecimal amount, Cause cause, Set<Context> contexts) {
    BigDecimal newBal = getBalance(currency).subtract(amount);
    // Check if the new balance is in bounds
    if (newBal.compareTo(BigDecimal.ZERO) == -1) {
        return resultAndEvent(this, amount, currency, ResultType.ACCOUNT_NO_FUNDS, TransactionTypes.WITHDRAW, cause);
    }
    if (newBal.compareTo(BigDecimal.valueOf(999999999)) == 1) {
        return resultAndEvent(this, amount, currency, ResultType.ACCOUNT_NO_SPACE, TransactionTypes.WITHDRAW, cause);
    }
    if (virtualService.withdraw(name, amount, currency, cause)) {
        return resultAndEvent(this, amount, currency, ResultType.SUCCESS, TransactionTypes.WITHDRAW, cause);
    } else {
        return resultAndEvent(this, amount, currency, ResultType.FAILED, TransactionTypes.WITHDRAW, cause);
    }
}
 
Example #7
Source File: EconomyServlet.java    From Web-API with MIT License 5 votes vote down vote up
@GET
@Path("/currency")
@Permission({ "currency", "list" })
@ApiOperation(
        value = "List currencies",
        notes = "Lists all the currencies that the current economy supports.")
public Collection<Currency> getCurrencies() {
    EconomyService srv = getEconomyService();
    return srv.getCurrencies();
}
 
Example #8
Source File: LoanBalanceCommand.java    From EconomyLite with MIT License 5 votes vote down vote up
@Override
public void run(Player src, CommandContext args) {
    Optional<Double> bOpt = module.getLoanManager().getLoanBalance(src.getUniqueId());
    if (bOpt.isPresent()) {
        Currency cur = EconomyLite.getEconomyService().getDefaultCurrency();
        src.sendMessage(messages.getMessage("module.loan.balance", "balance", String.format(Locale.ENGLISH, "%,.2f", bOpt.get()),
                "label", getPrefix(bOpt.get(), cur)));
    } else {
        src.sendMessage(messages.getMessage("command.error"));
    }
}
 
Example #9
Source File: LoanPayCommand.java    From EconomyLite with MIT License 5 votes vote down vote up
private String getPrefix(double amnt, Currency cur) {
    Text label = cur.getPluralDisplayName();
    if (amnt == 1.0) {
        label = cur.getDisplayName();
    }
    return TextSerializers.FORMATTING_CODE.serialize(label);
}
 
Example #10
Source File: LoanPayCommand.java    From EconomyLite with MIT License 5 votes vote down vote up
@Override
public void run(Player src, CommandContext args) {
    if (args.getOne("amount").isPresent()) {
        UUID uuid = src.getUniqueId();
        double payment = args.<Double>getOne("amount").get();
        Currency cur = EconomyLite.getEconomyService().getDefaultCurrency();
        Optional<Double> bOpt = module.getLoanManager().getLoanBalance(uuid);
        if (bOpt.isPresent()) {
            double loanBal = bOpt.get();
            if (payment > loanBal) {
                // Pay only what is needed
                if (module.getLoanManager().removeLoanBalance(uuid, loanBal)) {
                    // Successfully payed loan
                    src.sendMessage(messages.getMessage("module.loan.payed", "amount", String.format(Locale.ENGLISH, "%,.2f", loanBal),
                            "label", getPrefix(loanBal, cur)));
                } else {
                    // Failed to pay
                    src.sendMessage(messages.getMessage("module.loan.payedfail"));
                }
            } else {
                // Pay entire request
                if (module.getLoanManager().removeLoanBalance(uuid, payment)) {
                    // Successfully payed loan
                    src.sendMessage(messages.getMessage("module.loan.payed", "amount", String.format(Locale.ENGLISH, "%,.2f", payment),
                            "label", getPrefix(payment, cur)));
                } else {
                    // Failed to pay
                    src.sendMessage(messages.getMessage("module.loan.payedfail"));
                }
            }
        }
    } else {
        src.sendMessage(messages.getMessage("command.error"));
    }
}
 
Example #11
Source File: LoanTakeCommand.java    From EconomyLite with MIT License 5 votes vote down vote up
private String getPrefix(double amnt, Currency cur) {
    Text label = cur.getPluralDisplayName();
    if (amnt == 1.0) {
        label = cur.getDisplayName();
    }
    return TextSerializers.FORMATTING_CODE.serialize(label);
}
 
Example #12
Source File: LiteVirtualAccount.java    From EconomyLite with MIT License 5 votes vote down vote up
@Override
public Map<Currency, BigDecimal> getBalances(Set<Context> contexts) {
    HashMap<Currency, BigDecimal> balances = new HashMap<Currency, BigDecimal>();
    for (Currency currency : currencyService.getCurrencies()) {
        if (virtualService.accountExists(name, currency, CauseFactory.create("Get Balances"))) {
            balances.put(currency, virtualService.getBalance(name, currency, CauseFactory.create("Get Balances Put")));
        }
    }
    return balances;
}
 
Example #13
Source File: LiteUniqueAccount.java    From EconomyLite with MIT License 5 votes vote down vote up
@Override
public Map<Currency, TransactionResult> resetBalances(Cause cause, Set<Context> contexts) {
    HashMap<Currency, TransactionResult> results = new HashMap<>();
    for (Currency currency : currencyService.getCurrencies()) {
        if (playerService.accountExists(uuid, currency, cause)) {
            if (playerService.setBalance(uuid, getDefaultBalance(currency), currency, cause)) {
                results.put(currency, resultAndEvent(this, getBalance(currency), currency, ResultType.SUCCESS, TransactionTypes.WITHDRAW, cause));
            } else {
                results.put(currency, resultAndEvent(this, getBalance(currency), currency, ResultType.FAILED, TransactionTypes.WITHDRAW, cause));
            }
        }
    }
    return results;
}
 
Example #14
Source File: LiteVirtualAccount.java    From EconomyLite with MIT License 5 votes vote down vote up
@Override
public BigDecimal getDefaultBalance(Currency currency) {
    Optional<Double> bOpt = EconomyLite.getConfigManager().getValue(Double.class, "default-balance", "virtual");
    if (bOpt.isPresent()) {
        return BigDecimal.valueOf(bOpt.get());
    } else {
        return BigDecimal.ZERO;
    }
}
 
Example #15
Source File: VirtualServiceCommon.java    From EconomyLite with MIT License 5 votes vote down vote up
public void clearCurrency(Currency currency, Cause cause) {
    boolean result = manager.executeUpdate("DELETE FROM economylitevirts WHERE currency = ?", currency.getId());
    debug("virtcommon: Clearing currency '" + currency.getId() + "' - " + cause.toString() + " = " + result);
    balCache.clear();
    exCache.clear();
    topCache.clear();
}
 
Example #16
Source File: LiteVirtualAccount.java    From EconomyLite with MIT License 5 votes vote down vote up
@Override
public Map<Currency, TransactionResult> resetBalances(Cause cause, Set<Context> contexts) {
    HashMap<Currency, TransactionResult> results = new HashMap<>();
    for (Currency currency : currencyService.getCurrencies()) {
        if (virtualService.accountExists(name, currency, cause)) {
            if (virtualService.setBalance(name, getDefaultBalance(currency), currency, cause)) {
                results.put(currency, resultAndEvent(this, getBalance(currency), currency, ResultType.SUCCESS, TransactionTypes.WITHDRAW, cause));
            } else {
                results.put(currency, resultAndEvent(this, getBalance(currency), currency, ResultType.FAILED, TransactionTypes.WITHDRAW, cause));
            }
        }
    }
    return results;
}
 
Example #17
Source File: LiteVirtualAccount.java    From EconomyLite with MIT License 5 votes vote down vote up
@Override
public TransactionResult resetBalance(Currency currency, Cause cause, Set<Context> contexts) {
    if (virtualService.setBalance(name, getDefaultBalance(currency), currency, cause)) {
        return resultAndEvent(this, BigDecimal.ZERO, currency, ResultType.SUCCESS, TransactionTypes.WITHDRAW, cause);
    } else {
        return resultAndEvent(this, BigDecimal.ZERO, currency, ResultType.FAILED, TransactionTypes.WITHDRAW, cause);
    }
}
 
Example #18
Source File: LoanManager.java    From EconomyLite with MIT License 5 votes vote down vote up
/**
 * Removes loan balance from a player. Automatically removes funds from the
 * player's account.
 *
 * @param uuid The player.
 * @param amount The amount to remove.
 * @return If the amount was removed successfully.
 */
public boolean removeLoanBalance(UUID uuid, double amount) {
    Currency cur = eco.getDefaultCurrency();
    Optional<Double> bOpt = getLoanBalance(uuid);
    Optional<UniqueAccount> uOpt = eco.getOrCreateAccount(uuid);
    if (bOpt.isPresent() && uOpt.isPresent()) {
        double bal = bOpt.get();
        if (bal - amount < 0 || amount < 0) {
            return false;
        }
        return (uOpt.get().withdraw(cur, BigDecimal.valueOf(amount), CauseFactory.stringCause("loan")).getResult()
                .equals(ResultType.SUCCESS) && setLoanBalance(uuid, bal - amount));
    }
    return false;
}
 
Example #19
Source File: LoanBalanceCommand.java    From EconomyLite with MIT License 5 votes vote down vote up
private String getPrefix(double amnt, Currency cur) {
    Text label = cur.getPluralDisplayName();
    if (amnt == 1.0) {
        label = cur.getDisplayName();
    }
    return TextSerializers.FORMATTING_CODE.serialize(label);
}
 
Example #20
Source File: LiteUniqueAccount.java    From EconomyLite with MIT License 5 votes vote down vote up
@Override
public TransactionResult resetBalance(Currency currency, Cause cause, Set<Context> contexts) {
    if (playerService.setBalance(uuid, getDefaultBalance(currency), currency, cause)) {
        return resultAndEvent(this, BigDecimal.ZERO, currency, ResultType.SUCCESS, TransactionTypes.WITHDRAW, cause);
    } else {
        return resultAndEvent(this, BigDecimal.ZERO, currency, ResultType.FAILED, TransactionTypes.WITHDRAW, cause);
    }
}
 
Example #21
Source File: VirtualServiceCommon.java    From EconomyLite with MIT License 5 votes vote down vote up
public boolean accountExists(String id, Currency currency, Cause cause) {
    Boolean result = exCache.getIfPresent(formId(id, currency));
    if (result != null) {
        debug("virtcommon: {C} Checking if '" + id + "' exists with '" + currency.getId() + "' - " + cause.toString() + " = " + result);
        return result;
    }
    result = manager.queryExists("SELECT id FROM economylitevirts WHERE id = ? AND currency = ?", id, currency.getId());
    debug("virtcommon: Checking if '" + id + "' exists with '" + currency.getId() + "' - " + cause.toString() + " = " + result);
    exCache.update(formId(id, currency), result);
    return result;
}
 
Example #22
Source File: LiteUniqueAccount.java    From EconomyLite with MIT License 5 votes vote down vote up
@Override
public TransactionResult setBalance(Currency currency, BigDecimal amount, Cause cause, Set<Context> contexts) {
    // Check if the new balance is in bounds
    if (amount.compareTo(BigDecimal.ZERO) == -1 || amount.compareTo(BigDecimal.valueOf(999999999)) == 1) {
        return resultAndEvent(this, amount, currency, ResultType.ACCOUNT_NO_SPACE, TransactionTypes.DEPOSIT, cause);
    }
    if (playerService.setBalance(uuid, amount, currency, cause)) {
        return resultAndEvent(this, amount, currency, ResultType.SUCCESS, TransactionTypes.DEPOSIT, cause);
    } else {
        return resultAndEvent(this, amount, currency, ResultType.FAILED, TransactionTypes.DEPOSIT, cause);
    }
}
 
Example #23
Source File: LiteUniqueAccount.java    From EconomyLite with MIT License 5 votes vote down vote up
@Override
public Map<Currency, BigDecimal> getBalances(Set<Context> contexts) {
    HashMap<Currency, BigDecimal> balances = new HashMap<Currency, BigDecimal>();
    for (Currency currency : currencyService.getCurrencies()) {
        if (playerService.accountExists(uuid, currency, CauseFactory.create("Get Balances"))) {
            balances.put(currency, playerService.getBalance(uuid, currency, CauseFactory.create("Get Balances Put")));
        }
    }
    return balances;
}
 
Example #24
Source File: LiteVirtualAccount.java    From EconomyLite with MIT License 5 votes vote down vote up
@Override
public TransactionResult deposit(Currency currency, BigDecimal amount, Cause cause, Set<Context> contexts) {
    BigDecimal newBal = getBalance(currency).add(amount);
    // Check if the new balance is in bounds
    if (newBal.compareTo(BigDecimal.ZERO) == -1 || newBal.compareTo(BigDecimal.valueOf(999999999)) == 1) {
        return resultAndEvent(this, amount, currency, ResultType.ACCOUNT_NO_SPACE, TransactionTypes.DEPOSIT, cause);
    }
    if (virtualService.deposit(name, amount, currency, cause)) {
        return resultAndEvent(this, amount, currency, ResultType.SUCCESS, TransactionTypes.DEPOSIT, cause);
    } else {
        return resultAndEvent(this, amount, currency, ResultType.FAILED, TransactionTypes.DEPOSIT, cause);
    }
}
 
Example #25
Source File: LiteUniqueAccount.java    From EconomyLite with MIT License 5 votes vote down vote up
@Override
public BigDecimal getDefaultBalance(Currency currency) {
    Optional<Double> bOpt = EconomyLite.getConfigManager().getValue(Double.class, "default-balance", "player");
    if (bOpt.isPresent()) {
        return BigDecimal.valueOf(bOpt.get());
    } else {
        return BigDecimal.ZERO;
    }
}
 
Example #26
Source File: PlayerServiceCommon.java    From EconomyLite with MIT License 5 votes vote down vote up
public void setRawData(String uuid, String bal, Currency currency) {
    if (accountExists(UUID.fromString(uuid), currency, CauseFactory.stringCause("Migration"))) {
        manager.executeUpdate("UPDATE economyliteplayers SET balance = ? WHERE uuid = ? AND currency = ?", bal, uuid, currency.getId());
    } else {
        manager.executeUpdate("INSERT INTO economyliteplayers (`uuid`, `balance`, `currency`) VALUES (?, ?, ?)", uuid, bal, currency.getId());
    }
}
 
Example #27
Source File: PlayerServiceCommon.java    From EconomyLite with MIT License 5 votes vote down vote up
public boolean setBalanceAll(BigDecimal balance, Currency currency, Cause cause) {
    boolean result = manager.executeUpdate("UPDATE economyliteplayers SET balance = ? WHERE currency = ?", balance.toString(), currency.getId());
    debug("playercommon: +Account Exists+ Setting balance of ALL to '" + balance.toPlainString() + "' with '"
            + currency.getId() + "' - " + cause.toString() + " = " + result);
    topCache.clear();
    balCache.clear();
    return result;
}
 
Example #28
Source File: PlayerServiceCommon.java    From EconomyLite with MIT License 5 votes vote down vote up
public void clearCurrency(Currency currency, Cause cause) {
    boolean result = manager.executeUpdate("DELETE FROM economyliteplayers WHERE currency = ?", currency.getId());
    debug("playercommon: Clearing currency '" + currency.getId() + "' - " + cause.toString() + " = " + result);
    balCache.clear();
    exCache.clear();
    topCache.clear();
}
 
Example #29
Source File: PlayerServiceCommon.java    From EconomyLite with MIT License 5 votes vote down vote up
public boolean accountExists(UUID uuid, Currency currency, Cause cause) {
    Boolean result = exCache.getIfPresent(formId(uuid, currency));
    if (result != null) {
        debug("playercommon: {C} Checking if '" + uuid.toString() + "' exists with '" + currency.getId() + "' - " + cause.toString() + " = "
                + result);
        return result;
    }
    result = manager.queryExists("SELECT uuid FROM economyliteplayers WHERE uuid = ? AND currency = ?", uuid.toString(), currency.getId());
    debug("playercommon: Checking if '" + uuid.toString() + "' exists with '" + currency.getId() + "' - " + cause.toString() + " = " + result);
    exCache.update(formId(uuid, currency), result);
    return result;
}
 
Example #30
Source File: PlayerServiceCommon.java    From EconomyLite with MIT License 5 votes vote down vote up
public BigDecimal getBalance(UUID uuid, Currency currency, Cause cause, boolean cache) {
    BigDecimal result = balCache.getIfPresent(formId(uuid, currency));
    if (cache && result != null) {
        debug("playercommon: {C} Balance of '" + uuid.toString() + "' - " + cause.toString() + " = " + result.toPlainString());
        return result;
    }
    Optional<BigDecimal> bOpt =
            manager.queryType("balance", BigDecimal.class, "SELECT balance FROM economyliteplayers WHERE uuid = ? AND currency = ?",
                    uuid.toString(), currency.getId());
    result = (bOpt.isPresent()) ? bOpt.get() : BigDecimal.ZERO;
    balCache.update(formId(uuid, currency), result);
    exCache.update(formId(uuid, currency), true);
    debug("playercommon: Balance of '" + uuid.toString() + "' - " + cause.toString() + " = " + result.toPlainString());
    return result;
}