Show potion effects when eaten on item edible

This commit is contained in:
Bernhard Bonigl committed 2016-06-05 17:26:41 +02:00
1 parent 69ffb6314e
commit 3ff8b2d60f
1 file changed
+51 -7
@@ -1,5 +1,7 @@
package slimeknights.mantle.item;
import com.google.common.collect.ImmutableList;
import gnu.trove.map.hash.TIntFloatHashMap;
import gnu.trove.map.hash.TIntIntHashMap;
import gnu.trove.map.hash.TIntObjectHashMap;
@@ -10,10 +12,16 @@ import net.minecraft.item.Item;
import net.minecraft.item.ItemFood;
import net.minecraft.item.ItemStack;
import net.minecraft.potion.PotionEffect;
import net.minecraft.potion.PotionUtils;
import net.minecraft.util.ActionResult;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumHand;
import net.minecraft.util.text.translation.I18n;
import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import java.util.BitSet;
import java.util.List;
import javax.annotation.Nonnull;
@@ -26,6 +34,9 @@ public class ItemEdible extends ItemFood {
protected TIntIntHashMap foodLevels;
protected TIntFloatHashMap saturations;
protected TIntObjectHashMap<PotionEffect[]> potionEffects;
protected BitSet alwaysEdible;
public boolean displayEffectsTooltip; // set to false to not display effects of food in tooltip
public ItemEdible() {
super(0, 0, false);
@@ -35,23 +46,32 @@ public class ItemEdible extends ItemFood {
foodLevels = new TIntIntHashMap();
saturations = new TIntFloatHashMap();
potionEffects = new TIntObjectHashMap<PotionEffect[]>();
alwaysEdible = new BitSet();
displayEffectsTooltip = true;
}
public ItemStack addFood(int meta, int food, float saturation, String name, PotionEffect... effects) {
return addFood(meta, food, saturation, name, effects.length > 0, effects);
}
/**
* Add a new food type!
*
* @param meta Metadata to use, has to be free
* @param food How much food it restores on eating
* @param saturation Saturation multiplier on the food
* @param name Unlocalized postfix
* @param effects PotionEffects that will be applied on eating. The PotionEffect passed will be directly applied
* @return Itemstack containing the registered item
* @param meta Metadata to use, has to be free
* @param food How much food it restores on eating
* @param saturation Saturation multiplier on the food
* @param name Unlocalized postfix
* @param alwaysEdible Item is always edible even when hunger is full
* @param effects PotionEffects that will be applied on eating. The PotionEffect passed will be directly applied
* @return Itemstack containing the registered item
*/
public ItemStack addFood(int meta, int food, float saturation, String name, PotionEffect... effects) {
public ItemStack addFood(int meta, int food, float saturation, String name, boolean alwaysEdible, PotionEffect... effects) {
dynamic.addMeta(meta, name);
foodLevels.put(meta, food);
saturations.put(meta, saturation);
potionEffects.put(meta, effects);
this.alwaysEdible.set(meta, alwaysEdible);
return new ItemStack(this, 1, meta);
}
@@ -100,6 +120,13 @@ public class ItemEdible extends ItemFood {
@Override
public void addInformation(ItemStack stack, EntityPlayer playerIn, List<String> tooltip, boolean advanced) {
dynamic.addInformation(stack, playerIn, tooltip, advanced);
// effect info
if(displayEffectsTooltip) {
for(PotionEffect potionEffect : potionEffects.get(stack.getMetadata())) {
tooltip.add(I18n.translateToLocal(potionEffect.getEffectName()).trim());
}
}
}
@SideOnly(Side.CLIENT)
@@ -112,4 +139,21 @@ public class ItemEdible extends ItemFood {
public void registerItemModels() {
dynamic.registerItemModels(this);
}
@Nonnull
@Override
public ActionResult<ItemStack> onItemRightClick(@Nonnull ItemStack itemStackIn, World worldIn, EntityPlayer playerIn, @Nonnull EnumHand hand)
{
int meta = itemStackIn.getMetadata();
if(dynamic.isValid(meta) && playerIn.canEat(this.alwaysEdible.get(itemStackIn.getMetadata())))
{
playerIn.setActiveHand(hand);
return new ActionResult<ItemStack>(EnumActionResult.SUCCESS, itemStackIn);
}
else
{
return new ActionResult<ItemStack>(EnumActionResult.FAIL, itemStackIn);
}
}
}