Add sound and animation to damage event (#506)

This commit is contained in:
Bloepiloepi 2021-12-26 14:59:19 +01:00 committed by TheMode
parent e53f0c5995
commit 1a7fece34e
2 changed files with 49 additions and 4 deletions

View File

@ -335,14 +335,16 @@ public class LivingEntity extends Entity implements EquipmentHandler {
return false;
}
EntityDamageEvent entityDamageEvent = new EntityDamageEvent(this, type, value);
EntityDamageEvent entityDamageEvent = new EntityDamageEvent(this, type, value, type.getSound(this));
EventDispatcher.callCancellable(entityDamageEvent, () -> {
// Set the last damage type since the event is not cancelled
this.lastDamageSource = entityDamageEvent.getDamageType();
float remainingDamage = entityDamageEvent.getDamage();
sendPacketToViewersAndSelf(new EntityAnimationPacket(getEntityId(), EntityAnimationPacket.Animation.TAKE_DAMAGE));
if (entityDamageEvent.shouldAnimate()) {
sendPacketToViewersAndSelf(new EntityAnimationPacket(getEntityId(), EntityAnimationPacket.Animation.TAKE_DAMAGE));
}
// Additional hearts support
if (this instanceof Player player) {
@ -362,7 +364,7 @@ public class LivingEntity extends Entity implements EquipmentHandler {
setHealth(getHealth() - remainingDamage);
// play damage sound
final SoundEvent sound = type.getSound(this);
final SoundEvent sound = entityDamageEvent.getSound();
if (sound != null) {
Source soundCategory;
if (this instanceof Player) {

View File

@ -5,7 +5,9 @@ import net.minestom.server.entity.LivingEntity;
import net.minestom.server.entity.damage.DamageType;
import net.minestom.server.event.trait.CancellableEvent;
import net.minestom.server.event.trait.EntityInstanceEvent;
import net.minestom.server.sound.SoundEvent;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* Called with {@link LivingEntity#damage(DamageType, float)}.
@ -15,13 +17,17 @@ public class EntityDamageEvent implements EntityInstanceEvent, CancellableEvent
private final Entity entity;
private final DamageType damageType;
private float damage;
private SoundEvent sound;
private boolean animation = true;
private boolean cancelled;
public EntityDamageEvent(@NotNull LivingEntity entity, @NotNull DamageType damageType, float damage) {
public EntityDamageEvent(@NotNull LivingEntity entity, @NotNull DamageType damageType,
float damage, @Nullable SoundEvent sound) {
this.entity = entity;
this.damageType = damageType;
this.damage = damage;
this.sound = sound;
}
@NotNull
@ -58,6 +64,43 @@ public class EntityDamageEvent implements EntityInstanceEvent, CancellableEvent
this.damage = damage;
}
/**
* Gets the damage sound.
*
* @return the damage sound
*/
@Nullable
public SoundEvent getSound() {
return sound;
}
/**
* Changes the damage sound.
*
* @param sound the new damage sound
*/
public void setSound(@Nullable SoundEvent sound) {
this.sound = sound;
}
/**
* Gets whether the damage animation should be played.
*
* @return true if the animation should be played
*/
public boolean shouldAnimate() {
return animation;
}
/**
* Sets whether the damage animation should be played.
*
* @param animation whether the animation should be played or not
*/
public void setAnimation(boolean animation) {
this.animation = animation;
}
@Override
public boolean isCancelled() {
return cancelled;