From 3ff8b2d60f171e175cbb2a790547d576fd590482 Mon Sep 17 00:00:00 2001 From: Bernhard Bonigl Date: Sun, 5 Jun 2016 17:26:41 +0200 Subject: [PATCH] Show potion effects when eaten on item edible --- .../slimeknights/mantle/item/ItemEdible.java | 58 ++++++++++++++++--- 1 file changed, 51 insertions(+), 7 deletions(-) diff --git a/src/main/java/slimeknights/mantle/item/ItemEdible.java b/src/main/java/slimeknights/mantle/item/ItemEdible.java index 8cdd3a1..59877d4 100644 --- a/src/main/java/slimeknights/mantle/item/ItemEdible.java +++ b/src/main/java/slimeknights/mantle/item/ItemEdible.java @@ -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 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(); + 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 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 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(EnumActionResult.SUCCESS, itemStackIn); + } + else + { + return new ActionResult(EnumActionResult.FAIL, itemStackIn); + } + } + }