Search in sources :

Example 1 with TransferResult

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

the class TEAccount method transfer.

@Override
public TransferResult transfer(Account to, Currency currency, BigDecimal amount, Cause cause, Set<Context> contexts) {
    TransferResult transferResult;
    if (hasBalance(currency, contexts)) {
        BigDecimal curBalance = getBalance(currency, contexts);
        BigDecimal newBalance = curBalance.subtract(amount);
        if (newBalance.compareTo(BigDecimal.ZERO) >= 0) {
            withdraw(currency, amount, cause, contexts);
            if (to.hasBalance(currency)) {
                to.deposit(currency, amount, cause, contexts);
                transferResult = new TETransferResult(this, to, currency, amount, contexts, ResultType.SUCCESS, TransactionTypes.TRANSFER);
                totalEconomy.getGame().getEventManager().post(new TEEconomyTransactionEvent(transferResult));
                return transferResult;
            } else {
                transferResult = new TETransferResult(this, to, currency, amount, contexts, ResultType.FAILED, TransactionTypes.TRANSFER);
                totalEconomy.getGame().getEventManager().post(new TEEconomyTransactionEvent(transferResult));
                return transferResult;
            }
        } else {
            transferResult = new TETransferResult(this, to, currency, amount, contexts, ResultType.ACCOUNT_NO_FUNDS, TransactionTypes.TRANSFER);
            totalEconomy.getGame().getEventManager().post(new TEEconomyTransactionEvent(transferResult));
            return transferResult;
        }
    }
    transferResult = new TETransferResult(this, to, currency, amount, contexts, ResultType.FAILED, TransactionTypes.TRANSFER);
    totalEconomy.getGame().getEventManager().post(new TEEconomyTransactionEvent(transferResult));
    return transferResult;
}
Also used : TransferResult(org.spongepowered.api.service.economy.transaction.TransferResult) BigDecimal(java.math.BigDecimal)

Example 2 with TransferResult

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

the class PayCommand method execute.

@Override
public CommandResult execute(CommandSource src, CommandContext args) throws CommandException {
    String strAmount = (String) args.getOne("amount").get();
    Player recipient = (Player) args.getOne("player").get();
    if (src instanceof Player) {
        Player sender = (Player) src;
        if (sender.getUniqueId().equals(recipient.getUniqueId())) {
            throw new CommandException(Text.of("You cannot pay yourself!"));
        }
        Pattern amountPattern = Pattern.compile("^[+]?(\\d*\\.)?\\d+$");
        Matcher m = amountPattern.matcher(strAmount);
        if (m.matches()) {
            BigDecimal amount = new BigDecimal(strAmount).setScale(2, BigDecimal.ROUND_DOWN);
            TEAccount playerAccount = (TEAccount) accountManager.getOrCreateAccount(sender.getUniqueId()).get();
            TEAccount recipientAccount = (TEAccount) accountManager.getOrCreateAccount(recipient.getUniqueId()).get();
            TransferResult transferResult = playerAccount.transfer(recipientAccount, totalEconomy.getDefaultCurrency(), amount, Cause.of(NamedCause.of("TotalEconomy", totalEconomy.getPluginContainer())));
            if (transferResult.getResult() == ResultType.SUCCESS) {
                sender.sendMessage(Text.of(TextColors.GRAY, "You have sent ", TextColors.GOLD, defaultCurrency.format(amount), TextColors.GRAY, " to ", TextColors.GOLD, recipient.getName(), TextColors.GRAY, "."));
                recipient.sendMessage(Text.of(TextColors.GRAY, "You have received ", TextColors.GOLD, defaultCurrency.format(amount), TextColors.GRAY, " from ", TextColors.GOLD, sender.getName(), TextColors.GRAY, "."));
                return CommandResult.success();
            } else if (transferResult.getResult() == ResultType.ACCOUNT_NO_FUNDS) {
                throw new CommandException(Text.of("Insufficient funds!"));
            }
        } else {
            throw new CommandException(Text.of("Invalid amount! Must be a positive number!"));
        }
    }
    return CommandResult.empty();
}
Also used : Pattern(java.util.regex.Pattern) Player(org.spongepowered.api.entity.living.player.Player) Matcher(java.util.regex.Matcher) CommandException(org.spongepowered.api.command.CommandException) TEAccount(com.erigitic.config.TEAccount) TransferResult(org.spongepowered.api.service.economy.transaction.TransferResult) BigDecimal(java.math.BigDecimal)

Example 3 with TransferResult

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

the class TEVirtualAccount method transfer.

@Override
public TransferResult transfer(Account to, Currency currency, BigDecimal amount, Cause cause, Set<Context> contexts) {
    TransferResult transferResult;
    if (hasBalance(currency, contexts)) {
        BigDecimal curBalance = getBalance(currency, contexts);
        BigDecimal newBalance = curBalance.subtract(amount);
        if (newBalance.compareTo(BigDecimal.ZERO) >= 0) {
            withdraw(currency, amount, cause, contexts);
            if (to.hasBalance(currency)) {
                to.deposit(currency, amount, cause, contexts);
                transferResult = new TETransferResult(this, to, currency, amount, contexts, ResultType.SUCCESS, TransactionTypes.TRANSFER);
                totalEconomy.getGame().getEventManager().post(new TEEconomyTransactionEvent(transferResult));
                return transferResult;
            } else {
                transferResult = new TETransferResult(this, to, currency, amount, contexts, ResultType.FAILED, TransactionTypes.TRANSFER);
                totalEconomy.getGame().getEventManager().post(new TEEconomyTransactionEvent(transferResult));
                return transferResult;
            }
        } else {
            transferResult = new TETransferResult(this, to, currency, amount, contexts, ResultType.ACCOUNT_NO_FUNDS, TransactionTypes.TRANSFER);
            totalEconomy.getGame().getEventManager().post(new TEEconomyTransactionEvent(transferResult));
            return transferResult;
        }
    }
    transferResult = new TETransferResult(this, to, currency, amount, contexts, ResultType.FAILED, TransactionTypes.TRANSFER);
    totalEconomy.getGame().getEventManager().post(new TEEconomyTransactionEvent(transferResult));
    return transferResult;
}
Also used : TransferResult(org.spongepowered.api.service.economy.transaction.TransferResult) BigDecimal(java.math.BigDecimal)

Aggregations

BigDecimal (java.math.BigDecimal)3 TransferResult (org.spongepowered.api.service.economy.transaction.TransferResult)3 TEAccount (com.erigitic.config.TEAccount)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