Search in sources :

Example 16 with DamageFunction

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

the class AbstractDamageEntityEvent 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)

Example 17 with DamageFunction

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

the class AbstractDamageEntityEvent method setDamage.

@Override
public final void setDamage(DamageModifier damageModifier, DoubleUnaryOperator function) {
    checkNotNull(damageModifier, "Damage modifier was null!");
    checkNotNull(function, "Function was null!");
    int indexToAddTo = 0;
    boolean addAtEnd = true;
    for (Iterator<DamageFunction> iterator = this.modifierFunctions.iterator(); iterator.hasNext(); ) {
        ModifierFunction<DamageModifier> tuple = iterator.next();
        if (tuple.getModifier().equals(damageModifier)) {
            iterator.remove();
            addAtEnd = false;
            break;
        }
        indexToAddTo++;
    }
    if (addAtEnd) {
        this.modifierFunctions.add(new DamageFunction(damageModifier, function));
    } else {
        this.modifierFunctions.add(indexToAddTo, 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 18 with DamageFunction

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

the class AbstractAttackEntityEvent method setOutputDamage.

@Override
public final void setOutputDamage(DamageModifier damageModifier, DoubleUnaryOperator function) {
    checkNotNull(damageModifier, "Damage modifier was null!");
    checkNotNull(function, "Function was null!");
    int indexToAddTo = 0;
    boolean addAtEnd = true;
    for (Iterator<DamageFunction> iterator = this.modifierFunctions.iterator(); iterator.hasNext(); ) {
        ModifierFunction<DamageModifier> tuple = iterator.next();
        if (tuple.getModifier().equals(damageModifier)) {
            iterator.remove();
            addAtEnd = false;
            break;
        }
        indexToAddTo++;
    }
    if (addAtEnd) {
        this.modifierFunctions.add(new DamageFunction(damageModifier, function));
    } else {
        this.modifierFunctions.add(indexToAddTo, 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 19 with DamageFunction

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

the class AbstractAttackEntityEvent method addDamageModifierAfter.

@Override
public void addDamageModifierAfter(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 20 with DamageFunction

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

the class SpongeAbstractDamageEntityEventTest method testUseModifiers.

@Test
public void testUseModifiers() {
    Entity targetEntity = mockParam(Entity.class);
    final int originalDamage = 1;
    final int originalFinalDamage = 18;
    final int firstModifierDamage = 2;
    final int secondModifierDamage = 15;
    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);
    final List<DamageFunction> originalFunctions1 = event.getOriginalFunctions();
    assertThat(originalFunctions1, is(Matchers.equalTo(originalFunctions)));
    assertThat(event.getOriginalDamage(), is(closeTo(originalDamage, ERROR)));
    assertThat(event.getOriginalFinalDamage(), is(closeTo(originalFinalDamage, ERROR)));
    Map<DamageModifier, Double> originalDamages = event.getOriginalDamages();
    assertThat(originalDamages.size(), is(originalFunctions.size()));
    assertThat(originalDamages.get(firstModifer), is(closeTo(firstModifierDamage, ERROR)));
    assertThat(originalDamages.get(secondModifier), is(closeTo(secondModifierDamage, ERROR)));
    assertThat(event.getOriginalModifierDamage(firstModifer), is(closeTo(firstModifierDamage, ERROR)));
    assertThat(event.getOriginalModifierDamage(secondModifier), is(closeTo(secondModifierDamage, ERROR)));
    assertThat(event.getOriginalFunctions(), is(Matchers.equalTo(originalFunctions)));
}
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)

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