Search in sources :

Example 6 with EntityDamageSource

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

Example 7 with EntityDamageSource

use of org.spongepowered.api.event.cause.entity.damage.source.EntityDamageSource in project Skree by Skelril.

the class MeleeAttackClusterListener method getSource.

public Optional<Living> getSource(Cause cause) {
    Optional<EntityDamageSource> optEntityDamageSource = cause.first(EntityDamageSource.class);
    if (!optEntityDamageSource.isPresent()) {
        return Optional.empty();
    }
    EntityDamageSource damageSource = optEntityDamageSource.get();
    Entity source = damageSource.getSource();
    if (!(source instanceof Living)) {
        return Optional.empty();
    }
    return Optional.of((Living) source);
}
Also used : Entity(org.spongepowered.api.entity.Entity) Living(org.spongepowered.api.entity.living.Living) EntityDamageSource(org.spongepowered.api.event.cause.entity.damage.source.EntityDamageSource)

Aggregations

EntityDamageSource (org.spongepowered.api.event.cause.entity.damage.source.EntityDamageSource)7 Entity (org.spongepowered.api.entity.Entity)6 Player (org.spongepowered.api.entity.living.player.Player)6 DamageSource (org.spongepowered.api.event.cause.entity.damage.source.DamageSource)4 IndirectEntityDamageSource (org.spongepowered.api.event.cause.entity.damage.source.IndirectEntityDamageSource)4 Vector3d (com.flowpowered.math.vector.Vector3d)3 Living (org.spongepowered.api.entity.living.Living)3 ItemStack (org.spongepowered.api.item.inventory.ItemStack)3 World (org.spongepowered.api.world.World)3 ItemDropper (com.skelril.nitro.item.ItemDropper)2 ItemStackFactory.newItemStack (com.skelril.nitro.item.ItemStackFactory.newItemStack)2 Instruction (com.skelril.openboss.Instruction)2 Giant (org.spongepowered.api.entity.living.monster.Giant)2 Zombie (org.spongepowered.api.entity.living.monster.Zombie)2 Listener (org.spongepowered.api.event.Listener)2 DamageEntityEvent (org.spongepowered.api.event.entity.DamageEntityEvent)2 TEAccount (com.erigitic.config.TEAccount)1 Vector3i (com.flowpowered.math.vector.Vector3i)1 Lists (com.google.common.collect.Lists)1 Clause (com.skelril.nitro.Clause)1