use of org.spongepowered.api.service.economy.Currency in project TotalEconomy by Erigitic.
the class AccountManager method addNewCurrenciesToAccount.
/**
* Checks if a virtual account has a balance for each currency. If one doesn't exist, a new balance for that currency will be
* added and set to that currencies starting balance.
*
* @param virtualAccount The virtual account to add the balance to
* @throws IOException
*/
private void addNewCurrenciesToAccount(TEVirtualAccount virtualAccount) throws IOException {
String identifier = virtualAccount.getIdentifier();
for (Currency currency : totalEconomy.getCurrencies()) {
TECurrency teCurrency = (TECurrency) currency;
if (!virtualAccount.hasBalance(teCurrency)) {
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 AccountManager method createAccountInDatabase.
/**
* Creates a new unique account in the database.
*
* @param playerAccount A player's account
* @throws IOException
*/
private void createAccountInDatabase(TEAccount playerAccount) {
UUID uuid = playerAccount.getUniqueId();
SQLQuery.builder(sqlManager.dataSource).insert("accounts").columns("uid", "job", "job_notifications").values(uuid.toString(), "unemployed", String.valueOf(totalEconomy.isJobNotificationEnabled())).build();
SQLQuery.builder(sqlManager.dataSource).insert("levels").columns("uid").values(uuid.toString()).build();
SQLQuery.builder(sqlManager.dataSource).insert("experience").columns("uid").values(uuid.toString()).build();
for (Currency currency : totalEconomy.getCurrencies()) {
TECurrency teCurrency = (TECurrency) currency;
SQLQuery.builder(sqlManager.dataSource).update("accounts").set(teCurrency.getName().toLowerCase() + "_balance").equals(playerAccount.getDefaultBalance(teCurrency).toString()).where("uid").equals(uuid.toString()).build();
}
}
use of org.spongepowered.api.service.economy.Currency in project TotalEconomy by Erigitic.
the class AccountManager method addNewCurrenciesToAccount.
/**
* Checks if a unique account has a balance for each currency. If one doesn't exist, a new balance for that currency will be
* added and set to that currencies starting balance.
*
* @param playerAccount The unique account to add the balance to
* @throws IOException
*/
private void addNewCurrenciesToAccount(TEAccount playerAccount) throws IOException {
UUID uuid = playerAccount.getUniqueId();
for (Currency currency : totalEconomy.getCurrencies()) {
TECurrency teCurrency = (TECurrency) currency;
if (!playerAccount.hasBalance(teCurrency)) {
accountConfig.getNode(uuid.toString(), teCurrency.getName().toLowerCase() + "-balance").setValue(playerAccount.getDefaultBalance(teCurrency));
}
}
loader.save(accountConfig);
}
use of org.spongepowered.api.service.economy.Currency in project TotalEconomy by Erigitic.
the class AccountManager method createAccountInConfig.
/**
* Creates a new unique account in the accounts configuration file.
*
* @param playerAccount A player's account
* @throws IOException
*/
private void createAccountInConfig(TEAccount playerAccount) throws IOException {
UUID uuid = playerAccount.getUniqueId();
for (Currency currency : totalEconomy.getCurrencies()) {
TECurrency teCurrency = (TECurrency) currency;
accountConfig.getNode(uuid.toString(), teCurrency.getName().toLowerCase() + "-balance").setValue(playerAccount.getDefaultBalance(teCurrency));
}
accountConfig.getNode(uuid.toString(), "job").setValue("unemployed");
accountConfig.getNode(uuid.toString(), "jobnotifications").setValue(totalEconomy.isJobNotificationEnabled());
loader.save(accountConfig);
}
use of org.spongepowered.api.service.economy.Currency in project TotalEconomy by Erigitic.
the class JobManager method onPlayerPlaceBlock.
/**
* Used for the place option in jobs. Will check if the job has the place node and if it does it will check if the
* block that was placed is present in the config of the player's job. If it is, it will grab the job exp reward as
* well as the pay.
*
* @param event ChangeBlockEvent.Place
*/
@Listener
public void onPlayerPlaceBlock(ChangeBlockEvent.Place event) {
if (event.getCause().first(Player.class).isPresent()) {
Player player = event.getCause().first(Player.class).get();
UUID playerUUID = player.getUniqueId();
String playerJob = getPlayerJob(player);
Optional<TEJob> optPlayerJob = getJob(playerJob, true);
BlockState state = event.getTransactions().get(0).getFinal().getState();
String blockName = state.getType().getName();
// Enable admins to determine block information by displaying it to them - WHEN they have the flag enabled
if (accountManager.getUserOption("totaleconomy:block-place-info", player).orElse("0").equals("1")) {
List<BlockTrait<?>> traits = new ArrayList<>(state.getTraits());
int count = traits.size();
List<Text> traitTexts = new ArrayList<>(count);
for (int i = 0; i < count; i++) {
Object traitValue = state.getTraitValue(traits.get(i)).orElse(null);
traitTexts.add(i, Text.of(traits.get(i).getName(), '=', traitValue != null ? traitValue.toString() : "null"));
}
Text t = Text.of(TextColors.GRAY, "TRAITS:\n ", Text.joinWith(Text.of(",\n "), traitTexts.toArray(new Text[traits.size()])));
player.sendMessage(Text.of("Block-Name: ", blockName));
player.sendMessage(t);
}
if (optPlayerJob.isPresent()) {
Optional<TEActionReward> reward = Optional.empty();
List<String> sets = optPlayerJob.get().getSets();
for (String s : sets) {
Optional<TEJobSet> optSet = getJobSet(s);
if (!optSet.isPresent()) {
logger.warn("Job " + playerJob + " has the nonexistent set \"" + s + "\"");
continue;
}
Optional<TEAction> action = optSet.get().getActionFor("place", blockName);
if (!action.isPresent()) {
continue;
}
Optional<TEActionReward> currentReward = action.get().evaluatePlace(logger, state);
if (!reward.isPresent()) {
reward = currentReward;
continue;
}
if (!currentReward.isPresent()) {
continue;
}
// Use the one giving higher exp in case of duplicates
if (currentReward.get().getExpReward() > reward.get().getExpReward()) {
reward = currentReward;
}
}
if (reward.isPresent()) {
TEAccount playerAccount = (TEAccount) accountManager.getOrCreateAccount(player.getUniqueId()).get();
boolean notify = getNotificationState(playerUUID);
int expAmount = reward.get().getExpReward();
BigDecimal payAmount = new BigDecimal(reward.get().getMoneyReward());
Currency currency = totalEconomy.getDefaultCurrency();
if (reward.get().getCurrencyId() != null) {
Optional<Currency> currencyOpt = totalEconomy.getTECurrencyRegistryModule().getById("totaleconomy:" + reward.get().getCurrencyId());
if (currencyOpt.isPresent()) {
currency = currencyOpt.get();
}
}
if (notify) {
notifyPlayer(player, payAmount, currency);
}
addExp(player, expAmount);
playerAccount.deposit(currency, payAmount, event.getCause());
checkForLevel(player);
}
}
}
}
Aggregations