Search in sources :

Example 6 with DamageFunction

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

the class PatientXManager method setupBossManager.

private void setupBossManager() {
    Sponge.getEventManager().registerListeners(SkreePlugin.inst(), new BossListener<>(bossManager, Zombie.class));
    List<Instruction<BindCondition, Boss<Zombie, ZoneBossDetail<PatientXInstance>>>> bindProcessor = bossManager.getBindProcessor();
    bindProcessor.add((condition, boss) -> {
        Optional<Zombie> optBossEnt = boss.getTargetEntity();
        if (optBossEnt.isPresent()) {
            Zombie bossEnt = optBossEnt.get();
            bossEnt.offer(Keys.DISPLAY_NAME, Text.of("Patient X"));
            setMaxHealth(bossEnt, config.bossHealth, true);
        }
        return Optional.empty();
    });
    bindProcessor.add((condition, boss) -> {
        Optional<Zombie> optBoss = boss.getTargetEntity();
        if (optBoss.isPresent()) {
            optBoss.get().offer(Keys.PERSISTS, true);
        }
        return Optional.empty();
    });
    bindProcessor.add((condition, boss) -> {
        boss.getDetail().getZone().getPlayerMessageChannel(PlayerClassifier.SPECTATOR).send(Text.of(TextColors.GOLD, "Ice to meet you again!"));
        return Optional.empty();
    });
    List<Instruction<UnbindCondition, Boss<Zombie, ZoneBossDetail<PatientXInstance>>>> unbindProcessor = bossManager.getUnbindProcessor();
    unbindProcessor.add((condition, boss) -> {
        PatientXInstance inst = boss.getDetail().getZone();
        Location<World> target = inst.getCenter();
        for (Player player : inst.getPlayers(PlayerClassifier.PARTICIPANT)) {
            player.setLocation(target);
            boolean useX = Probability.getChance(2);
            int accel = Probability.getChance(2) ? 1 : -1;
            Vector3d v = new Vector3d(useX ? accel : 0, 0, !useX ? accel : 0);
            player.setVelocity(v);
        }
        inst.freezeBlocks(100, false);
        // Reset respawn mechanics
        inst.bossDied();
        return Optional.empty();
    });
    List<Instruction<DamageCondition, Boss<Zombie, ZoneBossDetail<PatientXInstance>>>> damageProcessor = bossManager.getDamageProcessor();
    damageProcessor.add((condition, boss) -> {
        PatientXInstance inst = boss.getDetail().getZone();
        DamageEntityEvent event = condition.getEvent();
        // Nullify all modifiers
        for (DamageFunction modifier : event.getModifiers()) {
            event.setDamage(modifier.getModifier(), (a) -> a);
        }
        event.setBaseDamage(inst.getDifficulty() * config.baseBossHit);
        return Optional.empty();
    });
    List<Instruction<DamagedCondition, Boss<Zombie, ZoneBossDetail<PatientXInstance>>>> damagedProcessor = bossManager.getDamagedProcessor();
    damagedProcessor.add((condition, boss) -> {
        DamageEntityEvent event = condition.getEvent();
        Optional<DamageSource> optDamageSource = condition.getDamageSource();
        if (optDamageSource.isPresent() && blockedDamage.contains(optDamageSource.get().getType())) {
            event.setCancelled(true);
            return Optional.empty();
        }
        return Optional.of((damagedCondition, zombieZoneBossDetailBoss) -> {
            PatientXInstance inst = boss.getDetail().getZone();
            if (optDamageSource.isPresent() && optDamageSource.get() instanceof EntityDamageSource) {
                if (optDamageSource.get() instanceof IndirectEntityDamageSource) {
                    Task.builder().execute(() -> {
                        VelocityEntitySpawner.sendRadial(EntityTypes.SNOWBALL, inst.getBoss().get(), Cause.source(SpawnCause.builder().type(SpawnTypes.PROJECTILE).build()).build());
                    }).delayTicks(1).submit(SkreePlugin.inst());
                } else {
                    Entity srcEntity = ((EntityDamageSource) optDamageSource.get()).getSource();
                    if (srcEntity instanceof Player) {
                        Optional<ItemStack> optHeld = ((Player) srcEntity).getItemInHand(HandTypes.MAIN_HAND);
                        if (optHeld.isPresent() && optHeld.get().getItem() == ItemTypes.BLAZE_ROD) {
                            inst.modifyDifficulty(2);
                        }
                    }
                }
            }
            inst.modifyDifficulty(.5);
            inst.teleportRandom(true);
            return Optional.empty();
        });
    });
}
Also used : Entity(org.spongepowered.api.entity.Entity) Instruction(com.skelril.openboss.Instruction) World(org.spongepowered.api.world.World) EntityDamageSource(org.spongepowered.api.event.cause.entity.damage.source.EntityDamageSource) IndirectEntityDamageSource(org.spongepowered.api.event.cause.entity.damage.source.IndirectEntityDamageSource) DamageFunction(org.spongepowered.api.event.cause.entity.damage.DamageFunction) IndirectEntityDamageSource(org.spongepowered.api.event.cause.entity.damage.source.IndirectEntityDamageSource) DamageEntityEvent(org.spongepowered.api.event.entity.DamageEntityEvent) Player(org.spongepowered.api.entity.living.player.Player) EntityDamageSource(org.spongepowered.api.event.cause.entity.damage.source.EntityDamageSource) DamageSource(org.spongepowered.api.event.cause.entity.damage.source.DamageSource) IndirectEntityDamageSource(org.spongepowered.api.event.cause.entity.damage.source.IndirectEntityDamageSource) Zombie(org.spongepowered.api.entity.living.monster.Zombie) Vector3d(com.flowpowered.math.vector.Vector3d) ItemStack(org.spongepowered.api.item.inventory.ItemStack)

Example 7 with DamageFunction

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

the class SpongeAbstractDamageEntityEventTest method testAddModifier.

@Test
public void testAddModifier() {
    Entity targetEntity = mockParam(Entity.class);
    final int originalDamage = 1;
    final int originalFinalDamage = 18;
    final int firstModifierDamage = 2;
    final int secondModifierDamage = 15;
    final int modifiedFinalDamage = 36;
    final int thirdDamage = 18;
    DamageModifier firstModifer = mockParam(DamageModifier.class);
    DamageModifier secondModifier = mockParam(DamageModifier.class);
    DamageModifier thirdModifier = mockParam(DamageModifier.class);
    DoubleUnaryOperator thirdFunction = p -> p;
    List<DamageFunction> originalFunctions = Lists.newArrayList(DamageFunction.of(firstModifer, p -> p * 2), DamageFunction.of(secondModifier, p -> p * 5));
    List<DamageFunction> newFunctions = Lists.newArrayList(originalFunctions);
    newFunctions.add(DamageFunction.of(thirdModifier, thirdFunction));
    DamageEntityEvent event = SpongeEventFactory.createDamageEntityEvent(Cause.of(EventContext.empty(), "none"), originalFunctions, targetEntity, originalDamage);
    assertThat(event.getOriginalFunctions(), is(Matchers.equalTo(originalFunctions)));
    assertThat(event.isModifierApplicable(thirdModifier), is(false));
    event.setDamage(thirdModifier, thirdFunction);
    assertThat(event.getDamage(firstModifer), is(closeTo(firstModifierDamage, ERROR)));
    assertThat(event.getDamage(secondModifier), is(closeTo(secondModifierDamage, ERROR)));
    assertThat(event.getDamage(thirdModifier), is(closeTo(thirdDamage, ERROR)));
    assertThat(event.getOriginalModifierDamage(firstModifer), is(closeTo(firstModifierDamage, ERROR)));
    assertThat(event.getOriginalModifierDamage(secondModifier), is(closeTo(secondModifierDamage, ERROR)));
    assertThat(event.getOriginalDamage(), is(closeTo(originalDamage, ERROR)));
    assertThat(event.getOriginalFinalDamage(), is(closeTo(originalFinalDamage, ERROR)));
    assertThat(event.getFinalDamage(), is(closeTo(modifiedFinalDamage, ERROR)));
    assertThat(event.getOriginalFunctions(), is(Matchers.equalTo(originalFunctions)));
    assertThat(event.getModifiers(), is(Matchers.equalTo(newFunctions)));
}
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) DoubleUnaryOperator(java.util.function.DoubleUnaryOperator) Test(org.junit.Test)

Example 8 with DamageFunction

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

the class SpongeAbstractDamageEntityEventTest method testSetModifiers.

@Test
public void testSetModifiers() {
    Entity targetEntity = mockParam(Entity.class);
    final int originalDamage = 1;
    final int originalFinalDamage = 18;
    final int firstModifierDamage = 2;
    final int secondModifierDamage = 15;
    final int firstChangedDamage = 1;
    final int secondChangedDamage = 10;
    final int modifiedFinalDamage = 12;
    DamageModifier firstModifer = mockParam(DamageModifier.class);
    DamageModifier secondModifier = mockParam(DamageModifier.class);
    List<DamageFunction> originalFunctions = Lists.newArrayList(DamageFunction.of(firstModifer, p -> p * 2), DamageFunction.of(secondModifier, p -> p * 5));
    DamageEntityEvent event = SpongeEventFactory.createDamageEntityEvent(Cause.of(EventContext.empty(), "none"), originalFunctions, targetEntity, originalDamage);
    assertThat(event.getOriginalFunctions(), is(Matchers.equalTo(originalFunctions)));
    DoubleUnaryOperator newFunction = p -> p;
    event.setDamage(firstModifer, newFunction);
    assertThat(event.getDamage(firstModifer), is(closeTo(firstChangedDamage, ERROR)));
    assertThat(event.getDamage(secondModifier), is(closeTo(secondChangedDamage, ERROR)));
    assertThat(event.getOriginalModifierDamage(firstModifer), is(closeTo(firstModifierDamage, ERROR)));
    assertThat(event.getOriginalModifierDamage(secondModifier), is(closeTo(secondModifierDamage, ERROR)));
    assertThat(event.getOriginalDamage(), is(closeTo(originalDamage, ERROR)));
    assertThat(event.getOriginalFinalDamage(), is(closeTo(originalFinalDamage, ERROR)));
    assertThat(event.getFinalDamage(), is(closeTo(modifiedFinalDamage, ERROR)));
    assertThat(event.getOriginalFunctions(), is(Matchers.equalTo(originalFunctions)));
    assertThat(event.getModifiers(), is(Matchers.equalTo(Lists.newArrayList(DamageFunction.of(firstModifer, newFunction), originalFunctions.get(1)))));
}
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) DoubleUnaryOperator(java.util.function.DoubleUnaryOperator) Test(org.junit.Test)

Example 9 with DamageFunction

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

the class AbstractDamageEntityEvent method addModifierAfter.

@Override
public void addModifierAfter(DamageModifier damageModifier, DoubleUnaryOperator function, Set<DamageModifierType> after) {
    checkNotNull(damageModifier, "Damage modifier was null!");
    checkNotNull(function, "Function was null!");
    int indexToAddAfter = -1;
    int index = 0;
    for (ModifierFunction<DamageModifier> tuple : this.modifierFunctions) {
        checkArgument(!tuple.getModifier().equals(damageModifier), "Cannot add a duplicate modifier!");
        if (after.contains(tuple.getModifier().getType())) {
            indexToAddAfter = index;
        }
        index++;
    }
    if (indexToAddAfter == -1) {
        this.modifierFunctions.add(new DamageFunction(damageModifier, function));
    } else {
        this.modifierFunctions.add(indexToAddAfter + 1, new DamageFunction(damageModifier, function));
    }
    this.recalculateDamages(this.baseDamage);
}
Also used : DamageModifier(org.spongepowered.api.event.cause.entity.damage.DamageModifier) DamageFunction(org.spongepowered.api.event.cause.entity.damage.DamageFunction)

Example 10 with DamageFunction

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

the class AbstractAttackEntityEvent method addDamageModifierBefore.

@Override
public void addDamageModifierBefore(DamageModifier damageModifier, DoubleUnaryOperator function, Set<DamageModifierType> before) {
    checkNotNull(damageModifier, "Damage modifier was null!");
    checkNotNull(function, "Function was null!");
    int indexToAddBefore = -1;
    int index = 0;
    for (ModifierFunction<DamageModifier> tuple : this.modifierFunctions) {
        checkArgument(!tuple.getModifier().equals(damageModifier), "Cannot add a duplicate modifier!");
        if (before.contains(tuple.getModifier().getType())) {
            indexToAddBefore = index;
        }
        index++;
    }
    if (indexToAddBefore == -1) {
        this.modifierFunctions.add(new DamageFunction(damageModifier, function));
    } else {
        this.modifierFunctions.add(indexToAddBefore, new DamageFunction(damageModifier, function));
    }
    this.recalculateDamages(this.baseDamage);
}
Also used : DamageModifier(org.spongepowered.api.event.cause.entity.damage.DamageModifier) DamageFunction(org.spongepowered.api.event.cause.entity.damage.DamageFunction)

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