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