use of org.spongepowered.api.event.cause.entity.health.HealthFunction in project SpongeAPI by SpongePowered.
the class SpongeAbstractHealEntityEventTest method testModifiersApplicable.
@Test
public void testModifiersApplicable() {
Entity targetEntity = mockParam(Entity.class);
HealthModifier firstModifer = mockParam(HealthModifier.class);
HealthModifier secondModifier = mockParam(HealthModifier.class);
List<HealthFunction> originalFunctions = Lists.newArrayList(HealthFunction.of(firstModifer, p -> p), HealthFunction.of(secondModifier, p -> p));
HealEntityEvent event = SpongeEventFactory.createHealEntityEvent(Cause.of(EventContext.empty(), "none"), originalFunctions, targetEntity, 0);
assertThat(event.isModifierApplicable(firstModifer), is(true));
assertThat(event.isModifierApplicable(secondModifier), is(true));
assertThat(event.isModifierApplicable(mockParam(HealthModifier.class)), is(false));
}
use of org.spongepowered.api.event.cause.entity.health.HealthFunction in project SpongeAPI by SpongePowered.
the class AbstractHealEntityEvent method setHealAmount.
@Override
public final void setHealAmount(HealthModifier healthModifier, DoubleUnaryOperator function) {
checkNotNull(healthModifier, "Damage modifier was null!");
checkNotNull(function, "Function was null!");
int indexToAddTo = 0;
boolean addAtEnd = true;
for (Iterator<HealthFunction> iterator = this.modifierFunctions.iterator(); iterator.hasNext(); ) {
ModifierFunction<HealthModifier> tuple = iterator.next();
if (tuple.getModifier().equals(healthModifier)) {
iterator.remove();
addAtEnd = false;
break;
}
indexToAddTo++;
}
if (addAtEnd) {
this.modifierFunctions.add(new HealthFunction(healthModifier, function));
} else {
this.modifierFunctions.add(indexToAddTo, new HealthFunction(healthModifier, function));
}
this.recalculateDamages(this.baseHealAmount);
}
use of org.spongepowered.api.event.cause.entity.health.HealthFunction in project SpongeAPI by SpongePowered.
the class SpongeAbstractHealEntityEventTest 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;
HealthModifier firstModifer = mockParam(HealthModifier.class);
HealthModifier secondModifier = mockParam(HealthModifier.class);
List<HealthFunction> originalFunctions = Lists.newArrayList(HealthFunction.of(firstModifer, p -> p * 2), HealthFunction.of(secondModifier, p -> p * 5));
HealEntityEvent event = SpongeEventFactory.createHealEntityEvent(Cause.of(EventContext.empty(), "none"), originalFunctions, targetEntity, originalDamage);
assertThat(event.getOriginalFunctions(), is(Matchers.equalTo(originalFunctions)));
assertThat(event.getOriginalHealAmount(), is(closeTo(originalDamage, ERROR)));
assertThat(event.getOriginalFinalHealAmount(), is(closeTo(originalFinalDamage, ERROR)));
Map<HealthModifier, Double> originalDamages = event.getOriginalHealingAmounts();
assertThat(originalDamages.size(), is(originalFunctions.size()));
assertThat(originalDamages.get(firstModifer), is(closeTo(firstModifierDamage, ERROR)));
assertThat(originalDamages.get(secondModifier), is(closeTo(secondModifierDamage, ERROR)));
assertThat(event.getOriginalHealingModifierAmount(firstModifer), is(closeTo(firstModifierDamage, ERROR)));
assertThat(event.getOriginalHealingModifierAmount(secondModifier), is(closeTo(secondModifierDamage, ERROR)));
assertThat(event.getOriginalFunctions(), is(Matchers.equalTo(originalFunctions)));
}
use of org.spongepowered.api.event.cause.entity.health.HealthFunction in project SpongeAPI by SpongePowered.
the class SpongeAbstractHealEntityEventTest 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;
HealthModifier firstModifer = mockParam(HealthModifier.class);
HealthModifier secondModifier = mockParam(HealthModifier.class);
HealthModifier thirdModifier = mockParam(HealthModifier.class);
DoubleUnaryOperator thirdFunction = p -> p;
List<HealthFunction> originalFunctions = Lists.newArrayList(HealthFunction.of(firstModifer, p -> p * 2), HealthFunction.of(secondModifier, p -> p * 5));
List<HealthFunction> newFunctions = Lists.newArrayList(originalFunctions);
newFunctions.add(HealthFunction.of(thirdModifier, thirdFunction));
HealEntityEvent event = SpongeEventFactory.createHealEntityEvent(Cause.of(EventContext.empty(), "none"), originalFunctions, targetEntity, originalDamage);
assertThat(event.getOriginalFunctions(), is(Matchers.equalTo(originalFunctions)));
assertThat(event.isModifierApplicable(thirdModifier), is(false));
event.setHealAmount(thirdModifier, thirdFunction);
assertThat(event.getHealAmount(firstModifer), is(closeTo(firstModifierDamage, ERROR)));
assertThat(event.getHealAmount(secondModifier), is(closeTo(secondModifierDamage, ERROR)));
assertThat(event.getHealAmount(thirdModifier), is(closeTo(thirdDamage, ERROR)));
assertThat(event.getOriginalHealingModifierAmount(firstModifer), is(closeTo(firstModifierDamage, ERROR)));
assertThat(event.getOriginalHealingModifierAmount(secondModifier), is(closeTo(secondModifierDamage, ERROR)));
assertThat(event.getOriginalHealAmount(), is(closeTo(originalDamage, ERROR)));
assertThat(event.getOriginalFinalHealAmount(), is(closeTo(originalFinalDamage, ERROR)));
assertThat(event.getFinalHealAmount(), is(closeTo(modifiedFinalDamage, ERROR)));
assertThat(event.getOriginalFunctions(), is(Matchers.equalTo(originalFunctions)));
assertThat(event.getModifiers(), is(Matchers.equalTo(newFunctions)));
}
use of org.spongepowered.api.event.cause.entity.health.HealthFunction in project SpongeAPI by SpongePowered.
the class SpongeAbstractHealEntityEventTest 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;
HealthModifier firstModifer = mockParam(HealthModifier.class);
HealthModifier secondModifier = mockParam(HealthModifier.class);
List<HealthFunction> originalFunctions = Lists.newArrayList(HealthFunction.of(firstModifer, p -> p * 2), HealthFunction.of(secondModifier, p -> p * 5));
HealEntityEvent event = SpongeEventFactory.createHealEntityEvent(Cause.of(EventContext.empty(), "none"), originalFunctions, targetEntity, originalDamage);
assertThat(event.getOriginalFunctions(), is(Matchers.equalTo(originalFunctions)));
DoubleUnaryOperator newFunction = p -> p;
event.setHealAmount(firstModifer, newFunction);
assertThat(event.getHealAmount(firstModifer), is(closeTo(firstChangedDamage, ERROR)));
assertThat(event.getHealAmount(secondModifier), is(closeTo(secondChangedDamage, ERROR)));
assertThat(event.getOriginalHealingModifierAmount(firstModifer), is(closeTo(firstModifierDamage, ERROR)));
assertThat(event.getOriginalHealingModifierAmount(secondModifier), is(closeTo(secondModifierDamage, ERROR)));
assertThat(event.getOriginalHealAmount(), is(closeTo(originalDamage, ERROR)));
assertThat(event.getOriginalFinalHealAmount(), is(closeTo(originalFinalDamage, ERROR)));
assertThat(event.getFinalHealAmount(), is(closeTo(modifiedFinalDamage, ERROR)));
assertThat(event.getOriginalFunctions(), is(Matchers.equalTo(originalFunctions)));
assertThat(event.getModifiers(), is(Matchers.equalTo(Lists.newArrayList(HealthFunction.of(firstModifer, newFunction), originalFunctions.get(1)))));
}
Aggregations