Search in sources :

Example 6 with TransactionResult

use of org.spongepowered.api.service.economy.transaction.TransactionResult in project TotalEconomy by Erigitic.

the class TEAccount method setBalance.

@Override
public TransactionResult setBalance(Currency currency, BigDecimal amount, Cause cause, Set<Context> contexts) {
    TransactionResult transactionResult;
    String currencyName = currency.getDisplayName().toPlain().toLowerCase();
    if (hasBalance(currency, contexts) && amount.compareTo(BigDecimal.ZERO) >= 0) {
        if (databaseActive) {
            SQLQuery sqlQuery = SQLQuery.builder(sqlHandler.dataSource).update("totaleconomy.accounts").set(currencyName + "_balance").equals(amount.setScale(2, BigDecimal.ROUND_DOWN).toPlainString()).where("uid").equals(uuid.toString()).build();
            if (sqlQuery.getRowsAffected() > 0) {
                transactionResult = new TETransactionResult(this, currency, amount, contexts, ResultType.SUCCESS, TransactionTypes.DEPOSIT);
                totalEconomy.getGame().getEventManager().post(new TEEconomyTransactionEvent(transactionResult));
            } else {
                transactionResult = new TETransactionResult(this, currency, amount, contexts, ResultType.FAILED, TransactionTypes.DEPOSIT);
                totalEconomy.getGame().getEventManager().post(new TEEconomyTransactionEvent(transactionResult));
            }
            return transactionResult;
        } else {
            accountConfig.getNode(uuid.toString(), currencyName + "-balance").setValue(amount.setScale(2, BigDecimal.ROUND_DOWN));
            accountManager.saveAccountConfig();
            transactionResult = new TETransactionResult(this, currency, amount, contexts, ResultType.SUCCESS, TransactionTypes.DEPOSIT);
            totalEconomy.getGame().getEventManager().post(new TEEconomyTransactionEvent(transactionResult));
            return transactionResult;
        }
    }
    transactionResult = new TETransactionResult(this, currency, amount, contexts, ResultType.ACCOUNT_NO_FUNDS, TransactionTypes.DEPOSIT);
    totalEconomy.getGame().getEventManager().post(new TEEconomyTransactionEvent(transactionResult));
    return transactionResult;
}
Also used : TransactionResult(org.spongepowered.api.service.economy.transaction.TransactionResult) SQLQuery(com.erigitic.sql.SQLQuery)

Example 7 with TransactionResult

use of org.spongepowered.api.service.economy.transaction.TransactionResult in project TotalEconomy by Erigitic.

the class AdminPayCommand method execute.

@Override
public CommandResult execute(CommandSource src, CommandContext args) throws CommandException {
    String strAmount = (String) args.getOne("amount").get();
    User recipient = args.<User>getOne("player").get();
    Pattern amountPattern = Pattern.compile("^[+-]?(\\d*\\.)?\\d+$");
    Matcher m = amountPattern.matcher(strAmount);
    if (m.matches()) {
        BigDecimal amount = new BigDecimal((String) args.getOne("amount").get()).setScale(2, BigDecimal.ROUND_DOWN);
        TEAccount recipientAccount = (TEAccount) accountManager.getOrCreateAccount(recipient.getUniqueId()).get();
        Text amountText = Text.of(defaultCurrency.format(amount).toPlain().replace("-", ""));
        TransactionResult transactionResult = recipientAccount.deposit(totalEconomy.getDefaultCurrency(), amount, Cause.of(NamedCause.of("TotalEconomy", totalEconomy.getPluginContainer())));
        if (transactionResult.getResult() == ResultType.SUCCESS) {
            if (!strAmount.contains("-")) {
                src.sendMessage(Text.of(TextColors.GRAY, "You have sent ", TextColors.GOLD, amountText, TextColors.GRAY, " to ", TextColors.GOLD, recipient.getName(), TextColors.GRAY, "."));
                if (recipient.isOnline()) {
                    recipient.getPlayer().get().sendMessage(Text.of(TextColors.GRAY, "You have received ", TextColors.GOLD, amountText, TextColors.GRAY, " from ", TextColors.GOLD, src.getName(), TextColors.GRAY, "."));
                }
            } else {
                src.sendMessage(Text.of(TextColors.GRAY, "You have removed ", TextColors.GOLD, amountText, TextColors.GRAY, " from ", TextColors.GOLD, recipient.getName(), "'s", TextColors.GRAY, " account."));
                if (recipient.isOnline()) {
                    recipient.getPlayer().get().sendMessage(Text.of(TextColors.GOLD, amountText, TextColors.GRAY, " has been removed from your account by ", TextColors.GOLD, src.getName(), TextColors.GRAY, "."));
                }
            }
            return CommandResult.success();
        }
    } else {
        throw new CommandException(Text.of("Invalid amount! Must be a number!"));
    }
    return CommandResult.empty();
}
Also used : Pattern(java.util.regex.Pattern) TransactionResult(org.spongepowered.api.service.economy.transaction.TransactionResult) User(org.spongepowered.api.entity.living.player.User) Matcher(java.util.regex.Matcher) Text(org.spongepowered.api.text.Text) CommandException(org.spongepowered.api.command.CommandException) TEAccount(com.erigitic.config.TEAccount) BigDecimal(java.math.BigDecimal)

Aggregations

TransactionResult (org.spongepowered.api.service.economy.transaction.TransactionResult)7 TEAccount (com.erigitic.config.TEAccount)2 SQLQuery (com.erigitic.sql.SQLQuery)2 BigDecimal (java.math.BigDecimal)2 ClientboundPlayerCurrencyPacket (com.almuradev.almura.feature.hud.network.ClientboundPlayerCurrencyPacket)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 CommandException (org.spongepowered.api.command.CommandException)1 Player (org.spongepowered.api.entity.living.player.Player)1 User (org.spongepowered.api.entity.living.player.User)1 Listener (org.spongepowered.api.event.Listener)1 Scheduler (org.spongepowered.api.scheduler.Scheduler)1 Task (org.spongepowered.api.scheduler.Task)1 Account (org.spongepowered.api.service.economy.account.Account)1 UniqueAccount (org.spongepowered.api.service.economy.account.UniqueAccount)1 Text (org.spongepowered.api.text.Text)1