Search in sources :

Example 1 with FishData

use of org.spongepowered.api.data.manipulator.mutable.item.FishData in project TotalEconomy by Erigitic.

the class TEJobManager method onPlayerFish.

/**
     * Used for the catch option in jobs. Will check if the job has the catch node and if it does it will check if the
     * item that was caught 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 FishingEvent.Stop
     */
@Listener
public void onPlayerFish(FishingEvent.Stop event) {
    if (event.getCause().first(Player.class).isPresent()) {
        // no transaction, so execution can stop
        if (event.getItemStackTransaction().size() == 0) {
            return;
        }
        Transaction<ItemStackSnapshot> itemTransaction = event.getItemStackTransaction().get(0);
        ItemStack itemStack = itemTransaction.getFinal().createStack();
        Player player = event.getCause().first(Player.class).get();
        UUID playerUUID = player.getUniqueId();
        String playerJob = getPlayerJob(player);
        Optional<TEJob> optPlayerJob = getJob(playerJob, true);
        if (optPlayerJob.isPresent()) {
            if (itemStack.get(FishData.class).isPresent()) {
                FishData fishData = itemStack.get(FishData.class).get();
                String fishName = fishData.type().get().getName();
                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("[TE] Job " + getPlayerJob(player) + " has nonexistent set \"" + s + "\"");
                        continue;
                    }
                    reward = optSet.get().getRewardFor("catch", fishName);
                }
                if (reward.isPresent()) {
                    int expAmount = reward.get().getExpReward();
                    BigDecimal payAmount = reward.get().getMoneyReward();
                    boolean notify = accountConfig.getNode(playerUUID.toString(), "jobnotifications").getBoolean();
                    TEAccount playerAccount = (TEAccount) accountManager.getOrCreateAccount(player.getUniqueId()).get();
                    if (notify) {
                        notifyPlayer(player, payAmount);
                    }
                    addExp(player, expAmount);
                    playerAccount.deposit(totalEconomy.getDefaultCurrency(), payAmount, Cause.of(NamedCause.of("TotalEconomy", totalEconomy.getPluginContainer())));
                    checkForLevel(player);
                }
            }
        }
    }
}
Also used : Player(org.spongepowered.api.entity.living.player.Player) TEAccount(com.erigitic.config.TEAccount) BigDecimal(java.math.BigDecimal) FishData(org.spongepowered.api.data.manipulator.mutable.item.FishData) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) ItemStack(org.spongepowered.api.item.inventory.ItemStack) Listener(org.spongepowered.api.event.Listener)

Example 2 with FishData

use of org.spongepowered.api.data.manipulator.mutable.item.FishData in project TotalEconomy by Erigitic.

the class JobManager method onPlayerFish.

/**
 * Used for the catch option in jobs. Will check if the job has the catch node and if it does it will check if the
 * item that was caught 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 FishingEvent.Stop
 */
@Listener
public void onPlayerFish(FishingEvent.Stop event) {
    if (event.getCause().first(Player.class).isPresent()) {
        // no transaction, so execution can stop
        if (event.getTransactions().size() == 0) {
            return;
        }
        Transaction<ItemStackSnapshot> itemTransaction = event.getItemStackTransaction().get(0);
        ItemStack itemStack = itemTransaction.getFinal().createStack();
        Player player = event.getCause().first(Player.class).get();
        UUID playerUUID = player.getUniqueId();
        String playerJob = getPlayerJob(player);
        Optional<TEJob> optPlayerJob = getJob(playerJob, true);
        if (optPlayerJob.isPresent()) {
            if (itemStack.get(FishData.class).isPresent()) {
                FishData fishData = itemStack.get(FishData.class).get();
                String fishName = fishData.type().get().getName();
                // Enable admins to determine fish information by displaying it to them - WHEN they have the flag enabled
                if (accountManager.getUserOption("totaleconomy:entity-fish-info", player).orElse("0").equals("1")) {
                    player.sendMessage(Text.of("Fish-Name: ", fishName));
                }
                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("catch", fishName);
                    if (!action.isPresent()) {
                        continue;
                    }
                    Optional<TEActionReward> currentReward = action.get().getReward();
                    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 : Player(org.spongepowered.api.entity.living.player.Player) TEAccount(com.erigitic.config.TEAccount) BigDecimal(java.math.BigDecimal) FishData(org.spongepowered.api.data.manipulator.mutable.item.FishData) Currency(org.spongepowered.api.service.economy.Currency) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) ItemStack(org.spongepowered.api.item.inventory.ItemStack) Listener(org.spongepowered.api.event.Listener)

Aggregations

TEAccount (com.erigitic.config.TEAccount)2 BigDecimal (java.math.BigDecimal)2 FishData (org.spongepowered.api.data.manipulator.mutable.item.FishData)2 Player (org.spongepowered.api.entity.living.player.Player)2 Listener (org.spongepowered.api.event.Listener)2 ItemStack (org.spongepowered.api.item.inventory.ItemStack)2 ItemStackSnapshot (org.spongepowered.api.item.inventory.ItemStackSnapshot)2 Currency (org.spongepowered.api.service.economy.Currency)1