Search in sources :

Example 1 with Currency

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);
}
Also used : Currency(org.spongepowered.api.service.economy.Currency)

Example 2 with Currency

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();
    }
}
Also used : Currency(org.spongepowered.api.service.economy.Currency) UUID(java.util.UUID)

Example 3 with Currency

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);
}
Also used : Currency(org.spongepowered.api.service.economy.Currency) UUID(java.util.UUID)

Example 4 with Currency

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);
}
Also used : Currency(org.spongepowered.api.service.economy.Currency) UUID(java.util.UUID)

Example 5 with Currency

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);
            }
        }
    }
}
Also used : BlockTrait(org.spongepowered.api.block.trait.BlockTrait) TEAccount(com.erigitic.config.TEAccount) Currency(org.spongepowered.api.service.economy.Currency) Player(org.spongepowered.api.entity.living.player.Player) Text(org.spongepowered.api.text.Text) BigDecimal(java.math.BigDecimal) BlockState(org.spongepowered.api.block.BlockState) Listener(org.spongepowered.api.event.Listener)

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