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;
}
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();
}
Aggregations