Add API to control scaled health. Adds BUKKIT-4590

This commit implements the ability to set the scale of hearts that the
client renders.  When the Packet44UpdateAttributes packet is sent, the
max health attribute is replaced with a scaled version, to preserve the
scaled health illusion clientside.

In order to accurately display the scaled health for players, a true
health is stored within CraftPlayer, and the datawatcher now stores the
scaled health. The getHealth() method for players still returns their
true health.

Changed setHealth() within EntityLiving to appropriately handle health
for instances of EntityPlayer. Inlined a call to
setHealth(getMaxHealth()) within the EntityLiving constructor to work
around CraftEntity instantiation.

Additionally fixes the health values sent when eating food within
FoodMetaData and ItemFood, which previously sent the unscaled health;
this commit alters them to send the properly scaled health.

Additionally fixes BUKKIT-4535, BUKKIT-4536, and BUKKIT-4127
This commit is contained in:
T00thpick1
2013-07-23 21:30:38 -05:00
committed by Wesley Wolfe
parent 4ad3cdd4b5
commit 1192f2a53a
7 changed files with 106 additions and 11 deletions

View File

@@ -82,7 +82,8 @@ public abstract class EntityLiving extends Entity {
public EntityLiving(World world) {
super(world);
this.ay();
this.setHealth(this.getMaxHealth());
// CraftBukkit - setHealth(getMaxHealth()) -> current - inlined to skip the instanceof check for EntityPlayers
this.datawatcher.watch(6, (float) this.getAttributeInstance(GenericAttributes.a).getValue());
this.m = true;
this.aM = (float) (Math.random() + 1.0D) * 0.01F;
this.setPosition(this.locX, this.locY, this.locZ);
@@ -583,10 +584,31 @@ public abstract class EntityLiving extends Entity {
}
public final float getHealth() {
// CraftBukkit start - Scaled Health
if (this instanceof EntityPlayer) {
return (float) ((EntityPlayer) this).getBukkitEntity().getHealth();
}
// CraftBukkit end
return this.datawatcher.getFloat(6);
}
public void setHealth(float f) {
// CraftBukkit start - Scaled Health
if (this instanceof EntityPlayer) {
org.bukkit.craftbukkit.entity.CraftPlayer player = ((EntityPlayer) this).getBukkitEntity();
// Squeeze
if (f < 0.0F) {
player.setRealHealth(0.0D);
} else if (f > player.getMaxHealth()) {
player.setRealHealth(player.getMaxHealth());
} else {
player.setRealHealth(f);
}
this.datawatcher.watch(6, Float.valueOf(player.getScaledHealth()));
return;
}
// CraftBukkit end
this.datawatcher.watch(6, Float.valueOf(MathHelper.a(f, 0.0F, this.getMaxHealth())));
}