February 13, 201312 yr Alternatively, when initializing the food, you can use .setPotionEffect(int potionID, int durationInSeconds, int amplifier, int chance (1.0F being 100%)) This is actually what the rawChicken and rottenFlesh items in net.minecraft.item.Item do, since there isn't a class extended from ItemFood for them: public static Item chickenRaw = (new ItemFood(109, 2, 0.3F, true)).setPotionEffect(Potion.hunger.id, 30, 0, 0.3F).setIconCoord(9, 7).setItemName("chickenRaw"); public static Item rottenFlesh = (new ItemFood(111, 4, 0.1F, true)).setPotionEffect(Potion.hunger.id, 30, 0, 0.8F).setIconCoord(11, 5).setItemName("rottenFlesh"); Both effects last 30 seconds, and Raw Chicken has a 30% chance of the effect kicking in while Rotten Flesh has an 80% chance of this happening. This is much simpler than using onFoodEaten, but that method allows you to have multiple potion effects occur at once, so I'll go over that as well. Concerning ItemFood's onFoodEaten method, the example used within the code is actually handled within a method that onFoodEaten calls and extended classes can override: public ItemStack onFoodEaten(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.func_77849_c(par1ItemStack, par2World, par3EntityPlayer); return par1ItemStack; } As for the method itself, also found in ItemFood: protected void func_77849_c(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)); } } The method name is currently obfuscated, but can be seen overridden in examples like ItemAppleGold: protected void func_77849_c(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer) { if (par1ItemStack.getItemDamage() > 0) { if (!par2World.isRemote) { par3EntityPlayer.addPotionEffect(new PotionEffect(Potion.regeneration.id, 600, 3)); par3EntityPlayer.addPotionEffect(new PotionEffect(Potion.resistance.id, 6000, 0)); par3EntityPlayer.addPotionEffect(new PotionEffect(Potion.fireResistance.id, 6000, 0)); } } else { super.func_77849_c(par1ItemStack, par2World, par3EntityPlayer); } } This example allows for the Enchanted Golden Apple (checked via the "if (par1ItemStack.getItemDamage() > 0)" statement) to provide 30 seconds of Regeneration IV, 5 minutes of Resistance, and 5 minutes of Fire Resistance. Divide all values by 20 for parameter #2 of PotionEffect to figure out how many seconds an effect lasts doing this. This is different than how the setPotionEffect method works when initializing an item, so be careful not to mix this up! There are pros and cons to using either approach, use the one that better suits what you want to do.
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.