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