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);
}
Aggregations