T00thpick1 1192f2a53a 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
2013-07-23 21:52:17 -05:00

94 lines
2.6 KiB
Java

package net.minecraft.server;
public class ItemFood extends Item {
public final int a;
private final int b;
private final float c;
private final boolean d;
private boolean cB;
private int cC;
private int cD;
private int cE;
private float cF;
public ItemFood(int i, int j, float f, boolean flag) {
super(i);
this.a = 32;
this.b = j;
this.d = flag;
this.c = f;
this.a(CreativeModeTab.h);
}
public ItemFood(int i, int j, boolean flag) {
this(i, j, 0.6F, flag);
}
public ItemStack b(ItemStack itemstack, World world, EntityHuman entityhuman) {
--itemstack.count;
// CraftBukkit start
int oldFoodLevel = entityhuman.getFoodData().foodLevel;
org.bukkit.event.entity.FoodLevelChangeEvent event = org.bukkit.craftbukkit.event.CraftEventFactory.callFoodLevelChangeEvent(entityhuman, this.getNutrition() + oldFoodLevel);
if (!event.isCancelled()) {
entityhuman.getFoodData().eat(event.getFoodLevel() - oldFoodLevel, this.getSaturationModifier());
}
((EntityPlayer) entityhuman).playerConnection.sendPacket(new Packet8UpdateHealth(((EntityPlayer) entityhuman).getBukkitEntity().getScaledHealth(), entityhuman.getFoodData().foodLevel, entityhuman.getFoodData().saturationLevel));
// CraftBukkit end
world.makeSound(entityhuman, "random.burp", 0.5F, world.random.nextFloat() * 0.1F + 0.9F);
this.c(itemstack, world, entityhuman);
return itemstack;
}
protected void c(ItemStack itemstack, World world, EntityHuman entityhuman) {
if (!world.isStatic && this.cC > 0 && world.random.nextFloat() < this.cF) {
entityhuman.addEffect(new MobEffect(this.cC, this.cD * 20, this.cE));
}
}
public int d_(ItemStack itemstack) {
return 32;
}
public EnumAnimation c_(ItemStack itemstack) {
return EnumAnimation.EAT;
}
public ItemStack a(ItemStack itemstack, World world, EntityHuman entityhuman) {
if (entityhuman.g(this.cB)) {
entityhuman.a(itemstack, this.d_(itemstack));
}
return itemstack;
}
public int getNutrition() {
return this.b;
}
public float getSaturationModifier() {
return this.c;
}
public boolean j() {
return this.d;
}
public ItemFood a(int i, int j, int k, float f) {
this.cC = i;
this.cD = j;
this.cE = k;
this.cF = f;
return this;
}
public ItemFood k() {
this.cB = true;
return this;
}
}