Added EntityDamageEvent

This commit is contained in:
Felix Cravic 2020-04-27 22:38:11 +02:00
parent eaf9349613
commit bf795c1442
6 changed files with 56 additions and 19 deletions

View File

@ -4,6 +4,7 @@ import net.minestom.server.collision.BoundingBox;
import net.minestom.server.entity.damage.DamageType; import net.minestom.server.entity.damage.DamageType;
import net.minestom.server.entity.property.Attribute; import net.minestom.server.entity.property.Attribute;
import net.minestom.server.event.DeathEvent; import net.minestom.server.event.DeathEvent;
import net.minestom.server.event.EntityDamageEvent;
import net.minestom.server.event.PickupItemEvent; import net.minestom.server.event.PickupItemEvent;
import net.minestom.server.instance.Chunk; import net.minestom.server.instance.Chunk;
import net.minestom.server.item.ItemStack; import net.minestom.server.item.ItemStack;
@ -108,15 +109,22 @@ public abstract class LivingEntity extends Entity {
if (isImmune(type)) { if (isImmune(type)) {
return; return;
} }
EntityDamageEvent entityDamageEvent = new EntityDamageEvent(type, value);
callCancellableEvent(EntityDamageEvent.class, entityDamageEvent, () -> {
float damage = entityDamageEvent.getDamage();
EntityAnimationPacket entityAnimationPacket = new EntityAnimationPacket(); EntityAnimationPacket entityAnimationPacket = new EntityAnimationPacket();
entityAnimationPacket.entityId = getEntityId(); entityAnimationPacket.entityId = getEntityId();
entityAnimationPacket.animation = EntityAnimationPacket.Animation.TAKE_DAMAGE; entityAnimationPacket.animation = EntityAnimationPacket.Animation.TAKE_DAMAGE;
sendPacketToViewersAndSelf(entityAnimationPacket); sendPacketToViewersAndSelf(entityAnimationPacket);
setHealth(getHealth() - value); setHealth(getHealth() - damage);
});
} }
/** /**
* Is this entity immune to the given type of damage? * Is this entity immune to the given type of damage?
*
* @param type the type of damage * @param type the type of damage
* @return true iff this entity is immune to the given type of damage * @return true iff this entity is immune to the given type of damage
*/ */

View File

@ -659,6 +659,7 @@ public class Player extends LivingEntity {
/** /**
* Returns true iff this player is in creative. Used for code readability * Returns true iff this player is in creative. Used for code readability
*
* @return * @return
*/ */
public boolean isCreative() { public boolean isCreative() {

View File

@ -22,19 +22,19 @@ public class DamageType {
return identifier; return identifier;
} }
public TextObject buildChatMessage(Player killed) { public static DamageType fromProjectile(Entity shooter, Entity projectile) {
return TextBuilder.of(killed.getUsername()+" was killed by damage of type "+identifier).build(); return new EntityProjectileDamage(shooter, projectile);
} }
public TextObject buildDeathMessage() { public TextObject buildChatMessage(Player killed) {
return TextBuilder.of("Killed by damage of type "+identifier).build(); return TextBuilder.of(killed.getUsername() + " was killed by damage of type " + identifier).build();
} }
public static DamageType fromPlayer(Player player) { public static DamageType fromPlayer(Player player) {
return new EntityDamage(player); return new EntityDamage(player);
} }
public static DamageType fromProjectile(Entity shooter, Entity projectile) { public TextObject buildDeathMessage() {
return new EntityProjectileDamage(shooter, projectile); return TextBuilder.of("Killed by damage of type " + identifier).build();
} }
} }

View File

@ -0,0 +1,26 @@
package net.minestom.server.event;
import net.minestom.server.entity.damage.DamageType;
public class EntityDamageEvent extends CancellableEvent {
private DamageType damageType;
private float damage;
public EntityDamageEvent(DamageType damageType, float damage) {
this.damageType = damageType;
this.damage = damage;
}
public DamageType getDamageType() {
return damageType;
}
public float getDamage() {
return damage;
}
public void setDamage(float damage) {
this.damage = damage;
}
}

View File

@ -95,6 +95,7 @@ public abstract class Instance implements BlockModifier, DataContainer {
/** /**
* Determines whether a position in the void. If true, entities should take damage and die. * Determines whether a position in the void. If true, entities should take damage and die.
* Always returning false allow entities to survive in the void * Always returning false allow entities to survive in the void
*
* @param position the position in the world * @param position the position in the world
* @return true iif position is inside the void * @return true iif position is inside the void
*/ */

View File

@ -33,6 +33,7 @@ public class PlayerConnection {
public void sendPacket(ServerPacket serverPacket) { public void sendPacket(ServerPacket serverPacket) {
ByteBuf buffer = PacketUtils.writePacket(serverPacket); ByteBuf buffer = PacketUtils.writePacket(serverPacket);
sendPacket(buffer); sendPacket(buffer);
buffer.release();
} }
public void flush() { public void flush() {