use of org.bukkit.event.entity.EntityRegainHealthEvent in project Essentials by drtshock.
the class Commandheal method healPlayer.
private void healPlayer(final User user) throws PlayerExemptException, QuietAbortException {
final Player player = user.getBase();
if (player.getHealth() == 0) {
throw new PlayerExemptException(tl("healDead"));
}
final double amount = player.getMaxHealth() - player.getHealth();
final EntityRegainHealthEvent erhe = new EntityRegainHealthEvent(player, amount, RegainReason.CUSTOM);
ess.getServer().getPluginManager().callEvent(erhe);
if (erhe.isCancelled()) {
throw new QuietAbortException();
}
double newAmount = player.getHealth() + erhe.getAmount();
if (newAmount > player.getMaxHealth()) {
newAmount = player.getMaxHealth();
}
player.setHealth(newAmount);
player.setFoodLevel(20);
player.setFireTicks(0);
user.sendMessage(tl("heal"));
for (PotionEffect effect : player.getActivePotionEffects()) {
player.removePotionEffect(effect.getType());
}
}
use of org.bukkit.event.entity.EntityRegainHealthEvent in project Essentials by EssentialsX.
the class Commandheal method healPlayer.
private void healPlayer(final User user) throws PlayerExemptException, QuietAbortException {
final Player player = user.getBase();
if (player.getHealth() == 0) {
throw new PlayerExemptException(tl("healDead"));
}
final double amount = player.getMaxHealth() - player.getHealth();
final EntityRegainHealthEvent erhe = new EntityRegainHealthEvent(player, amount, RegainReason.CUSTOM);
ess.getServer().getPluginManager().callEvent(erhe);
if (erhe.isCancelled()) {
throw new QuietAbortException();
}
double newAmount = player.getHealth() + erhe.getAmount();
if (newAmount > player.getMaxHealth()) {
newAmount = player.getMaxHealth();
}
player.setHealth(newAmount);
player.setFoodLevel(20);
player.setFireTicks(0);
user.sendMessage(tl("heal"));
for (PotionEffect effect : player.getActivePotionEffects()) {
player.removePotionEffect(effect.getType());
}
}
use of org.bukkit.event.entity.EntityRegainHealthEvent in project AuthMeReloaded by AuthMe.
the class EntityListenerTest method shouldNotCancelRegainedHealth.
@Test
public void shouldNotCancelRegainedHealth() {
// given
EntityRegainHealthEvent event = mock(EntityRegainHealthEvent.class);
given(listenerService.shouldCancelEvent(event)).willReturn(false);
// when
listener.entityRegainHealthEvent(event);
// then
verify(listenerService).shouldCancelEvent(event);
verifyNoInteractions(event);
}
use of org.bukkit.event.entity.EntityRegainHealthEvent in project AuthMeReloaded by AuthMe.
the class EntityListenerTest method shouldCancelRegainHealthEvent.
@Test
public void shouldCancelRegainHealthEvent() {
// given
EntityRegainHealthEvent event = mock(EntityRegainHealthEvent.class);
given(listenerService.shouldCancelEvent(event)).willReturn(true);
// when
listener.entityRegainHealthEvent(event);
// then
verify(listenerService).shouldCancelEvent(event);
verify(event).setCancelled(true);
verify(event).setAmount(0);
}
use of org.bukkit.event.entity.EntityRegainHealthEvent in project MagicPlugin by elBukkit.
the class HealAction method perform.
@Override
public SpellResult perform(CastContext context) {
Entity entity = context.getTargetEntity();
if (!(entity instanceof LivingEntity)) {
return SpellResult.NO_TARGET;
}
LivingEntity targetEntity = (LivingEntity) entity;
if (targetEntity.getHealth() == targetEntity.getMaxHealth() || targetEntity.isDead()) {
return SpellResult.NO_TARGET;
}
double healAmount = amount;
if (percentage > 0) {
healAmount = targetEntity.getMaxHealth() * percentage;
}
if (maxDistanceSquared > 0) {
double distanceSquared = context.getLocation().distanceSquared(entity.getLocation());
if (distanceSquared > maxDistanceSquared) {
return SpellResult.NO_TARGET;
}
if (distanceSquared > 0) {
healAmount = healAmount * (1 - distanceSquared / maxDistanceSquared);
}
}
EntityRegainHealthEvent event = new EntityRegainHealthEvent(targetEntity, healAmount, RegainReason.CUSTOM);
Bukkit.getServer().getPluginManager().callEvent(event);
if (event.isCancelled()) {
return SpellResult.CANCELLED;
}
healAmount = event.getAmount();
if (healAmount == 0) {
return SpellResult.NO_TARGET;
}
context.registerModified(targetEntity);
targetEntity.setHealth(Math.min(targetEntity.getHealth() + healAmount, targetEntity.getMaxHealth()));
return SpellResult.CAST;
}
Aggregations