Search in sources :

Example 1 with EventContext

use of org.spongepowered.api.event.cause.EventContext in project TotalEconomy by Erigitic.

the class JobManager 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();
                EventContext eventContext = EventContext.builder().add(EventContextKeys.PLAYER, player).build();
                Cause cause = Cause.builder().append(totalEconomy.getPluginContainer()).build(eventContext);
                TransactionResult result = playerAccount.deposit(totalEconomy.getDefaultCurrency(), salary, cause);
                if (result.getResult() == ResultType.SUCCESS) {
                    Map<String, String> messageValues = new HashMap<>();
                    messageValues.put("amount", totalEconomy.getDefaultCurrency().format(salary).toPlain());
                    player.sendMessage(messageManager.getMessage("jobs.salary", messageValues));
                } 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) EventContext(org.spongepowered.api.event.cause.EventContext) Cause(org.spongepowered.api.event.cause.Cause)

Aggregations

TEAccount (com.erigitic.config.TEAccount)1 BigDecimal (java.math.BigDecimal)1 Player (org.spongepowered.api.entity.living.player.Player)1 Cause (org.spongepowered.api.event.cause.Cause)1 EventContext (org.spongepowered.api.event.cause.EventContext)1 Scheduler (org.spongepowered.api.scheduler.Scheduler)1 Task (org.spongepowered.api.scheduler.Task)1 TransactionResult (org.spongepowered.api.service.economy.transaction.TransactionResult)1