| 1 | package net.minecraft.item;
|
| 2 |
|
| 3 | import net.minecraft.creativetab.CreativeTabs;
|
| 4 | import net.minecraft.entity.player.EntityPlayer;
|
| 5 | import net.minecraft.potion.PotionEffect;
|
| 6 | import net.minecraft.stats.StatList;
|
| 7 | import net.minecraft.world.World;
|
| 8 |
|
| 9 | public class ItemFood extends Item
|
| 10 | {
|
| 11 | /** Number of ticks to run while 'EnumAction'ing until result. */
|
| 12 | public final int itemUseDuration;
|
| 13 |
|
| 14 | /** The amount this food item heals the player. */
|
| 15 | private final int healAmount;
|
| 16 | private final float saturationModifier;
|
| 17 |
|
| 18 | /** Whether wolves like this food (true for raw and cooked porkchop). */
|
| 19 | private final boolean isWolfsFavoriteMeat;
|
| 20 |
|
| 21 | /**
|
| 22 | * If this field is true, the food can be consumed even if the player don't need to eat.
|
| 23 | */
|
| 24 | private boolean alwaysEdible;
|
| 25 |
|
| 26 | /**
|
| 27 | * represents the potion effect that will occurr upon eating this food. Set by setPotionEffect
|
| 28 | */
|
| 29 | private int potionId;
|
| 30 |
|
| 31 | /** set by setPotionEffect */
|
| 32 | private int potionDuration;
|
| 33 |
|
| 34 | /** set by setPotionEffect */
|
| 35 | private int potionAmplifier;
|
| 36 |
|
| 37 | /** probably of the set potion effect occurring */
|
| 38 | private float potionEffectProbability;
|
| 39 |
|
| 40 | public ItemFood(int amount, float saturation, boolean isWolfFood)
|
| 41 | {
|
| 42 | this.itemUseDuration = 32;
|
| 43 | this.healAmount = amount;
|
| 44 | this.isWolfsFavoriteMeat = isWolfFood;
|
| 45 | this.saturationModifier = saturation;
|
| 46 | this.setCreativeTab(CreativeTabs.tabFood);
|
| 47 | }
|
| 48 |
|
| 49 | public ItemFood(int amount, boolean isWolfFood)
|
| 50 | {
|
| 51 | this(amount, 0.6F, isWolfFood);
|
| 52 | }
|
| 53 |
|
| 54 | /**
|
| 55 | * Called when the player finishes using this Item (E.g. finishes eating.). Not called when the player stops using
|
| 56 | * the Item before the action is complete.
|
| 57 | */
|
| 58 | public ItemStack onItemUseFinish(ItemStack stack, World worldIn, EntityPlayer playerIn)
|
| 59 | {
|
| 60 | --stack.stackSize;
|
| 61 | playerIn.getFoodStats().addStats(this, stack);
|
| 62 | worldIn.playSoundAtEntity(playerIn, "random.burp", 0.5F, worldIn.rand.nextFloat() * 0.1F + 0.9F);
|
| 63 | this.onFoodEaten(stack, worldIn, playerIn);
|
| 64 | playerIn.triggerAchievement(StatList.objectUseStats[Item.getIdFromItem(this)]);
|
| 65 | return stack;
|
| 66 | }
|
| 67 |
|
| 68 | protected void onFoodEaten(ItemStack stack, World worldIn, EntityPlayer player)
|
| 69 | {
|
| 70 | if (!worldIn.isRemote && this.potionId > 0 && worldIn.rand.nextFloat() < this.potionEffectProbability)
|
| 71 | {
|
| 72 | player.addPotionEffect(new PotionEffect(this.potionId, this.potionDuration * 20, this.potionAmplifier));
|
| 73 | }
|
| 74 | }
|
| 75 |
|
| 76 | /**
|
| 77 | * How long it takes to use or consume an item
|
| 78 | */
|
| 79 | public int getMaxItemUseDuration(ItemStack stack)
|
| 80 | {
|
| 81 | return 32;
|
| 82 | }
|
| 83 |
|
| 84 | /**
|
| 85 | * returns the action that specifies what animation to play when the items is being used
|
| 86 | */
|
| 87 | public EnumAction getItemUseAction(ItemStack stack)
|
| 88 | {
|
| 89 | return EnumAction.EAT;
|
| 90 | }
|
| 91 |
|
| 92 | /**
|
| 93 | * Called whenever this item is equipped and the right mouse button is pressed. Args: itemStack, world, entityPlayer
|
| 94 | */
|
| 95 | public ItemStack onItemRightClick(ItemStack itemStackIn, World worldIn, EntityPlayer playerIn)
|
| 96 | {
|
| 97 | if (playerIn.canEat(this.alwaysEdible))
|
| 98 | {
|
| 99 | playerIn.setItemInUse(itemStackIn, this.getMaxItemUseDuration(itemStackIn));
|
| 100 | }
|
| 101 |
|
| 102 | return itemStackIn;
|
| 103 | }
|
| 104 |
|
| 105 | public int getHealAmount(ItemStack stack)
|
| 106 | {
|
| 107 | return this.healAmount;
|
| 108 | }
|
| 109 |
|
| 110 | public float getSaturationModifier(ItemStack stack)
|
| 111 | {
|
| 112 | return this.saturationModifier;
|
| 113 | }
|
| 114 |
|
| 115 | /**
|
| 116 | * Whether wolves like this food (true for raw and cooked porkchop).
|
| 117 | */
|
| 118 | public boolean isWolfsFavoriteMeat()
|
| 119 | {
|
| 120 | return this.isWolfsFavoriteMeat;
|
| 121 | }
|
| 122 |
|
| 123 | /**
|
| 124 | * sets a potion effect on the item. Args: int potionId, int duration (will be multiplied by 20), int amplifier,
|
| 125 | * float probability of effect happening
|
| 126 | */
|
| 127 | public ItemFood setPotionEffect(int id, int duration, int amplifier, float probability)
|
| 128 | {
|
| 129 | this.potionId = id;
|
| 130 | this.potionDuration = duration;
|
| 131 | this.potionAmplifier = amplifier;
|
| 132 | this.potionEffectProbability = probability;
|
| 133 | return this;
|
| 134 | }
|
| 135 |
|
| 136 | /**
|
| 137 | * Set the field 'alwaysEdible' to true, and make the food edible even if the player don't need to eat.
|
| 138 | */
|
| 139 | public ItemFood setAlwaysEdible()
|
| 140 | {
|
| 141 | this.alwaysEdible = true;
|
| 142 | return this;
|
| 143 | }
|
| 144 | }
|
| 145 |
|