use of org.spongepowered.api.event.entity.HealEntityEvent 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)))));
}
use of org.spongepowered.api.event.entity.HealEntityEvent in project SpongeAPI by SpongePowered.
the class SpongeAbstractHealEntityEventTest method testNotApplicableModifer.
@Test(expected = IllegalArgumentException.class)
public void testNotApplicableModifer() {
HealEntityEvent event = SpongeEventFactory.createHealEntityEvent(Cause.of(EventContext.empty(), "none"), Lists.newArrayList(), mockParam(Entity.class), 0);
HealthModifier modifier = mockParam(HealthModifier.class);
assertThat(event.isModifierApplicable(modifier), is(false));
event.getOriginalHealingModifierAmount(modifier);
}
use of org.spongepowered.api.event.entity.HealEntityEvent in project LanternServer by LanternPowered.
the class LanternEntity method heal.
/**
* Heals the entity for the specified amount.
*
* <p>Will not heal them if they are dead and will not set
* them above their maximum health.</p>
*
* @param amount The amount to heal for
* @param source The healing source
*/
public boolean heal(double amount, HealingSource source) {
if (isDead()) {
return false;
}
final MutableBoundedValue<Double> health = getValue(Keys.HEALTH).orElse(null);
if (health == null || health.get() >= health.getMaxValue()) {
return false;
}
final CauseStack causeStack = CauseStack.current();
try (CauseStack.Frame frame = causeStack.pushCauseFrame()) {
frame.pushCause(source);
frame.addContext(LanternEventContextKeys.HEALING_TYPE, source.getHealingType());
final HealEntityEvent event = SpongeEventFactory.createHealEntityEvent(frame.getCurrentCause(), new ArrayList<>(), this, amount);
Sponge.getEventManager().post(event);
if (event.isCancelled()) {
return false;
}
amount = event.getFinalHealAmount();
if (amount > 0) {
offer(Keys.HEALTH, Math.min(health.get() + amount, health.getMaxValue()));
}
}
return true;
}
Aggregations