Search in sources :

Example 16 with EntityDamageSource

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);
                }
            }
        }
    }
}
Also used : TileEntity(org.spongepowered.api.block.tileentity.TileEntity) Entity(org.spongepowered.api.entity.Entity) Player(org.spongepowered.api.entity.living.player.Player) TEAccount(com.erigitic.config.TEAccount) BigDecimal(java.math.BigDecimal) EntityDamageSource(org.spongepowered.api.event.cause.entity.damage.source.EntityDamageSource) Currency(org.spongepowered.api.service.economy.Currency) Listener(org.spongepowered.api.event.Listener)

Aggregations

EntityDamageSource (org.spongepowered.api.event.cause.entity.damage.source.EntityDamageSource)16 Entity (org.spongepowered.api.entity.Entity)11 Player (org.spongepowered.api.entity.living.player.Player)8 IndirectEntityDamageSource (org.spongepowered.api.event.cause.entity.damage.source.IndirectEntityDamageSource)7 Living (org.spongepowered.api.entity.living.Living)6 DamageSource (org.spongepowered.api.event.cause.entity.damage.source.DamageSource)5 Vector3d (com.flowpowered.math.vector.Vector3d)4 Listener (org.spongepowered.api.event.Listener)4 DamageFunction (org.spongepowered.api.event.cause.entity.damage.DamageFunction)3 DamageEntityEvent (org.spongepowered.api.event.entity.DamageEntityEvent)3 ItemStack (org.spongepowered.api.item.inventory.ItemStack)3 World (org.spongepowered.api.world.World)3 TEAccount (com.erigitic.config.TEAccount)2 ItemDropper (com.skelril.nitro.item.ItemDropper)2 ItemStackFactory.newItemStack (com.skelril.nitro.item.ItemStackFactory.newItemStack)2 Instruction (com.skelril.openboss.Instruction)2 Nullable (javax.annotation.Nullable)2 EntityLivingBase (net.minecraft.entity.EntityLivingBase)2 Giant (org.spongepowered.api.entity.living.monster.Giant)2 Zombie (org.spongepowered.api.entity.living.monster.Zombie)2