Search in sources :

Example 16 with Currency

use of org.spongepowered.api.service.economy.Currency 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 17 with Currency

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

the class AccountManager method setupDatabase.

/**
 * Setup the database that will contain the user accounts
 */
public void setupDatabase() {
    String currencyCols = "";
    for (Currency currency : getCurrencies()) {
        TECurrency teCurrency = (TECurrency) currency;
        currencyCols += teCurrency.getName().toLowerCase() + "_balance decimal(19,2) NOT NULL DEFAULT '" + teCurrency.getStartingBalance() + "',";
    }
    sqlManager.createTable("accounts", "uid varchar(60) NOT NULL," + currencyCols + "job varchar(50) NOT NULL DEFAULT 'Unemployed'," + "job_notifications boolean NOT NULL DEFAULT TRUE," + "PRIMARY KEY (uid)");
    sqlManager.createTable("virtual_accounts", "uid varchar(60) NOT NULL," + currencyCols + "PRIMARY KEY (uid)");
    sqlManager.createTable("levels", "uid varchar(60)," + "miner int(10) unsigned NOT NULL DEFAULT '1'," + "lumberjack int(10) unsigned NOT NULL DEFAULT '1'," + "warrior int(10) unsigned NOT NULL DEFAULT '1'," + "fisherman int(10) unsigned NOT NULL DEFAULT '1'," + "FOREIGN KEY (uid) REFERENCES accounts(uid) ON DELETE CASCADE");
    sqlManager.createTable("experience", "uid varchar(60)," + "miner int(10) unsigned NOT NULL DEFAULT '0'," + "lumberjack int(10) unsigned NOT NULL DEFAULT '0'," + "warrior int(10) unsigned NOT NULL DEFAULT '0'," + "fisherman int(10) unsigned NOT NULL DEFAULT '0'," + "FOREIGN KEY (uid) REFERENCES accounts(uid) ON DELETE CASCADE");
}
Also used : Currency(org.spongepowered.api.service.economy.Currency)

Example 18 with Currency

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

the class AccountManager method createAccountInDatabase.

/**
 * Creates a new virtual account in the database.
 *
 * @param virtualAccount A virtual account
 * @throws IOException
 */
private void createAccountInDatabase(TEVirtualAccount virtualAccount) {
    String identifier = virtualAccount.getIdentifier();
    for (Currency currency : totalEconomy.getCurrencies()) {
        TECurrency teCurrency = (TECurrency) currency;
        SQLQuery.builder(sqlManager.dataSource).insert("virtual_accounts").columns(teCurrency.getName().toLowerCase() + "_balance").values(virtualAccount.getDefaultBalance(teCurrency).toString()).where("uid").equals(identifier).build();
    }
}
Also used : Currency(org.spongepowered.api.service.economy.Currency)

Example 19 with Currency

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

the class AccountManager method createAccountInConfig.

/**
 * Creates a new virtual account in the accounts configuration file.
 *
 * @param virtualAccount A virtual account
 * @throws IOException
 */
private void createAccountInConfig(TEVirtualAccount virtualAccount) throws IOException {
    String identifier = virtualAccount.getIdentifier();
    for (Currency currency : totalEconomy.getCurrencies()) {
        TECurrency teCurrency = (TECurrency) currency;
        accountConfig.getNode(identifier, teCurrency.getName().toLowerCase() + "-balance").setValue(virtualAccount.getDefaultBalance(teCurrency));
    }
    loader.save(accountConfig);
}
Also used : Currency(org.spongepowered.api.service.economy.Currency)

Example 20 with Currency

use of org.spongepowered.api.service.economy.Currency 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

Currency (org.spongepowered.api.service.economy.Currency)21 BigDecimal (java.math.BigDecimal)11 TEAccount (com.erigitic.config.TEAccount)6 Player (org.spongepowered.api.entity.living.player.Player)6 EconomyService (org.spongepowered.api.service.economy.EconomyService)6 Listener (org.spongepowered.api.event.Listener)5 Account (org.spongepowered.api.service.economy.account.Account)5 UUID (java.util.UUID)4 TECurrency (com.erigitic.config.TECurrency)3 Text (org.spongepowered.api.text.Text)3 LuckPerms (net.luckperms.api.LuckPerms)2 BlockState (org.spongepowered.api.block.BlockState)2 BlockTrait (org.spongepowered.api.block.trait.BlockTrait)2 CommandException (org.spongepowered.api.command.CommandException)2 UniqueAccount (org.spongepowered.api.service.economy.account.UniqueAccount)2 ClientboundClaimDataPacket (com.almuradev.almura.feature.claim.network.ClientboundClaimDataPacket)1 ClientboundPlayerDiedPacket (com.almuradev.almura.feature.death.network.ClientboundPlayerDiedPacket)1 ClientboundPlayerCurrencyPacket (com.almuradev.almura.feature.hud.network.ClientboundPlayerCurrencyPacket)1 ClientboundMembershipGuiOpenPacket (com.almuradev.almura.feature.membership.network.ClientboundMembershipGuiOpenPacket)1 ClientboundMembershipSuccessPacket (com.almuradev.almura.feature.membership.network.ClientboundMembershipSuccessPacket)1