Paper/src/main/java/net/minecraft/server/FoodMetaData.java

117 Zeilen
4.6 KiB
Java

package net.minecraft.server;
public class FoodMetaData {
2013-03-25 05:22:32 +01:00
// CraftBukkit start - All made public
2011-09-15 18:36:27 +02:00
public int foodLevel = 20;
public float saturationLevel = 5.0F;
public float exhaustionLevel;
2013-07-01 13:03:00 +02:00
public int foodTickTimer;
2013-11-04 14:07:38 +01:00
private EntityHuman entityhuman;
// CraftBukkit end
private int e = 20;
2013-11-04 14:07:38 +01:00
public FoodMetaData() { throw new AssertionError("Whoopsie, we missed the bukkit."); } // CraftBukkit start - throw an error
// CraftBukkit start - added EntityHuman constructor
public FoodMetaData(EntityHuman entityhuman) {
org.apache.commons.lang.Validate.notNull(entityhuman);
this.entityhuman = entityhuman;
}
// CraftBukkit end
public void eat(int i, float f) {
2011-09-15 18:36:27 +02:00
this.foodLevel = Math.min(i + this.foodLevel, 20);
this.saturationLevel = Math.min(this.saturationLevel + (float) i * f * 2.0F, (float) this.foodLevel);
}
2013-11-04 14:07:38 +01:00
public void a(ItemFood itemfood, ItemStack itemstack) {
// CraftBukkit start
int oldFoodLevel = foodLevel;
org.bukkit.event.entity.FoodLevelChangeEvent event = org.bukkit.craftbukkit.event.CraftEventFactory.callFoodLevelChangeEvent(entityhuman, itemfood.getNutrition(itemstack) + oldFoodLevel);
if (!event.isCancelled()) {
this.eat(event.getFoodLevel() - oldFoodLevel, itemfood.getSaturationModifier(itemstack));
}
((EntityPlayer) entityhuman).playerConnection.sendPacket(new PacketPlayOutUpdateHealth(((EntityPlayer) entityhuman).getBukkitEntity().getScaledHealth(), entityhuman.getFoodData().foodLevel, entityhuman.getFoodData().saturationLevel));
// CraftBukkit end
}
public void a(EntityHuman entityhuman) {
2013-11-04 14:07:38 +01:00
EnumDifficulty enumdifficulty = entityhuman.world.difficulty;
2011-09-15 18:36:27 +02:00
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);
2013-11-04 14:07:38 +01:00
} else if (enumdifficulty != EnumDifficulty.PEACEFUL) {
2011-09-22 00:54:31 +02:00
// CraftBukkit start
org.bukkit.event.entity.FoodLevelChangeEvent event = org.bukkit.craftbukkit.event.CraftEventFactory.callFoodLevelChangeEvent(entityhuman, Math.max(this.foodLevel - 1, 0));
2011-09-22 00:54:31 +02:00
if (!event.isCancelled()) {
this.foodLevel = event.getFoodLevel();
}
2013-11-04 14:07:38 +01:00
((EntityPlayer) entityhuman).playerConnection.sendPacket(new PacketPlayOutUpdateHealth(((EntityPlayer) entityhuman).getBukkitEntity().getScaledHealth(), this.foodLevel, this.saturationLevel));
2011-09-22 00:54:31 +02:00
// CraftBukkit end
}
}
2013-11-04 14:07:38 +01:00
if (entityhuman.world.getGameRules().getBoolean("naturalRegeneration") && this.foodLevel >= 18 && entityhuman.bP()) {
2011-09-15 18:36:27 +02:00
++this.foodTickTimer;
if (this.foodTickTimer >= 80) {
2012-07-29 09:33:13 +02:00
// CraftBukkit - added RegainReason
2013-07-01 13:03:00 +02:00
entityhuman.heal(1.0F, org.bukkit.event.entity.EntityRegainHealthEvent.RegainReason.SATIATED);
2013-11-04 14:07:38 +01:00
this.a(3.0F);
2011-09-15 18:36:27 +02:00
this.foodTickTimer = 0;
}
2011-09-15 18:36:27 +02:00
} else if (this.foodLevel <= 0) {
++this.foodTickTimer;
if (this.foodTickTimer >= 80) {
2013-11-04 14:07:38 +01:00
if (entityhuman.getHealth() > 10.0F || enumdifficulty == EnumDifficulty.HARD || entityhuman.getHealth() > 1.0F && enumdifficulty == EnumDifficulty.NORMAL) {
2013-07-01 13:03:00 +02:00
entityhuman.damageEntity(DamageSource.STARVE, 1.0F);
}
2011-09-15 18:36:27 +02:00
this.foodTickTimer = 0;
}
} else {
2011-09-15 18:36:27 +02:00
this.foodTickTimer = 0;
}
}
public void a(NBTTagCompound nbttagcompound) {
2013-11-04 14:07:38 +01:00
if (nbttagcompound.hasKeyOfType("foodLevel", 99)) {
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() {
2011-09-15 18:36:27 +02:00
return this.foodLevel;
}
2012-07-29 09:33:13 +02:00
public boolean c() {
2011-09-15 18:36:27 +02:00
return this.foodLevel < 20;
}
public void a(float f) {
2011-09-15 18:36:27 +02:00
this.exhaustionLevel = Math.min(this.exhaustionLevel + f, 40.0F);
}
2012-07-29 09:33:13 +02:00
public float e() {
2011-09-15 18:36:27 +02:00
return this.saturationLevel;
}
2011-09-15 18:36:27 +02:00
}