package net.minecraft.src;
public class FoodStats
{
/** The player's food level. */
private int foodLevel = 20;
/** The player's food saturation. */
private float foodSaturationLevel = 5.0F;
/** The player's food exhaustion. */
private float foodExhaustionLevel;
/** The player's food timer value. */
private int foodTimer = 0;
private int prevFoodLevel = 20;
/**
* Args: int foodLevel, float foodSaturationModifier
*/
public void addStats(int par1, float par2)
{
this.foodLevel = Math.min(par1 + this.foodLevel, 20);
this.foodSaturationLevel = Math.min(this.foodSaturationLevel + (float)par1 * par2 * 2.0F, (float)this.foodLevel);
}
/**
* Eat some food.
*/
public void addStats(ItemFood par1ItemFood)
{
this.addStats(par1ItemFood.getHealAmount(), par1ItemFood.getSaturationModifier());
}
/**
* Handles the food game logic.
*/
public void onUpdate(EntityPlayer par1EntityPlayer)
{
int var2 = par1EntityPlayer.worldObj.difficultySetting;
this.prevFoodLevel = this.foodLevel;
if (this.foodExhaustionLevel > 4.0F)
{
this.foodExhaustionLevel -= 4.0F;
if (this.foodSaturationLevel > 0.0F)
{
this.foodSaturationLevel = Math.max(this.foodSaturationLevel - 1.0F, 0.0F);
}
else if (var2 > 0)
{
this.foodLevel = Math.max(this.foodLevel - 1, 0);
}
}
if (this.foodLevel >= 18 && par1EntityPlayer.shouldHeal())
{
++this.foodTimer;
if (this.foodTimer >= 80)
{
par1EntityPlayer.heal(1);
this.foodTimer = 0;
}
}
else if (this.foodLevel <= 0)
{
++this.foodTimer;
if (this.foodTimer >= 80)
{
if (par1EntityPlayer.getHealth() > 10 || var2 >= 3 || par1EntityPlayer.getHealth() > 1 && var2 >= 2)
{
par1EntityPlayer.attackEntityFrom(DamageSource.starve, 1);
}
this.foodTimer = 0;
}
}
else
{
this.foodTimer = 0;
}
}
/**
* Reads food stats from an NBT object.
*/
public void readNBT(NBTTagCompound par1NBTTagCompound)
{
if (par1NBTTagCompound.hasKey("foodLevel"))
{
this.foodLevel = par1NBTTagCompound.getInteger("foodLevel");
this.foodTimer = par1NBTTagCompound.getInteger("foodTickTimer");
this.foodSaturationLevel = par1NBTTagCompound.getFloat("foodSaturationLevel");
this.foodExhaustionLevel = par1NBTTagCompound.getFloat("foodExhaustionLevel");
}
}
/**
* Writes food stats to an NBT object.
*/
public void writeNBT(NBTTagCompound par1NBTTagCompound)
{
par1NBTTagCompound.setInteger("foodLevel", this.foodLevel);
par1NBTTagCompound.setInteger("foodTickTimer", this.foodTimer);
par1NBTTagCompound.setFloat("foodSaturationLevel", this.foodSaturationLevel);
par1NBTTagCompound.setFloat("foodExhaustionLevel", this.foodExhaustionLevel);
}
/**
* Get the player's food level.
*/
public int getFoodLevel()
{
return this.foodLevel;
}
public int getPrevFoodLevel()
{
return this.prevFoodLevel;
}
/**
* If foodLevel is not max.
*/
public boolean needFood()
{
return this.foodLevel < 20;
}
/**
* adds input to foodExhaustionLevel to a max of 40
*/
public void addExhaustion(float par1)
{
this.foodExhaustionLevel = Math.min(this.foodExhaustionLevel + par1, 40.0F);
}
/**
* Get the player's food saturation level.
*/
public float getSaturationLevel()
{
return this.foodSaturationLevel;
}
public void setFoodLevel(int par1)
{
this.foodLevel = par1;
}
public void setFoodSaturationLevel(float par1)
{
this.foodSaturationLevel = par1;
}
}
public int getFoodLevel()
{
return 10;
}
В апдейте приравнивай переменную голода к 15.
:m_faceplam:Чтобы восполняло хп вместо голода: хук в ItemFood#onItemUseFinish и прибавляй хп.
package net.minecraft.src;
public class ItemFood extends Item
{
/** Number of ticks to run while 'EnumAction'ing until result. */
public final int itemUseDuration;
/** The amount this food item heals the player. */
private final int healAmount;
private final float saturationModifier;
/** Whether wolves like this food (true for raw and cooked porkchop). */
private final boolean isWolfsFavoriteMeat;
/**
* If this field is true, the food can be consumed even if the player don't need to eat.
*/
private boolean alwaysEdible;
/**
* represents the potion effect that will occurr upon eating this food. Set by setPotionEffect
*/
private int potionId;
private int treatment;
/** set by setPotionEffect */
private int potionDuration;
/** set by setPotionEffect */
private int potionAmplifier;
/** probably of the set potion effect occurring */
private float potionEffectProbability;
public ItemFood(int par1, int par2, float par3, boolean par4, int par5)
{
super(par1);
this.itemUseDuration = par5;
this.healAmount = par2;
this.isWolfsFavoriteMeat = par4;
this.saturationModifier = par3;
this.setCreativeTab(CreativeTabs.tabFood);
}
public ItemFood(int par1, int par2, boolean par3)
{
this(par1, par2, 0.6F, par3, par2);
}
public ItemStack onEaten(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer)
{
--par1ItemStack.stackSize;
par3EntityPlayer.getFoodStats().addStats(this);
par2World.playSoundAtEntity(par3EntityPlayer, "random.burp", 0.5F, par2World.rand.nextFloat() * 0.1F + 0.9F);
this.onFoodEaten(par1ItemStack, par2World, par3EntityPlayer);
return par1ItemStack;
}
protected void onFoodEaten(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer)
{
if (!par2World.isRemote && this.potionId > 0 && par2World.rand.nextFloat() < this.potionEffectProbability)
{
par3EntityPlayer.addPotionEffect(new PotionEffect(this.potionId, this.potionDuration * 20, this.potionAmplifier));
}
}
/**
* How long it takes to use or consume an item
*/
public int getMaxItemUseDuration(ItemStack par1ItemStack)
{
return 32;
}
/**
* returns the action that specifies what animation to play when the items is being used
*/
public EnumAction getItemUseAction(ItemStack par1ItemStack)
{
return EnumAction.eat;
}
/**
* Called whenever this item is equipped and the right mouse button is pressed. Args: itemStack, world, entityPlayer
*/
public ItemStack onItemRightClick(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer)
{
if (par3EntityPlayer.canEat(this.alwaysEdible))
{
par3EntityPlayer.setItemInUse(par1ItemStack, this.getMaxItemUseDuration(par1ItemStack));
}
return par1ItemStack;
}
public int getHealAmount()
{
return this.healAmount;
}
/**
* gets the saturationModifier of the ItemFood
*/
public float getSaturationModifier()
{
return this.saturationModifier;
}
/**
* Whether wolves like this food (true for raw and cooked porkchop).
*/
public boolean isWolfsFavoriteMeat()
{
return this.isWolfsFavoriteMeat;
}
/**
* sets a potion effect on the item. Args: int potionId, int duration (will be multiplied by 20), int amplifier,
* float probability of effect happening
*/
public ItemFood setPotionEffect(int par1, int par2, int par3, float par4)
{
this.potionId = par1;
this.potionDuration = par2;
this.potionAmplifier = par3;
this.potionEffectProbability = par4;
return this;
}
/**
* Set the field 'alwaysEdible' to true, and make the food edible even if the player don't need to eat.
*/
public ItemFood setAlwaysEdible()
{
this.alwaysEdible = true;
return this;
}
}
public ItemStack onEaten(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer) { --par1ItemStack.stackSize; par3EntityPlayer.getFoodStats().addStats(this); par2World.playSoundAtEntity(par3EntityPlayer, "random.burp", 0.5F, par2World.rand.nextFloat() * 0.1F + 0.9F); this.onFoodEaten(par1ItemStack, par2World, par3EntityPlayer); return par1ItemStack; }
par3EntityPlayer.getFoodStats().addStats(this);
player.healприбавляй хп