Search in sources :

Example 11 with TEAccount

use of com.erigitic.config.TEAccount in project TotalEconomy by Erigitic.

the class JobManager method onPlayerBlockBreak.

/**
 * Used for the break option in jobs. Will check if the job has the break node and if it does it will check if the
 * block that was broken 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.Break
 */
@Listener
public void onPlayerBlockBreak(ChangeBlockEvent.Break 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).getOriginal().getState();
        String blockName = state.getType().getName();
        Optional<UUID> blockCreator = event.getTransactions().get(0).getOriginal().getCreator();
        // Enable admins to determine block information by displaying it to them - WHEN they have the flag enabled
        if (accountManager.getUserOption("totaleconomy:block-break-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("break", blockName);
                if (!action.isPresent()) {
                    continue;
                }
                Optional<TEActionReward> currentReward = action.get().evaluateBreak(logger, state, blockCreator.orElse(null));
                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)

Example 12 with TEAccount

use of com.erigitic.config.TEAccount 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)

Example 13 with TEAccount

use of com.erigitic.config.TEAccount in project TotalEconomy by Erigitic.

the class JobManager method onPlayerKillEntity.

/**
 * Used for the break option in jobs. Will check if the job has the break node and if it does it will check if the
 * block that was broken 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 DestructEntityEvent.Death
 */
@Listener
public void onPlayerKillEntity(DestructEntityEvent.Death event) {
    Optional<EntityDamageSource> optDamageSource = event.getCause().first(EntityDamageSource.class);
    if (optDamageSource.isPresent()) {
        EntityDamageSource damageSource = optDamageSource.get();
        Entity killer = damageSource.getSource();
        Entity victim = event.getTargetEntity();
        if (!(killer instanceof Player)) {
            // If a projectile was shot to kill an entity, this will grab the player who shot it
            Optional<UUID> damageCreator = damageSource.getSource().getCreator();
            if (damageCreator.isPresent()) {
                killer = Sponge.getServer().getPlayer(damageCreator.get()).get();
            }
        }
        if (killer instanceof Player) {
            Player player = (Player) killer;
            UUID playerUUID = player.getUniqueId();
            String victimName = victim.getType().getName();
            String playerJob = getPlayerJob(player);
            Optional<TEJob> optPlayerJob = getJob(playerJob, true);
            // Enable admins to determine victim information by displaying it to them - WHEN they have the flag enabled
            if (accountManager.getUserOption("totaleconomy:entity-kill-info", player).orElse("0").equals("1")) {
                player.sendMessage(Text.of("Victim-Name: ", victimName));
            }
            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("kill", victimName);
                    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 : TileEntity(org.spongepowered.api.block.tileentity.TileEntity) Entity(org.spongepowered.api.entity.Entity) Player(org.spongepowered.api.entity.living.player.Player) TEAccount(com.erigitic.config.TEAccount) BigDecimal(java.math.BigDecimal) EntityDamageSource(org.spongepowered.api.event.cause.entity.damage.source.EntityDamageSource) Currency(org.spongepowered.api.service.economy.Currency) Listener(org.spongepowered.api.event.Listener)

Example 14 with TEAccount

use of com.erigitic.config.TEAccount in project TotalEconomy by Erigitic.

the class SetBalanceCommand method execute.

@Override
public CommandResult execute(CommandSource src, CommandContext args) throws CommandException {
    User recipient = args.<User>getOne("player").get();
    String amountStr = args.<String>getOne("amount").get();
    Optional<String> optCurrencyName = args.getOne("currencyName");
    Pattern amountPattern = Pattern.compile("^[+]?(\\d*\\.)?\\d+$");
    Matcher m = amountPattern.matcher(amountStr);
    if (m.matches()) {
        BigDecimal amount = new BigDecimal(amountStr).setScale(2, BigDecimal.ROUND_DOWN);
        TEAccount recipientAccount = (TEAccount) accountManager.getOrCreateAccount(recipient.getUniqueId()).get();
        TransactionResult transactionResult = getTransactionResult(recipientAccount, amount, optCurrencyName);
        if (transactionResult.getResult() == ResultType.SUCCESS) {
            Text amountText = Text.of(transactionResult.getCurrency().format(amount));
            Map<String, String> messageValues = new HashMap<>();
            messageValues.put("recipient", recipient.getName());
            messageValues.put("amount", amountText.toPlain());
            src.sendMessage(messageManager.getMessage("command.setbalance", messageValues));
            return CommandResult.success();
        } else {
            throw new CommandException(Text.of("[TE] An error occurred while setting a player's balance!"));
        }
    } else {
        throw new CommandException(Text.of("[TE] Invalid amount! Must be a positive number!"));
    }
}
Also used : Pattern(java.util.regex.Pattern) TransactionResult(org.spongepowered.api.service.economy.transaction.TransactionResult) User(org.spongepowered.api.entity.living.player.User) Matcher(java.util.regex.Matcher) HashMap(java.util.HashMap) Text(org.spongepowered.api.text.Text) CommandException(org.spongepowered.api.command.CommandException) TEAccount(com.erigitic.config.TEAccount) BigDecimal(java.math.BigDecimal)

Example 15 with TEAccount

use of com.erigitic.config.TEAccount in project TotalEconomy by Erigitic.

the class AdminPayCommand method execute.

@Override
public CommandResult execute(CommandSource src, CommandContext args) throws CommandException {
    String amountStr = (String) args.getOne("amount").get();
    User recipient = args.<User>getOne("player").get();
    Optional<String> optCurrencyName = args.getOne("currencyName");
    Pattern amountPattern = Pattern.compile("^[+-]?(\\d*\\.)?\\d+$");
    Matcher m = amountPattern.matcher(amountStr);
    if (m.matches()) {
        BigDecimal amount = new BigDecimal((String) args.getOne("amount").get()).setScale(2, BigDecimal.ROUND_DOWN);
        TEAccount recipientAccount = (TEAccount) accountManager.getOrCreateAccount(recipient.getUniqueId()).get();
        TransactionResult transactionResult = getTransactionResult(recipientAccount, amount, optCurrencyName);
        if (transactionResult.getResult() == ResultType.SUCCESS) {
            Text amountText = Text.of(transactionResult.getCurrency().format(amount).toPlain().replace("-", ""));
            Map<String, String> messageValues = new HashMap<>();
            messageValues.put("sender", src.getName());
            messageValues.put("recipient", recipient.getName());
            messageValues.put("amount", amountText.toPlain());
            if (!amountStr.contains("-")) {
                src.sendMessage(messageManager.getMessage("command.adminpay.send.sender", messageValues));
                if (recipient.isOnline()) {
                    recipient.getPlayer().get().sendMessage(messageManager.getMessage("command.adminpay.send.recipient", messageValues));
                }
            } else {
                src.sendMessage(messageManager.getMessage("command.adminpay.remove.sender", messageValues));
                if (recipient.isOnline()) {
                    recipient.getPlayer().get().sendMessage(messageManager.getMessage("command.adminpay.remove.recipient", messageValues));
                }
            }
            return CommandResult.success();
        } else {
            throw new CommandException(Text.of("[TE] An error occurred while paying a player!"));
        }
    } else {
        throw new CommandException(Text.of("[TE] Invalid amount! Must be a number!"));
    }
}
Also used : Pattern(java.util.regex.Pattern) TransactionResult(org.spongepowered.api.service.economy.transaction.TransactionResult) User(org.spongepowered.api.entity.living.player.User) Matcher(java.util.regex.Matcher) HashMap(java.util.HashMap) Text(org.spongepowered.api.text.Text) CommandException(org.spongepowered.api.command.CommandException) TEAccount(com.erigitic.config.TEAccount) BigDecimal(java.math.BigDecimal)

Aggregations

TEAccount (com.erigitic.config.TEAccount)18 BigDecimal (java.math.BigDecimal)14 Player (org.spongepowered.api.entity.living.player.Player)12 Listener (org.spongepowered.api.event.Listener)10 Text (org.spongepowered.api.text.Text)7 Currency (org.spongepowered.api.service.economy.Currency)6 HashMap (java.util.HashMap)5 CommandException (org.spongepowered.api.command.CommandException)5 TransactionResult (org.spongepowered.api.service.economy.transaction.TransactionResult)5 TECurrency (com.erigitic.config.TECurrency)3 Matcher (java.util.regex.Matcher)3 Pattern (java.util.regex.Pattern)3 User (org.spongepowered.api.entity.living.player.User)3 BlockState (org.spongepowered.api.block.BlockState)2 TileEntity (org.spongepowered.api.block.tileentity.TileEntity)2 BlockTrait (org.spongepowered.api.block.trait.BlockTrait)2 FishData (org.spongepowered.api.data.manipulator.mutable.item.FishData)2 Entity (org.spongepowered.api.entity.Entity)2 EntityDamageSource (org.spongepowered.api.event.cause.entity.damage.source.EntityDamageSource)2 ItemStack (org.spongepowered.api.item.inventory.ItemStack)2