Paper/src/main/java/net/minecraft/server/FoodMetaData.java
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

100 Zeilen
3.6 KiB
Java

package net.minecraft.server;
import org.bukkit.event.entity.EntityDamageEvent; // CraftBukkit
public class FoodMetaData {
// CraftBukkit start - All made public
public int foodLevel = 20;
public float saturationLevel = 5.0F;
public float exhaustionLevel;
public int foodTickTimer;
// CraftBukkit end
private int e = 20;
public FoodMetaData() {}
public void eat(int i, float f) {
this.foodLevel = Math.min(i + this.foodLevel, 20);
this.saturationLevel = Math.min(this.saturationLevel + (float) i * f * 2.0F, (float) this.foodLevel);
}
public void a(ItemFood itemfood) {
this.eat(itemfood.getNutrition(), itemfood.getSaturationModifier());
}
public void a(EntityHuman entityhuman) {
int i = entityhuman.world.difficulty;
this.e = this.foodLevel;
if (this.exhaustionLevel > 4.0F) {
this.exhaustionLevel -= 4.0F;
if (this.saturationLevel > 0.0F) {
this.saturationLevel = Math.max(this.saturationLevel - 1.0F, 0.0F);
} else if (i > 0) {
// CraftBukkit start
org.bukkit.event.entity.FoodLevelChangeEvent event = org.bukkit.craftbukkit.event.CraftEventFactory.callFoodLevelChangeEvent(entityhuman, Math.max(this.foodLevel - 1, 0));
if (!event.isCancelled()) {
this.foodLevel = event.getFoodLevel();
}
((EntityPlayer) entityhuman).playerConnection.sendPacket(new Packet8UpdateHealth(((EntityPlayer) entityhuman).getBukkitEntity().getScaledHealth(), this.foodLevel, this.saturationLevel));
// CraftBukkit end
}
}
if (entityhuman.world.getGameRules().getBoolean("naturalRegeneration") && this.foodLevel >= 18 && entityhuman.bI()) {
++this.foodTickTimer;
if (this.foodTickTimer >= 80) {
// CraftBukkit - added RegainReason
entityhuman.heal(1.0F, org.bukkit.event.entity.EntityRegainHealthEvent.RegainReason.SATIATED);
this.foodTickTimer = 0;
}
} else if (this.foodLevel <= 0) {
++this.foodTickTimer;
if (this.foodTickTimer >= 80) {
if (entityhuman.getHealth() > 10.0F || i >= 3 || entityhuman.getHealth() > 1.0F && i >= 2) {
entityhuman.damageEntity(DamageSource.STARVE, 1.0F);
}
this.foodTickTimer = 0;
}
} else {
this.foodTickTimer = 0;
}
}
public void a(NBTTagCompound nbttagcompound) {
if (nbttagcompound.hasKey("foodLevel")) {
this.foodLevel = nbttagcompound.getInt("foodLevel");
this.foodTickTimer = nbttagcompound.getInt("foodTickTimer");
this.saturationLevel = nbttagcompound.getFloat("foodSaturationLevel");
this.exhaustionLevel = nbttagcompound.getFloat("foodExhaustionLevel");
}
}
public void b(NBTTagCompound nbttagcompound) {
nbttagcompound.setInt("foodLevel", this.foodLevel);
nbttagcompound.setInt("foodTickTimer", this.foodTickTimer);
nbttagcompound.setFloat("foodSaturationLevel", this.saturationLevel);
nbttagcompound.setFloat("foodExhaustionLevel", this.exhaustionLevel);
}
public int a() {
return this.foodLevel;
}
public boolean c() {
return this.foodLevel < 20;
}
public void a(float f) {
this.exhaustionLevel = Math.min(this.exhaustionLevel + f, 40.0F);
}
public float e() {
return this.saturationLevel;
}
}