From 22511ca05242ec808a02b2748b6bc7cf1af579e7 Mon Sep 17 00:00:00 2001 From: Felix Cravic Date: Mon, 27 Apr 2020 23:03:21 +0200 Subject: [PATCH] Added damage application verification --- .../java/net/minestom/server/entity/LivingEntity.java | 11 +++++++++-- src/main/java/net/minestom/server/entity/Player.java | 7 ++++--- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/main/java/net/minestom/server/entity/LivingEntity.java b/src/main/java/net/minestom/server/entity/LivingEntity.java index 3445512aa..9923fb00b 100644 --- a/src/main/java/net/minestom/server/entity/LivingEntity.java +++ b/src/main/java/net/minestom/server/entity/LivingEntity.java @@ -105,9 +105,14 @@ public abstract class LivingEntity extends Entity { callEvent(DeathEvent.class, deathEvent); } - public void damage(DamageType type, float value) { + /** + * @param type the damage type + * @param value the amount of damage + * @return true if damage has been applied, false if it didn't + */ + public boolean damage(DamageType type, float value) { if (isImmune(type)) { - return; + return false; } EntityDamageEvent entityDamageEvent = new EntityDamageEvent(type, value); @@ -120,6 +125,8 @@ public abstract class LivingEntity extends Entity { sendPacketToViewersAndSelf(entityAnimationPacket); setHealth(getHealth() - damage); }); + + return !entityDamageEvent.isCancelled(); } /** diff --git a/src/main/java/net/minestom/server/entity/Player.java b/src/main/java/net/minestom/server/entity/Player.java index 27dd683ee..23abaab26 100644 --- a/src/main/java/net/minestom/server/entity/Player.java +++ b/src/main/java/net/minestom/server/entity/Player.java @@ -116,11 +116,12 @@ public class Player extends LivingEntity { } @Override - public void damage(DamageType type, float value) { - if (!isImmune(type)) { + public boolean damage(DamageType type, float value) { + boolean result = super.damage(type, value); + if (result) { lastDamageSource = type; } - super.damage(type, value); + return result; } @Override