Added Player#isSprinting, Player#isSneaking and some cleanup related to last damage source + LivingEntity#isInvulnerable

This commit is contained in:
themode 2020-09-10 21:23:59 +02:00
parent 3473ccfb41
commit 6c4b9e0833
3 changed files with 53 additions and 29 deletions

View File

@ -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;
}
/**

View File

@ -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
* <p>
* 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
* <p>
* 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();
}

View File

@ -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