001 package net.minecraft.item; 002 003 import net.minecraft.creativetab.CreativeTabs; 004 import net.minecraft.entity.player.EntityPlayer; 005 import net.minecraft.potion.PotionEffect; 006 import net.minecraft.world.World; 007 008 public class ItemFood extends Item 009 { 010 /** Number of ticks to run while 'EnumAction'ing until result. */ 011 public final int itemUseDuration; 012 013 /** The amount this food item heals the player. */ 014 private final int healAmount; 015 private final float saturationModifier; 016 017 /** Whether wolves like this food (true for raw and cooked porkchop). */ 018 private final boolean isWolfsFavoriteMeat; 019 020 /** 021 * If this field is true, the food can be consumed even if the player don't need to eat. 022 */ 023 private boolean alwaysEdible; 024 025 /** 026 * represents the potion effect that will occurr upon eating this food. Set by setPotionEffect 027 */ 028 private int potionId; 029 030 /** set by setPotionEffect */ 031 private int potionDuration; 032 033 /** set by setPotionEffect */ 034 private int potionAmplifier; 035 036 /** probably of the set potion effect occurring */ 037 private float potionEffectProbability; 038 039 public ItemFood(int par1, int par2, float par3, boolean par4) 040 { 041 super(par1); 042 this.itemUseDuration = 32; 043 this.healAmount = par2; 044 this.isWolfsFavoriteMeat = par4; 045 this.saturationModifier = par3; 046 this.setCreativeTab(CreativeTabs.tabFood); 047 } 048 049 public ItemFood(int par1, int par2, boolean par3) 050 { 051 this(par1, par2, 0.6F, par3); 052 } 053 054 public ItemStack onFoodEaten(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer) 055 { 056 --par1ItemStack.stackSize; 057 par3EntityPlayer.getFoodStats().addStats(this); 058 par2World.playSoundAtEntity(par3EntityPlayer, "random.burp", 0.5F, par2World.rand.nextFloat() * 0.1F + 0.9F); 059 this.func_77849_c(par1ItemStack, par2World, par3EntityPlayer); 060 return par1ItemStack; 061 } 062 063 protected void func_77849_c(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer) 064 { 065 if (!par2World.isRemote && this.potionId > 0 && par2World.rand.nextFloat() < this.potionEffectProbability) 066 { 067 par3EntityPlayer.addPotionEffect(new PotionEffect(this.potionId, this.potionDuration * 20, this.potionAmplifier)); 068 } 069 } 070 071 /** 072 * How long it takes to use or consume an item 073 */ 074 public int getMaxItemUseDuration(ItemStack par1ItemStack) 075 { 076 return 32; 077 } 078 079 /** 080 * returns the action that specifies what animation to play when the items is being used 081 */ 082 public EnumAction getItemUseAction(ItemStack par1ItemStack) 083 { 084 return EnumAction.eat; 085 } 086 087 /** 088 * Called whenever this item is equipped and the right mouse button is pressed. Args: itemStack, world, entityPlayer 089 */ 090 public ItemStack onItemRightClick(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer) 091 { 092 if (par3EntityPlayer.canEat(this.alwaysEdible)) 093 { 094 par3EntityPlayer.setItemInUse(par1ItemStack, this.getMaxItemUseDuration(par1ItemStack)); 095 } 096 097 return par1ItemStack; 098 } 099 100 public int getHealAmount() 101 { 102 return this.healAmount; 103 } 104 105 /** 106 * gets the saturationModifier of the ItemFood 107 */ 108 public float getSaturationModifier() 109 { 110 return this.saturationModifier; 111 } 112 113 /** 114 * Whether wolves like this food (true for raw and cooked porkchop). 115 */ 116 public boolean isWolfsFavoriteMeat() 117 { 118 return this.isWolfsFavoriteMeat; 119 } 120 121 /** 122 * sets a potion effect on the item. Args: int potionId, int duration (will be multiplied by 20), int amplifier, 123 * float probability of effect happening 124 */ 125 public ItemFood setPotionEffect(int par1, int par2, int par3, float par4) 126 { 127 this.potionId = par1; 128 this.potionDuration = par2; 129 this.potionAmplifier = par3; 130 this.potionEffectProbability = par4; 131 return this; 132 } 133 134 /** 135 * Set the field 'alwaysEdible' to true, and make the food edible even if the player don't need to eat. 136 */ 137 public ItemFood setAlwaysEdible() 138 { 139 this.alwaysEdible = true; 140 return this; 141 } 142 }