Search in sources :

Example 21 with DamageFunction

use of org.spongepowered.api.event.cause.entity.damage.DamageFunction in project SpongeAPI by SpongePowered.

the class SpongeAbstractDamageEntityEventTest method testModifiersApplicable.

@Test
public void testModifiersApplicable() {
    Entity targetEntity = mockParam(Entity.class);
    DamageModifier firstModifer = mockParam(DamageModifier.class);
    DamageModifier secondModifier = mockParam(DamageModifier.class);
    List<DamageFunction> originalFunctions = Lists.newArrayList(DamageFunction.of(firstModifer, p -> p), DamageFunction.of(secondModifier, p -> p));
    DamageEntityEvent event = SpongeEventFactory.createDamageEntityEvent(Cause.of(EventContext.empty(), "none"), originalFunctions, targetEntity, 0);
    assertThat(event.isModifierApplicable(firstModifer), is(true));
    assertThat(event.isModifierApplicable(secondModifier), is(true));
    assertThat(event.isModifierApplicable(mockParam(DamageModifier.class)), is(false));
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) Matchers(org.hamcrest.Matchers) Test(org.junit.Test) Entity(org.spongepowered.api.entity.Entity) DoubleUnaryOperator(java.util.function.DoubleUnaryOperator) Assert.assertThat(org.junit.Assert.assertThat) Cause(org.spongepowered.api.event.cause.Cause) List(java.util.List) Lists(com.google.common.collect.Lists) DamageFunction(org.spongepowered.api.event.cause.entity.damage.DamageFunction) DamageModifier(org.spongepowered.api.event.cause.entity.damage.DamageModifier) Matchers.closeTo(org.hamcrest.Matchers.closeTo) DamageEntityEvent(org.spongepowered.api.event.entity.DamageEntityEvent) Map(java.util.Map) EventContext(org.spongepowered.api.event.cause.EventContext) DamageEntityEvent(org.spongepowered.api.event.entity.DamageEntityEvent) Entity(org.spongepowered.api.entity.Entity) DamageModifier(org.spongepowered.api.event.cause.entity.damage.DamageModifier) DamageFunction(org.spongepowered.api.event.cause.entity.damage.DamageFunction) Test(org.junit.Test)

Example 22 with DamageFunction

use of org.spongepowered.api.event.cause.entity.damage.DamageFunction 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 (DamageFunction modifier : event.getModifiers()) {
            event.setDamage(modifier.getModifier(), (a) -> 0D);
        }
        event.setBaseDamage(ratio * inst.getDifficulty());
    }
}
Also used : Entity(org.spongepowered.api.entity.Entity) Player(org.spongepowered.api.entity.living.player.Player) DamageSource(org.spongepowered.api.event.cause.entity.damage.source.DamageSource) DamageFunction(org.spongepowered.api.event.cause.entity.damage.DamageFunction) Listener(org.spongepowered.api.event.Listener)

Example 23 with DamageFunction

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

the class WildernessWorldWrapper method createFor.

private PlayerCombatParser createFor(Cancellable event, int level) {
    return new PlayerCombatParser() {

        @Override
        public void processPvP(Player attacker, Player defender) {
            if (allowsPvP(level)) {
                return;
            }
            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;
                }
            }
            attacker.sendMessage(Text.of(TextColors.RED, "PvP is opt-in only in this part of the Wilderness!"));
            attacker.sendMessage(Text.of(TextColors.RED, "Mandatory PvP is from level ", getFirstPvPLevel(), " and on."));
            event.setCancelled(true);
        }

        @Override
        public void processMonsterAttack(Living attacker, Player defender) {
            if (!(event instanceof DamageEntityEvent)) {
                return;
            }
            DamageEntityEvent dEvent = (DamageEntityEvent) event;
            // If they're endermites they hit through armor, otherwise they get a damage boost
            if (attacker.getType() == EntityTypes.ENDERMITE) {
                for (DamageFunction modifier : dEvent.getModifiers()) {
                    dEvent.setDamage(modifier.getModifier(), (a) -> 0D);
                }
                dEvent.setBaseDamage(Probability.getCompoundRandom(getDamageMod(level), 3));
                if (Probability.getChance(5)) {
                    List<PotionEffect> potionEffects = defender.getOrElse(Keys.POTION_EFFECTS, new ArrayList<>());
                    potionEffects.add(PotionEffect.of(PotionEffectTypes.POISON, 2, 30 * 20));
                    defender.offer(Keys.POTION_EFFECTS, potionEffects);
                }
            } else {
                dEvent.setBaseDamage(dEvent.getBaseDamage() + getDamageMod(level));
            }
            // Only apply scoring while in survival mode
            if (defender.get(Keys.GAME_MODE).orElse(GameModes.SURVIVAL) != GameModes.SURVIVAL) {
                return;
            }
            WildernessPlayerMeta meta = playerMetaMap.get(defender.getUniqueId());
            if (meta != null) {
                meta.hit();
            }
        }

        @Override
        public void processPlayerAttack(Player attacker, Living defender) {
            Task.builder().delayTicks(1).execute(() -> healthPrinter.print(MessageChannel.fixed(attacker), defender)).submit(SkreePlugin.inst());
            if (!(defender instanceof Monster) || defender instanceof Creeper) {
                return;
            }
            // Only apply scoring while in survival mode
            if (attacker.get(Keys.GAME_MODE).orElse(GameModes.SURVIVAL) != GameModes.SURVIVAL) {
                return;
            }
            WildernessPlayerMeta meta = playerMetaMap.get(attacker.getUniqueId());
            if (meta != null) {
                meta.attack();
                if (meta.getRatio() > 30 && meta.getFactors() > 35) {
                    Deque<Entity> spawned = new ArrayDeque<>();
                    for (int i = Probability.getRandom(5); i > 0; --i) {
                        Entity entity = attacker.getWorld().createEntity(EntityTypes.ENDERMITE, defender.getLocation().getPosition());
                        entity.getWorld().spawnEntity(entity, Cause.source(SpawnCause.builder().type(SpawnTypes.PLUGIN).build()).build());
                        spawned.add(entity);
                    }
                    IntegratedRunnable runnable = new IntegratedRunnable() {

                        @Override
                        public boolean run(int times) {
                            Entity mob = spawned.poll();
                            if (mob.isLoaded() && mob.getWorld().equals(attacker.getWorld())) {
                                mob.setLocation(attacker.getLocation());
                            }
                            return true;
                        }

                        @Override
                        public void end() {
                        }
                    };
                    TimedRunnable timedRunnable = new TimedRunnable<>(runnable, spawned.size());
                    timedRunnable.setTask(Task.builder().execute(timedRunnable).delayTicks(40).intervalTicks(20).submit(SkreePlugin.inst()));
                }
                if (System.currentTimeMillis() - meta.getLastReset() >= TimeUnit.MINUTES.toMillis(5)) {
                    meta.reset();
                }
            }
        }
    };
}
Also used : DamageEntityEvent(org.spongepowered.api.event.entity.DamageEntityEvent) Player(org.spongepowered.api.entity.living.player.Player) PotionEffect(org.spongepowered.api.effect.potion.PotionEffect) Living(org.spongepowered.api.entity.living.Living) Creeper(org.spongepowered.api.entity.living.monster.Creeper) TimedRunnable(com.skelril.nitro.time.TimedRunnable) PvPService(com.skelril.skree.service.PvPService) IntegratedRunnable(com.skelril.nitro.time.IntegratedRunnable) DamageFunction(org.spongepowered.api.event.cause.entity.damage.DamageFunction) Monster(org.spongepowered.api.entity.living.monster.Monster) PlayerCombatParser(com.skelril.nitro.combat.PlayerCombatParser)

Example 24 with DamageFunction

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

the class TheForgeListener method createCombatParser.

private PlayerCombatParser createCombatParser(Event event, TheForgeInstance inst) {
    return new PlayerCombatParser() {

        @Override
        public void processPlayerAttack(Player attacker, Living defender) {
            if (!(event instanceof DamageEntityEvent)) {
                return;
            }
            if (attacker.getLocation().getY() != attacker.getLocation().getBlockY()) {
                DamageEntityEvent dEvent = (DamageEntityEvent) event;
                dEvent.setBaseDamage(Probability.getCompoundRandom(dEvent.getBaseDamage(), 4));
            }
        }

        @Override
        public void processMonsterAttack(Living attacker, Player defender) {
            if (!(event instanceof DamageEntityEvent)) {
                return;
            }
            if (inst.isInvunerable(defender)) {
                ((DamageEntityEvent) event).setCancelled(true);
                return;
            }
            if (Probability.getChance(5)) {
                DamageEntityEvent dEvent = (DamageEntityEvent) event;
                for (DamageFunction modifier : dEvent.getModifiers()) {
                    dEvent.setDamage(modifier.getModifier(), (a) -> 0D);
                }
            }
        }
    };
}
Also used : DamageEntityEvent(org.spongepowered.api.event.entity.DamageEntityEvent) Player(org.spongepowered.api.entity.living.player.Player) Living(org.spongepowered.api.entity.living.Living) DamageFunction(org.spongepowered.api.event.cause.entity.damage.DamageFunction) PlayerCombatParser(com.skelril.nitro.combat.PlayerCombatParser)

Example 25 with DamageFunction

use of org.spongepowered.api.event.cause.entity.damage.DamageFunction in project LanternServer by LanternPowered.

the class LanternEntity method collectDamageFunctions.

protected void collectDamageFunctions(List<Tuple<DamageFunction, Consumer<DamageEntityEvent>>> damageFunctions) {
    // Absorption health modifier
    get(Keys.ABSORPTION).filter(value -> value > 0).ifPresent(value -> {
        final DoubleUnaryOperator function = d -> -(Math.max(d - Math.max(d - value, 0), 0));
        final DamageModifier modifier = DamageModifier.builder().cause(Cause.of(EventContext.empty(), this)).type(DamageModifierTypes.ABSORPTION).build();
        damageFunctions.add(new Tuple<>(new DamageFunction(modifier, function), event -> {
            final double mod = event.getDamage(modifier);
            offer(Keys.ABSORPTION, Math.max(get(Keys.ABSORPTION).get() + mod, 0));
        }));
    });
}
Also used : EventContextKeys(org.spongepowered.api.event.cause.EventContextKeys) Random(java.util.Random) DamageSource(org.spongepowered.api.event.cause.entity.damage.source.DamageSource) LanternTexts(org.lanternpowered.server.text.LanternTexts) DataQueries(org.lanternpowered.server.data.DataQueries) ValueCollection(org.lanternpowered.server.data.ValueCollection) EntitySnapshot(org.spongepowered.api.entity.EntitySnapshot) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) DataHelper(org.lanternpowered.server.data.DataHelper) DamageEntityEvent(org.spongepowered.api.event.entity.DamageEntityEvent) EntityEvent(org.lanternpowered.server.entity.event.EntityEvent) AdditionalContainerCollection(org.lanternpowered.server.data.AdditionalContainerCollection) Living(org.spongepowered.api.entity.living.Living) EnumSet(java.util.EnumSet) HealingSource(org.spongepowered.api.event.cause.entity.health.source.HealingSource) Location(org.spongepowered.api.world.Location) AbstractPropertyHolder(org.lanternpowered.server.data.property.AbstractPropertyHolder) MutableBoundedValue(org.spongepowered.api.data.value.mutable.MutableBoundedValue) Sponge(org.spongepowered.api.Sponge) DataContainer(org.spongepowered.api.data.DataContainer) DamageTypes(org.spongepowered.api.event.cause.entity.damage.DamageTypes) Tuple(org.spongepowered.api.util.Tuple) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) LanternGame(org.lanternpowered.server.game.LanternGame) Preconditions.checkState(com.google.common.base.Preconditions.checkState) EntityTypeRegistryModule(org.lanternpowered.server.game.registry.type.entity.EntityTypeRegistryModule) Direction(org.spongepowered.api.util.Direction) Cause(org.spongepowered.api.event.cause.Cause) List(java.util.List) Transform(org.spongepowered.api.entity.Transform) World(org.spongepowered.api.world.World) IAdditionalDataHolder(org.lanternpowered.server.data.IAdditionalDataHolder) DataManipulator(org.spongepowered.api.data.manipulator.DataManipulator) RelativePositions(org.spongepowered.api.util.RelativePositions) Optional(java.util.Optional) EntityProtocolType(org.lanternpowered.server.network.entity.EntityProtocolType) EventContext(org.spongepowered.api.event.cause.EventContext) LanternKeys(org.lanternpowered.server.data.key.LanternKeys) Keys(org.spongepowered.api.data.key.Keys) DestructEntityEvent(org.spongepowered.api.event.entity.DestructEntityEvent) Vector3d(com.flowpowered.math.vector.Vector3d) DamageModifierTypes(org.spongepowered.api.event.cause.entity.damage.DamageModifierTypes) AABB(org.spongepowered.api.util.AABB) DamagedEntityEvent(org.lanternpowered.server.entity.event.DamagedEntityEvent) GameModes(org.spongepowered.api.entity.living.player.gamemode.GameModes) HealEntityEvent(org.spongepowered.api.event.entity.HealEntityEvent) DoubleUnaryOperator(java.util.function.DoubleUnaryOperator) ArrayList(java.util.ArrayList) CauseStack(org.lanternpowered.server.event.CauseStack) ImmutableList(com.google.common.collect.ImmutableList) Text(org.spongepowered.api.text.Text) Quaternions(org.lanternpowered.server.util.Quaternions) LanternWorld(org.lanternpowered.server.world.LanternWorld) FixedTranslation(org.spongepowered.api.text.translation.FixedTranslation) Nullable(javax.annotation.Nullable) Translation(org.spongepowered.api.text.translation.Translation) LanternEventContextKeys(org.lanternpowered.server.event.LanternEventContextKeys) DamageSources(org.spongepowered.api.event.cause.entity.damage.source.DamageSources) SpongeEventFactory(org.spongepowered.api.event.SpongeEventFactory) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) InvalidDataException(org.spongepowered.api.data.persistence.InvalidDataException) DataHolder(org.spongepowered.api.data.DataHolder) Entity(org.spongepowered.api.entity.Entity) Consumer(java.util.function.Consumer) DamageFunction(org.spongepowered.api.event.cause.entity.damage.DamageFunction) DamageModifier(org.spongepowered.api.event.cause.entity.damage.DamageModifier) DataView(org.spongepowered.api.data.DataView) Vector3i(com.flowpowered.math.vector.Vector3i) LanternPlayer(org.lanternpowered.server.entity.living.player.LanternPlayer) EntityType(org.spongepowered.api.entity.EntityType) MessageChannel(org.spongepowered.api.text.channel.MessageChannel) MessageEvent(org.spongepowered.api.event.message.MessageEvent) EntityArchetype(org.spongepowered.api.entity.EntityArchetype) DamageModifier(org.spongepowered.api.event.cause.entity.damage.DamageModifier) DamageFunction(org.spongepowered.api.event.cause.entity.damage.DamageFunction) DoubleUnaryOperator(java.util.function.DoubleUnaryOperator)

Aggregations

DamageFunction (org.spongepowered.api.event.cause.entity.damage.DamageFunction)25 DamageModifier (org.spongepowered.api.event.cause.entity.damage.DamageModifier)20 List (java.util.List)14 Cause (org.spongepowered.api.event.cause.Cause)13 EventContext (org.spongepowered.api.event.cause.EventContext)13 DoubleUnaryOperator (java.util.function.DoubleUnaryOperator)12 Lists (com.google.common.collect.Lists)11 ArrayList (java.util.ArrayList)11 Map (java.util.Map)11 EntityLivingBase (net.minecraft.entity.EntityLivingBase)10 EntityPlayer (net.minecraft.entity.player.EntityPlayer)10 DamageSource (net.minecraft.util.DamageSource)10 DamageEntityEvent (org.spongepowered.api.event.entity.DamageEntityEvent)10 Optional (java.util.Optional)9 Entity (net.minecraft.entity.Entity)9 Sponge (org.spongepowered.api.Sponge)9 DamageModifierTypes (org.spongepowered.api.event.cause.entity.damage.DamageModifierTypes)9 IBlockState (net.minecraft.block.state.IBlockState)8 EnchantmentHelper (net.minecraft.enchantment.EnchantmentHelper)8 MobEffects (net.minecraft.init.MobEffects)8