From 6c4b9e083342f5e0f72c89cb90fd1cfa2264e8ed Mon Sep 17 00:00:00 2001 From: themode Date: Thu, 10 Sep 2020 21:23:59 +0200 Subject: [PATCH] Added Player#isSprinting, Player#isSneaking and some cleanup related to last damage source + LivingEntity#isInvulnerable --- .../minestom/server/entity/LivingEntity.java | 35 ++++++++++++--- .../net/minestom/server/entity/Player.java | 45 ++++++++++--------- .../ai/target/LastEntityDamagerTarget.java | 2 +- 3 files changed, 53 insertions(+), 29 deletions(-) diff --git a/src/main/java/net/minestom/server/entity/LivingEntity.java b/src/main/java/net/minestom/server/entity/LivingEntity.java index a36e6a329..790152ae0 100644 --- a/src/main/java/net/minestom/server/entity/LivingEntity.java +++ b/src/main/java/net/minestom/server/entity/LivingEntity.java @@ -30,7 +30,7 @@ public abstract class LivingEntity extends Entity implements EquipmentHandler { protected boolean isDead; private float health; - private DamageType lastDamageType; + protected DamageType lastDamageSource; // Bounding box used for items' pickup (see LivingEntity#setBoundingBox) protected BoundingBox expandedBoundingBox; @@ -43,6 +43,9 @@ public abstract class LivingEntity extends Entity implements EquipmentHandler { // The number of arrows in entity private int arrowCount; + // Abilities + protected boolean invulnerable; + /** * Time at which this entity must be extinguished */ @@ -173,6 +176,24 @@ public abstract class LivingEntity extends Entity implements EquipmentHandler { sendMetadataIndex(11); } + /** + * Get if the entity is invulnerable + * + * @return true if the entity is invulnerable + */ + public boolean isInvulnerable() { + return invulnerable; + } + + /** + * Make the entity vulnerable or invulnerable + * + * @param invulnerable should the entity be invulnerable + */ + public void setInvulnerable(boolean invulnerable) { + this.invulnerable = invulnerable; + } + /** * Kill the entity, trigger the {@link EntityDeathEvent} event */ @@ -233,7 +254,7 @@ public abstract class LivingEntity extends Entity implements EquipmentHandler { public boolean damage(DamageType type, float value) { if (isDead()) return false; - if (isImmune(type)) { + if (isInvulnerable() || isImmune(type)) { return false; } @@ -280,7 +301,7 @@ public abstract class LivingEntity extends Entity implements EquipmentHandler { } // Set the last damage type since the event is not cancelled - this.lastDamageType = entityDamageEvent.getDamageType(); + this.lastDamageSource = entityDamageEvent.getDamageType(); }); return !entityDamageEvent.isCancelled(); @@ -321,12 +342,12 @@ public abstract class LivingEntity extends Entity implements EquipmentHandler { } /** - * Get the last damage type of this entity + * Get the last damage source which damaged of this entity * - * @return the last damage type, null if not any + * @return the last damage source, null if not any */ - public DamageType getLastDamageType() { - return lastDamageType; + public DamageType getLastDamageSource() { + return lastDamageSource; } /** diff --git a/src/main/java/net/minestom/server/entity/Player.java b/src/main/java/net/minestom/server/entity/Player.java index 8f2cbde92..c6ce3cbd1 100644 --- a/src/main/java/net/minestom/server/entity/Player.java +++ b/src/main/java/net/minestom/server/entity/Player.java @@ -110,17 +110,11 @@ public class Player extends LivingEntity implements CommandSender { private BelowNameTag belowNameTag; - /** - * Last damage source to hit this player, used to display the death message. - */ - private DamageType lastDamageSource; - private int permissionLevel; private boolean reducedDebugScreenInformation; // Abilities - private boolean invulnerable; private boolean flying; private boolean allowFlying; private boolean instantBreak; @@ -267,19 +261,6 @@ public class Player extends LivingEntity implements CommandSender { this.playerConnection.setPlayer(this); } - @Override - public boolean damage(DamageType type, float value) { - if (isInvulnerable()) - return false; - - // Compute final heart based on health and additional hearts - final boolean result = super.damage(type, value); - if (result) { - lastDamageSource = type; - } - return result; - } - @Override public float getAttributeValue(Attribute attribute) { if (attribute == Attribute.MOVEMENT_SPEED) { @@ -1444,6 +1425,28 @@ public class Player extends LivingEntity implements CommandSender { this.belowNameTag = belowNameTag; } + /** + * Get if the player is sneaking + *

+ * WARNING: this can be bypassed by hacked client, this is only what the client told the server + * + * @return true if the player is sneaking + */ + public boolean isSneaking() { + return crouched; + } + + /** + * Get if the player is sprinting + *

+ * WARNING: this can be bypassed by hacked client, this is only what the client told the server + * + * @return true if the player is sprinting + */ + public boolean isSprinting() { + return sprinting; + } + /** * Used to get the {@link CustomBlock} that the player is currently mining * @@ -1642,7 +1645,7 @@ public class Player extends LivingEntity implements CommandSender { * @return true if the player is invulnerable, false otherwise */ public boolean isInvulnerable() { - return invulnerable; + return super.isInvulnerable(); } /** @@ -1652,7 +1655,7 @@ public class Player extends LivingEntity implements CommandSender { * @param invulnerable should the player be invulnerable */ public void setInvulnerable(boolean invulnerable) { - this.invulnerable = invulnerable; + super.setInvulnerable(invulnerable); refreshAbilities(); } diff --git a/src/main/java/net/minestom/server/entity/ai/target/LastEntityDamagerTarget.java b/src/main/java/net/minestom/server/entity/ai/target/LastEntityDamagerTarget.java index a15783713..d6fbf50b9 100644 --- a/src/main/java/net/minestom/server/entity/ai/target/LastEntityDamagerTarget.java +++ b/src/main/java/net/minestom/server/entity/ai/target/LastEntityDamagerTarget.java @@ -20,7 +20,7 @@ public class LastEntityDamagerTarget extends TargetSelector { @Override public Entity findTarget() { - final DamageType damageType = entityCreature.getLastDamageType(); + final DamageType damageType = entityCreature.getLastDamageSource(); if (!(damageType instanceof EntityDamage)) { // No damager recorded, return null