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