use of com.erigitic.config.TECurrency in project TotalEconomy by Erigitic.
the class ViewBalanceCommand method execute.
@Override
public CommandResult execute(CommandSource sender, CommandContext args) throws CommandException {
User recipient = args.<User>getOne("player").get();
Optional<String> optCurrencyName = args.getOne("currencyName");
TEAccount recipientAccount = (TEAccount) accountManager.getOrCreateAccount(recipient.getUniqueId()).get();
TransactionResult transactionResult = getTransactionResult(recipientAccount, optCurrencyName);
if (transactionResult.getResult() == ResultType.SUCCESS) {
TECurrency currency = (TECurrency) transactionResult.getCurrency();
Text balanceText = currency.format(recipientAccount.getBalance(currency));
Map<String, String> messageValues = new HashMap<>();
messageValues.put("recipient", recipient.getName());
messageValues.put("amount", balanceText.toPlain());
sender.sendMessage(messageManager.getMessage("command.viewbalance", messageValues));
return CommandResult.success();
} else {
throw new CommandException(Text.of("[TE] An error occurred setting a player's balance!"));
}
}
use of com.erigitic.config.TECurrency in project TotalEconomy by Erigitic.
the class TotalEconomy method loadCurrencies.
/**
* Load each currency from the default configuration file. Sets the default currency.
*/
private void loadCurrencies() {
config.getNode("currency").getChildrenMap().keySet().forEach(currencyName -> {
ConfigurationNode currencyNode = config.getNode("currency", currencyName.toString());
String currencySingular = currencyNode.getNode("currency-singular").getString();
String currencyPlural = currencyNode.getNode("currency-plural").getString();
String currencySymbol = currencyNode.getNode("symbol").getString();
boolean isDefault = currencyNode.getNode("default").getBoolean();
boolean prefixSymbol = currencyNode.getNode("prefix-symbol").getBoolean();
boolean isTransferable = currencyNode.getNode("transferable").getBoolean();
BigDecimal startBalance = new BigDecimal(currencyNode.getNode("startbalance").getDouble());
TECurrency currency = new TECurrency(Text.of(currencySingular), Text.of(currencyPlural), Text.of(currencySymbol), 2, isDefault, prefixSymbol, isTransferable, startBalance);
if (isDefault) {
defaultCurrency = currency;
}
currencies.add(currency);
});
}
use of com.erigitic.config.TECurrency in project TotalEconomy by Erigitic.
the class ViewBalanceCommand method getTransactionResult.
/**
* Get the transaction result returned from setting a player's balance.
*
* @param recipientAccount The account
* @param optCurrencyName The currency name
* @return TransactionResult Result of set balance
* @throws CommandException
*/
private TransactionResult getTransactionResult(TEAccount recipientAccount, Optional<String> optCurrencyName) throws CommandException {
Cause cause = Cause.builder().append(totalEconomy.getPluginContainer()).build(EventContext.empty());
if (optCurrencyName.isPresent()) {
Optional<Currency> optCurrency = totalEconomy.getTECurrencyRegistryModule().getById("totaleconomy:" + optCurrencyName.get().toLowerCase());
if (optCurrency.isPresent()) {
TECurrency currency = (TECurrency) optCurrency.get();
BigDecimal balance = recipientAccount.getBalance(currency);
return recipientAccount.setBalance(currency, balance, cause);
} else {
throw new CommandException(Text.of(TextColors.RED, "[TE] The specified currency does not exist!"));
}
} else {
BigDecimal balance = recipientAccount.getBalance(totalEconomy.getDefaultCurrency());
return recipientAccount.setBalance(totalEconomy.getDefaultCurrency(), balance, cause);
}
}
use of com.erigitic.config.TECurrency in project TotalEconomy by Erigitic.
the class BalanceCommand method execute.
@Override
public CommandResult execute(CommandSource src, CommandContext args) throws CommandException {
if (src instanceof Player) {
Player sender = (Player) src;
TEAccount playerAccount = (TEAccount) accountManager.getOrCreateAccount(sender.getUniqueId()).get();
Optional<String> optCurrencyName = args.getOne("currencyName");
Map<String, String> messageValues = new HashMap<>();
if (optCurrencyName.isPresent()) {
Optional<Currency> optCurrency = totalEconomy.getTECurrencyRegistryModule().getById("totaleconomy:" + optCurrencyName.get().toLowerCase());
if (optCurrency.isPresent()) {
TECurrency currency = (TECurrency) optCurrency.get();
messageValues.put("currency", currency.getName());
messageValues.put("amount", currency.format(playerAccount.getBalance(currency)).toPlain());
sender.sendMessage(messageManager.getMessage("command.balance.other", messageValues));
} else {
throw new CommandException(Text.of(TextColors.RED, "[TE] The specified currency does not exist!"));
}
} else {
messageValues.put("amount", defaultCurrency.format(playerAccount.getBalance(defaultCurrency)).toPlain());
sender.sendMessage(messageManager.getMessage("command.balance.default", messageValues));
}
return CommandResult.success();
} else {
throw new CommandException(Text.of("[TE] This command can only be run by a player!"));
}
}
Aggregations