Search in sources :

Example 1 with TECurrency

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!"));
    }
}
Also used : TransactionResult(org.spongepowered.api.service.economy.transaction.TransactionResult) User(org.spongepowered.api.entity.living.player.User) TECurrency(com.erigitic.config.TECurrency) HashMap(java.util.HashMap) Text(org.spongepowered.api.text.Text) CommandException(org.spongepowered.api.command.CommandException) TEAccount(com.erigitic.config.TEAccount)

Example 2 with TECurrency

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);
    });
}
Also used : TECurrency(com.erigitic.config.TECurrency) ConfigurationNode(ninja.leaping.configurate.ConfigurationNode) CommentedConfigurationNode(ninja.leaping.configurate.commented.CommentedConfigurationNode) BigDecimal(java.math.BigDecimal)

Example 3 with TECurrency

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);
    }
}
Also used : TECurrency(com.erigitic.config.TECurrency) Cause(org.spongepowered.api.event.cause.Cause) Currency(org.spongepowered.api.service.economy.Currency) TECurrency(com.erigitic.config.TECurrency) CommandException(org.spongepowered.api.command.CommandException) BigDecimal(java.math.BigDecimal)

Example 4 with TECurrency

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!"));
    }
}
Also used : Player(org.spongepowered.api.entity.living.player.Player) TECurrency(com.erigitic.config.TECurrency) HashMap(java.util.HashMap) Currency(org.spongepowered.api.service.economy.Currency) TECurrency(com.erigitic.config.TECurrency) CommandException(org.spongepowered.api.command.CommandException) TEAccount(com.erigitic.config.TEAccount)

Aggregations

TECurrency (com.erigitic.config.TECurrency)4 CommandException (org.spongepowered.api.command.CommandException)3 TEAccount (com.erigitic.config.TEAccount)2 BigDecimal (java.math.BigDecimal)2 HashMap (java.util.HashMap)2 Currency (org.spongepowered.api.service.economy.Currency)2 ConfigurationNode (ninja.leaping.configurate.ConfigurationNode)1 CommentedConfigurationNode (ninja.leaping.configurate.commented.CommentedConfigurationNode)1 Player (org.spongepowered.api.entity.living.player.Player)1 User (org.spongepowered.api.entity.living.player.User)1 Cause (org.spongepowered.api.event.cause.Cause)1 TransactionResult (org.spongepowered.api.service.economy.transaction.TransactionResult)1 Text (org.spongepowered.api.text.Text)1