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);
}
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);
}
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);
}
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);
}
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)));
}
Aggregations