Search in sources :

Example 1 with TEAccount

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

the class TEJobManager 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);
        String blockName = event.getTransactions().get(0).getFinal().getState().getType().getName();
        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("[TE] Job " + getPlayerJob(player) + " has nonexistent set \"" + s + "\"");
                    continue;
                }
                reward = optSet.get().getRewardFor("place", blockName);
            }
            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) Listener(org.spongepowered.api.event.Listener)

Example 2 with TEAccount

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

the class TEJobManager 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);
            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("[TE] Job " + getPlayerJob(player) + " has nonexistent set \"" + s + "\"");
                        continue;
                    }
                    reward = optSet.get().getRewardFor("kill", victimName);
                }
                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 : 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) Listener(org.spongepowered.api.event.Listener)

Example 3 with TEAccount

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

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

the class TEJobManager method startSalaryTask.

/**
     * Start the timer that pays out the salary to each player after a specified time in seconds
     */
private void startSalaryTask() {
    Scheduler scheduler = totalEconomy.getGame().getScheduler();
    Task.Builder payTask = scheduler.createTaskBuilder();
    payTask.execute(() -> {
        for (Player player : totalEconomy.getServer().getOnlinePlayers()) {
            Optional<TEJob> optJob = getJob(getPlayerJob(player), true);
            if (!optJob.isPresent()) {
                player.sendMessage(Text.of(TextColors.RED, "[TE] Cannot pay your salary! Contact your administrator!"));
                return;
            }
            if (optJob.get().salaryEnabled()) {
                BigDecimal salary = optJob.get().getSalary();
                TEAccount playerAccount = (TEAccount) accountManager.getOrCreateAccount(player.getUniqueId()).get();
                TransactionResult result = playerAccount.deposit(totalEconomy.getDefaultCurrency(), salary, Cause.of(NamedCause.of("TotalEconomy", totalEconomy.getPluginContainer())));
                if (result.getResult() == ResultType.SUCCESS) {
                    player.sendMessage(Text.of(TextColors.GRAY, "Your salary of ", TextColors.GOLD, totalEconomy.getDefaultCurrency().format(salary), TextColors.GRAY, " has just been paid."));
                } else {
                    player.sendMessage(Text.of(TextColors.RED, "[TE] Failed to pay your salary! You may want to contact your admin - TransactionResult: ", result.getResult().toString()));
                }
            }
        }
    }).delay(jobsConfig.getNode("salarydelay").getInt(), TimeUnit.SECONDS).interval(jobsConfig.getNode("salarydelay").getInt(), TimeUnit.SECONDS).name("Pay Day").submit(totalEconomy);
}
Also used : Task(org.spongepowered.api.scheduler.Task) Player(org.spongepowered.api.entity.living.player.Player) TransactionResult(org.spongepowered.api.service.economy.transaction.TransactionResult) Scheduler(org.spongepowered.api.scheduler.Scheduler) TEAccount(com.erigitic.config.TEAccount) BigDecimal(java.math.BigDecimal)

Example 5 with TEAccount

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

the class PayCommand method execute.

@Override
public CommandResult execute(CommandSource src, CommandContext args) throws CommandException {
    String amountStr = (String) args.getOne("amount").get();
    Player recipient = (Player) args.getOne("player").get();
    Optional<String> optCurrencyName = args.getOne("currencyName");
    if (src instanceof Player) {
        Player sender = (Player) src;
        if (sender.getUniqueId().equals(recipient.getUniqueId())) {
            throw new CommandException(Text.of("[TE] You cannot pay yourself!"));
        }
        // Positive numbers only
        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 senderAccount = (TEAccount) accountManager.getOrCreateAccount(sender.getUniqueId()).get();
            TEAccount recipientAccount = (TEAccount) accountManager.getOrCreateAccount(recipient.getUniqueId()).get();
            TransferResult transferResult = getTransferResult(senderAccount, recipientAccount, amount, optCurrencyName);
            if (transferResult.getResult() == ResultType.SUCCESS) {
                Text amountText = Text.of(transferResult.getCurrency().format(amount));
                Map<String, String> messageValues = new HashMap<>();
                messageValues.put("sender", src.getName());
                messageValues.put("recipient", recipient.getName());
                messageValues.put("amount", amountText.toPlain());
                sender.sendMessage(messageManager.getMessage("command.pay.sender", messageValues));
                recipient.sendMessage(messageManager.getMessage("command.pay.recipient", messageValues));
                return CommandResult.success();
            } else if (transferResult.getResult() == ResultType.ACCOUNT_NO_FUNDS) {
                throw new CommandException(Text.of("[TE] Insufficient funds!"));
            } else {
                throw new CommandException(Text.of("[TE] An error occurred while paying another player!"));
            }
        } else {
            throw new CommandException(Text.of("[TE] Invalid amount! Must be a positive number!"));
        }
    } else {
        throw new CommandException(Text.of("[TE] This command can only be run by a player!"));
    }
}
Also used : Pattern(java.util.regex.Pattern) Player(org.spongepowered.api.entity.living.player.Player) 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) TransferResult(org.spongepowered.api.service.economy.transaction.TransferResult) 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