Search in sources :

Example 6 with DamageSource

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

the class PlayerCombatParser method parse.

default default void parse(DamageEntityEvent event) {
    Entity entity = event.getTargetEntity();
    if (!(entity instanceof Living)) {
        return;
    }
    Optional<DamageSource> optDamageSource = event.getCause().first(DamageSource.class);
    if (optDamageSource.isPresent()) {
        Entity srcEntity = null;
        Entity indirectSrcEntity = null;
        if (optDamageSource.get() instanceof IndirectEntityDamageSource) {
            srcEntity = ((IndirectEntityDamageSource) optDamageSource.get()).getIndirectSource();
            indirectSrcEntity = ((IndirectEntityDamageSource) optDamageSource.get()).getSource();
        } else if (optDamageSource.get() instanceof EntityDamageSource) {
            srcEntity = ((EntityDamageSource) optDamageSource.get()).getSource();
        }
        if (!(srcEntity instanceof Living)) {
            if (entity instanceof Player) {
                processNonLivingAttack(optDamageSource.get(), (Player) entity);
            }
            return;
        }
        Living living = (Living) srcEntity;
        if (verify(living)) {
            if (entity instanceof Player && living instanceof Player) {
                processPvP((Player) living, (Player) entity);
                processPvP((Player) living, (Player) entity, indirectSrcEntity);
            } else if (entity instanceof Player) {
                processMonsterAttack(living, (Player) entity);
            } else if (living instanceof Player) {
                processPlayerAttack((Player) living, (Living) entity);
            }
        }
    }
}
Also used : Entity(org.spongepowered.api.entity.Entity) Player(org.spongepowered.api.entity.living.player.Player) EntityDamageSource(org.spongepowered.api.event.cause.entity.damage.source.EntityDamageSource) IndirectEntityDamageSource(org.spongepowered.api.event.cause.entity.damage.source.IndirectEntityDamageSource) DamageSource(org.spongepowered.api.event.cause.entity.damage.source.DamageSource) Living(org.spongepowered.api.entity.living.Living) IndirectEntityDamageSource(org.spongepowered.api.event.cause.entity.damage.source.IndirectEntityDamageSource) EntityDamageSource(org.spongepowered.api.event.cause.entity.damage.source.EntityDamageSource) IndirectEntityDamageSource(org.spongepowered.api.event.cause.entity.damage.source.IndirectEntityDamageSource)

Example 7 with DamageSource

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

the class MainWorldWrapper method createFor.

private PlayerCombatParser createFor(Cancellable event) {
    return new PlayerCombatParser() {

        @Override
        public void processPvP(Player attacker, Player defender, @Nullable Entity indirectSource) {
            Optional<PvPService> optService = Sponge.getServiceManager().provide(PvPService.class);
            if (optService.isPresent()) {
                PvPService service = optService.get();
                if (service.getPvPState(attacker).allowByDefault() && service.getPvPState(defender).allowByDefault()) {
                    return;
                }
            }
            if (!(indirectSource instanceof Snowball) || !lobby.contains(attacker)) {
                attacker.sendMessage(Text.of(TextColors.RED, "PvP is opt-in only in the main world!"));
                event.setCancelled(true);
            }
        }

        @Override
        public void processNonLivingAttack(DamageSource attacker, Player defender) {
            if (attacker.getType() == DamageTypes.VOID) {
                defender.setLocation(defender.getWorld().getSpawnLocation());
                defender.offer(Keys.FALL_DISTANCE, 0F);
                event.setCancelled(true);
            }
        }
    };
}
Also used : Entity(org.spongepowered.api.entity.Entity) Snowball(org.spongepowered.api.entity.projectile.Snowball) Player(org.spongepowered.api.entity.living.player.Player) PvPService(com.skelril.skree.service.PvPService) DamageSource(org.spongepowered.api.event.cause.entity.damage.source.DamageSource) PlayerCombatParser(com.skelril.nitro.combat.PlayerCombatParser) Nullable(javax.annotation.Nullable)

Example 8 with DamageSource

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

the class JungleRaidEffectListener method createFor.

private PlayerCombatParser createFor(Cancellable event, JungleRaidInstance inst) {
    return new PlayerCombatParser() {

        @Override
        public void processPvP(Player attacker, Player defender, @Nullable Entity indirectSource) {
            final boolean isDamageEntityEvent = event instanceof DamageEntityEvent;
            // Do Death Touch before anything else
            if (inst.isFlagEnabled(JungleRaidFlag.DEATH_TOUCH) && isDamageEntityEvent) {
                ((DamageEntityEvent) event).setBaseDamage(Math.pow(defender.get(Keys.MAX_HEALTH).orElse(20D), 3));
                return;
            }
            Optional<JungleRaidClass> optClass = inst.getClass(attacker);
            if (optClass.isPresent()) {
                JungleRaidClass jrClass = optClass.get();
                if (jrClass == JungleRaidClass.SNIPER) {
                    Optional<ItemStack> optHeld = attacker.getItemInHand(HandTypes.MAIN_HAND);
                    boolean hasWoodenSword = optHeld.isPresent() && optHeld.get().getItem() == ItemTypes.WOODEN_SWORD;
                    if (indirectSource != null || !hasWoodenSword) {
                        double distSq = attacker.getLocation().getPosition().distanceSquared(defender.getLocation().getPosition());
                        double targetDistSq = Math.pow(70, 2);
                        double ratio = Math.min(distSq, targetDistSq) / targetDistSq;
                        if (isDamageEntityEvent) {
                            // Handle damage modification
                            ((DamageEntityEvent) event).setBaseDamage(((DamageEntityEvent) event).getBaseDamage() * ratio);
                        } else {
                            // Disable the arrow fire in the Impact event
                            if (ratio < .7 && indirectSource != null) {
                                indirectSource.offer(Keys.FIRE_TICKS, 0);
                            }
                        }
                    }
                }
            }
            if (inst.isFlagEnabled(JungleRaidFlag.TITAN_MODE) && attacker.getUniqueId().equals(inst.getFlagData().titan) && isDamageEntityEvent) {
                ((DamageEntityEvent) event).setBaseDamage(((DamageEntityEvent) event).getBaseDamage() * 2);
            }
        }

        @Override
        public void processNonLivingAttack(DamageSource attacker, Player defender) {
            if (!(event instanceof DamageEntityEvent)) {
                return;
            }
            if (attacker.getType() == DamageTypes.FALL) {
                BlockType belowType = defender.getLocation().add(0, -1, 0).getBlockType();
                if (inst.isFlagEnabled(JungleRaidFlag.TRAMPOLINE)) {
                    Vector3d oldVel = defender.getVelocity();
                    Vector3d newVel = new Vector3d(oldVel.getX(), 0, oldVel.getZ());
                    defender.setVelocity(new Vector3d(0, .1, 0).mul(((DamageEntityEvent) event).getBaseDamage()).add(newVel));
                    event.setCancelled(true);
                } else if (belowType == BlockTypes.LEAVES || belowType == BlockTypes.LEAVES2) {
                    if (Probability.getChance(2)) {
                        Vector3d oldVel = defender.getVelocity();
                        Vector3d newVel = new Vector3d(oldVel.getX() > 0 ? -.5 : .5, 0, oldVel.getZ() > 0 ? -.5 : .5);
                        defender.setVelocity(new Vector3d(0, .1, 0).mul(((DamageEntityEvent) event).getBaseDamage()).add(newVel));
                    }
                    event.setCancelled(true);
                }
            } else if (attacker.getType() == DamageTypes.CUSTOM) {
                if (inst.isFlagEnabled(JungleRaidFlag.EXPLOSIVE_ARROWS) || inst.isFlagEnabled(JungleRaidFlag.GRENADES)) {
                    ((DamageEntityEvent) event).setBaseDamage(Math.min(((DamageEntityEvent) event).getBaseDamage(), 2));
                }
            }
        }
    };
}
Also used : DamageEntityEvent(org.spongepowered.api.event.entity.DamageEntityEvent) Entity(org.spongepowered.api.entity.Entity) Player(org.spongepowered.api.entity.living.player.Player) DamageSource(org.spongepowered.api.event.cause.entity.damage.source.DamageSource) IndirectEntityDamageSource(org.spongepowered.api.event.cause.entity.damage.source.IndirectEntityDamageSource) BlockType(org.spongepowered.api.block.BlockType) Vector3d(com.flowpowered.math.vector.Vector3d) PlayerCombatParser(com.skelril.nitro.combat.PlayerCombatParser) ItemStack(org.spongepowered.api.item.inventory.ItemStack) ItemStackFactory.newItemStack(com.skelril.nitro.item.ItemStackFactory.newItemStack) Nullable(javax.annotation.Nullable)

Example 9 with DamageSource

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

the class PatientXListener method onEntityDamageEvent.

@Listener(order = Order.LAST)
public void onEntityDamageEvent(DamageEntityEvent event) {
    Entity defender = event.getTargetEntity();
    Optional<PatientXInstance> optInst = manager.getApplicableZone(defender);
    if (!optInst.isPresent())
        return;
    PatientXInstance inst = optInst.get();
    DamageSource dmgSource = event.getCause().first(DamageSource.class).get();
    if (defender instanceof Player && manager.getBlockedDamage().contains(dmgSource.getType())) {
        // Explosive damage formula: (1 × 1 + 1) × 8 × power + 1
        // Use 49, snowball power is 3
        double ratio = event.getBaseDamage() / 49;
        // Nullify all modifiers
        for (Tuple<DamageModifier, Function<? super Double, Double>> modifier : event.getModifiers()) {
            event.setDamage(modifier.getFirst(), (a) -> 0D);
        }
        event.setBaseDamage(ratio * inst.getDifficulty());
    }
}
Also used : Entity(org.spongepowered.api.entity.Entity) Function(java.util.function.Function) Player(org.spongepowered.api.entity.living.player.Player) DamageSource(org.spongepowered.api.event.cause.entity.damage.source.DamageSource) DamageModifier(org.spongepowered.api.event.cause.entity.damage.DamageModifier) Listener(org.spongepowered.api.event.Listener)

Aggregations

DamageSource (org.spongepowered.api.event.cause.entity.damage.source.DamageSource)9 Entity (org.spongepowered.api.entity.Entity)8 Player (org.spongepowered.api.entity.living.player.Player)7 IndirectEntityDamageSource (org.spongepowered.api.event.cause.entity.damage.source.IndirectEntityDamageSource)5 Vector3d (com.flowpowered.math.vector.Vector3d)4 EntityDamageSource (org.spongepowered.api.event.cause.entity.damage.source.EntityDamageSource)4 DamageEntityEvent (org.spongepowered.api.event.entity.DamageEntityEvent)4 Instruction (com.skelril.openboss.Instruction)3 Living (org.spongepowered.api.entity.living.Living)3 ItemStack (org.spongepowered.api.item.inventory.ItemStack)3 World (org.spongepowered.api.world.World)3 PlayerCombatParser (com.skelril.nitro.combat.PlayerCombatParser)2 ItemStackFactory.newItemStack (com.skelril.nitro.item.ItemStackFactory.newItemStack)2 Nullable (javax.annotation.Nullable)2 Giant (org.spongepowered.api.entity.living.monster.Giant)2 Zombie (org.spongepowered.api.entity.living.monster.Zombie)2 Vector3i (com.flowpowered.math.vector.Vector3i)1 Lists (com.google.common.collect.Lists)1 Clause (com.skelril.nitro.Clause)1 EntityHealthPrinter (com.skelril.nitro.entity.EntityHealthPrinter)1