use of org.spongepowered.api.event.cause.entity.damage.source.EntityDamageSource 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);
}
}
}
}
}
Aggregations