From bf795c1442b5a3101d803686cfcc3f7c4574f56a Mon Sep 17 00:00:00 2001 From: Felix Cravic Date: Mon, 27 Apr 2020 22:38:11 +0200 Subject: [PATCH] Added EntityDamageEvent --- .../minestom/server/entity/LivingEntity.java | 22 +++++++++++----- .../net/minestom/server/entity/Player.java | 13 +++++----- .../server/entity/damage/DamageType.java | 12 ++++----- .../server/event/EntityDamageEvent.java | 26 +++++++++++++++++++ .../minestom/server/instance/Instance.java | 1 + .../network/player/PlayerConnection.java | 1 + 6 files changed, 56 insertions(+), 19 deletions(-) create mode 100644 src/main/java/net/minestom/server/event/EntityDamageEvent.java diff --git a/src/main/java/net/minestom/server/entity/LivingEntity.java b/src/main/java/net/minestom/server/entity/LivingEntity.java index 0cb52eb10..3445512aa 100644 --- a/src/main/java/net/minestom/server/entity/LivingEntity.java +++ b/src/main/java/net/minestom/server/entity/LivingEntity.java @@ -4,6 +4,7 @@ import net.minestom.server.collision.BoundingBox; import net.minestom.server.entity.damage.DamageType; import net.minestom.server.entity.property.Attribute; import net.minestom.server.event.DeathEvent; +import net.minestom.server.event.EntityDamageEvent; import net.minestom.server.event.PickupItemEvent; import net.minestom.server.instance.Chunk; import net.minestom.server.item.ItemStack; @@ -105,18 +106,25 @@ public abstract class LivingEntity extends Entity { } public void damage(DamageType type, float value) { - if(isImmune(type)) { + if (isImmune(type)) { return; } - EntityAnimationPacket entityAnimationPacket = new EntityAnimationPacket(); - entityAnimationPacket.entityId = getEntityId(); - entityAnimationPacket.animation = EntityAnimationPacket.Animation.TAKE_DAMAGE; - sendPacketToViewersAndSelf(entityAnimationPacket); - setHealth(getHealth() - value); + + EntityDamageEvent entityDamageEvent = new EntityDamageEvent(type, value); + callCancellableEvent(EntityDamageEvent.class, entityDamageEvent, () -> { + float damage = entityDamageEvent.getDamage(); + + EntityAnimationPacket entityAnimationPacket = new EntityAnimationPacket(); + entityAnimationPacket.entityId = getEntityId(); + entityAnimationPacket.animation = EntityAnimationPacket.Animation.TAKE_DAMAGE; + sendPacketToViewersAndSelf(entityAnimationPacket); + setHealth(getHealth() - damage); + }); } /** * Is this entity immune to the given type of damage? + * * @param type the type of damage * @return true iff this entity is immune to the given type of damage */ @@ -212,7 +220,7 @@ public abstract class LivingEntity extends Entity { @Override protected void handleVoid() { // Kill if in void - if(getInstance().isInVoid(this.position)) { + if (getInstance().isInVoid(this.position)) { damage(DamageType.VOID, 10f); } } diff --git a/src/main/java/net/minestom/server/entity/Player.java b/src/main/java/net/minestom/server/entity/Player.java index 31918c4f1..27dd683ee 100644 --- a/src/main/java/net/minestom/server/entity/Player.java +++ b/src/main/java/net/minestom/server/entity/Player.java @@ -117,7 +117,7 @@ public class Player extends LivingEntity { @Override public void damage(DamageType type, float value) { - if(!isImmune(type)) { + if (!isImmune(type)) { lastDamageSource = type; } super.damage(type, value); @@ -245,10 +245,10 @@ public class Player extends LivingEntity { @Override public void kill() { - if(!isDead()) { + if (!isDead()) { // send death message to player TextObject deathMessage; - if(lastDamageSource != null) { + if (lastDamageSource != null) { deathMessage = lastDamageSource.buildDeathMessage(); } else { // may happen if killed by the server without applying damage deathMessage = TextBuilder.of("Killed by poor programming.").build(); @@ -258,10 +258,10 @@ public class Player extends LivingEntity { // send death message to chat TextObject chatMessage; - if(lastDamageSource != null) { + if (lastDamageSource != null) { chatMessage = lastDamageSource.buildChatMessage(this); } else { // may happen if killed by the server without applying damage - chatMessage = TextBuilder.of(getUsername()+" was killed by poor programming.").build(); + chatMessage = TextBuilder.of(getUsername() + " was killed by poor programming.").build(); } MinecraftServer.getConnectionManager().getOnlinePlayers().forEach(player -> { player.sendMessage(chatMessage); @@ -455,7 +455,7 @@ public class Player extends LivingEntity { @Override public boolean isImmune(DamageType type) { - if(getGameMode().canTakeDamage()) { + if (getGameMode().canTakeDamage()) { return type != DamageType.VOID; } return super.isImmune(type); @@ -659,6 +659,7 @@ public class Player extends LivingEntity { /** * Returns true iff this player is in creative. Used for code readability + * * @return */ public boolean isCreative() { diff --git a/src/main/java/net/minestom/server/entity/damage/DamageType.java b/src/main/java/net/minestom/server/entity/damage/DamageType.java index 9b0342600..c0540cdd1 100644 --- a/src/main/java/net/minestom/server/entity/damage/DamageType.java +++ b/src/main/java/net/minestom/server/entity/damage/DamageType.java @@ -22,19 +22,19 @@ public class DamageType { return identifier; } - public TextObject buildChatMessage(Player killed) { - return TextBuilder.of(killed.getUsername()+" was killed by damage of type "+identifier).build(); + public static DamageType fromProjectile(Entity shooter, Entity projectile) { + return new EntityProjectileDamage(shooter, projectile); } - public TextObject buildDeathMessage() { - return TextBuilder.of("Killed by damage of type "+identifier).build(); + public TextObject buildChatMessage(Player killed) { + return TextBuilder.of(killed.getUsername() + " was killed by damage of type " + identifier).build(); } public static DamageType fromPlayer(Player player) { return new EntityDamage(player); } - public static DamageType fromProjectile(Entity shooter, Entity projectile) { - return new EntityProjectileDamage(shooter, projectile); + public TextObject buildDeathMessage() { + return TextBuilder.of("Killed by damage of type " + identifier).build(); } } diff --git a/src/main/java/net/minestom/server/event/EntityDamageEvent.java b/src/main/java/net/minestom/server/event/EntityDamageEvent.java new file mode 100644 index 000000000..0f9f0a6b2 --- /dev/null +++ b/src/main/java/net/minestom/server/event/EntityDamageEvent.java @@ -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; + } +} diff --git a/src/main/java/net/minestom/server/instance/Instance.java b/src/main/java/net/minestom/server/instance/Instance.java index 923fbb164..56f7a72fa 100644 --- a/src/main/java/net/minestom/server/instance/Instance.java +++ b/src/main/java/net/minestom/server/instance/Instance.java @@ -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. * Always returning false allow entities to survive in the void + * * @param position the position in the world * @return true iif position is inside the void */ diff --git a/src/main/java/net/minestom/server/network/player/PlayerConnection.java b/src/main/java/net/minestom/server/network/player/PlayerConnection.java index ac9d88dbf..ce347ec2b 100644 --- a/src/main/java/net/minestom/server/network/player/PlayerConnection.java +++ b/src/main/java/net/minestom/server/network/player/PlayerConnection.java @@ -33,6 +33,7 @@ public class PlayerConnection { public void sendPacket(ServerPacket serverPacket) { ByteBuf buffer = PacketUtils.writePacket(serverPacket); sendPacket(buffer); + buffer.release(); } public void flush() {