From 71b518702d3c8d30ab756b00be4bd5251c99c5cd Mon Sep 17 00:00:00 2001 From: Sollace Date: Tue, 29 Jan 2019 13:53:43 +0200 Subject: [PATCH] All major flowers can now be eaten by the player --- .../com/minelittlepony/unicopia/UItems.java | 24 +++++ .../edibles/BushToxicityDeterminent.java | 47 +++++++++ .../edibles/FlowerToxicityDeterminent.java | 52 ++++++++++ .../unicopia/edibles/IEdible.java | 16 ++++ .../unicopia/edibles/ItemEdible.java | 95 +++++++++++++++++++ .../unicopia/edibles/Toxicity.java | 35 +++++++ .../unicopia/edibles/UItemFoodDelegate.java | 58 +++++++++++ 7 files changed, 327 insertions(+) create mode 100644 src/main/java/com/minelittlepony/unicopia/edibles/BushToxicityDeterminent.java create mode 100644 src/main/java/com/minelittlepony/unicopia/edibles/FlowerToxicityDeterminent.java create mode 100644 src/main/java/com/minelittlepony/unicopia/edibles/IEdible.java create mode 100644 src/main/java/com/minelittlepony/unicopia/edibles/ItemEdible.java create mode 100644 src/main/java/com/minelittlepony/unicopia/edibles/Toxicity.java create mode 100644 src/main/java/com/minelittlepony/unicopia/edibles/UItemFoodDelegate.java diff --git a/src/main/java/com/minelittlepony/unicopia/UItems.java b/src/main/java/com/minelittlepony/unicopia/UItems.java index 7d982bd0..661b426f 100644 --- a/src/main/java/com/minelittlepony/unicopia/UItems.java +++ b/src/main/java/com/minelittlepony/unicopia/UItems.java @@ -19,6 +19,8 @@ import com.minelittlepony.unicopia.item.UItemDecoration; import com.minelittlepony.unicopia.item.UItemSlab; import com.minelittlepony.unicopia.spell.SpellRegistry; +import net.minecraft.block.BlockDoublePlant; +import net.minecraft.block.BlockFlower; import net.minecraft.client.Minecraft; import net.minecraft.client.renderer.block.model.ModelResourceLocation; import net.minecraft.client.renderer.color.ItemColors; @@ -42,6 +44,10 @@ import net.minecraftforge.registries.IForgeRegistry; import static com.minelittlepony.unicopia.Predicates.*; +import com.minelittlepony.unicopia.edibles.BushToxicityDeterminent; +import com.minelittlepony.unicopia.edibles.FlowerToxicityDeterminent; +import com.minelittlepony.unicopia.edibles.ItemEdible; +import com.minelittlepony.unicopia.edibles.UItemFoodDelegate; import com.minelittlepony.unicopia.forgebullshit.BuildInTexturesBakery; import com.minelittlepony.unicopia.forgebullshit.RegistryLockSpinner; @@ -119,11 +125,29 @@ public class UItems { public static final Item apple_leaves = new ItemFruitLeaves(UBlocks.apple_leaves, Unicopia.MODID, "apple_leaves"); + public static final Item double_plant = new UItemFoodDelegate(Blocks.DOUBLE_PLANT, stack -> + BlockDoublePlant.EnumPlantType.byMetadata(stack.getMetadata()).getTranslationKey() + ).setFoodDelegate(new ItemEdible(new BushToxicityDeterminent())) + .setTranslationKey("doublePlant"); + + public static final Item yellow_flower = new UItemFoodDelegate(Blocks.YELLOW_FLOWER, stack -> + BlockFlower.EnumFlowerType.getType(BlockFlower.EnumFlowerColor.YELLOW, stack.getMetadata()).getTranslationKey() + ).setFoodDelegate(new ItemEdible(new FlowerToxicityDeterminent(BlockFlower.EnumFlowerColor.YELLOW))) + .setTranslationKey("flower"); + + public static final Item red_flower = new UItemFoodDelegate(Blocks.RED_FLOWER, stack -> + BlockFlower.EnumFlowerType.getType(BlockFlower.EnumFlowerColor.RED, stack.getMetadata()).getTranslationKey() + ).setFoodDelegate(new ItemEdible(new FlowerToxicityDeterminent(BlockFlower.EnumFlowerColor.RED))) + .setTranslationKey("rose"); + static void registerItems(IForgeRegistry registry) { RegistryLockSpinner.unlock(Item.REGISTRY); RegistryLockSpinner.commit(Item.REGISTRY, Items.APPLE, apple, Items.class); RegistryLockSpinner.commit(Item.REGISTRY, Items.STICK, stick, Items.class); + RegistryLockSpinner.commit(Item.REGISTRY, Item.getItemFromBlock(Blocks.DOUBLE_PLANT), double_plant, Items.class); + RegistryLockSpinner.commit(Item.REGISTRY, Item.getItemFromBlock(Blocks.YELLOW_FLOWER), yellow_flower, Items.class); + RegistryLockSpinner.commit(Item.REGISTRY, Item.getItemFromBlock(Blocks.RED_FLOWER), red_flower, Items.class); RegistryLockSpinner.lock(Item.REGISTRY); diff --git a/src/main/java/com/minelittlepony/unicopia/edibles/BushToxicityDeterminent.java b/src/main/java/com/minelittlepony/unicopia/edibles/BushToxicityDeterminent.java new file mode 100644 index 00000000..56c064a9 --- /dev/null +++ b/src/main/java/com/minelittlepony/unicopia/edibles/BushToxicityDeterminent.java @@ -0,0 +1,47 @@ +package com.minelittlepony.unicopia.edibles; + +import net.minecraft.block.BlockDoublePlant; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.init.MobEffects; +import net.minecraft.item.ItemStack; +import net.minecraft.potion.PotionEffect; + +import static net.minecraft.block.BlockDoublePlant.EnumPlantType.*; + +public class BushToxicityDeterminent implements IEdible { + + BlockDoublePlant.EnumPlantType getType(ItemStack stack) { + return byMetadata(stack.getMetadata()); + } + + @Override + public Toxicity getToxicityLevel(ItemStack stack) { + switch (getType(stack)) { + case SUNFLOWER: + case GRASS: return Toxicity.SAFE; + case PAEONIA: + case SYRINGA: return Toxicity.FAIR; + case FERN: + case ROSE: return Toxicity.SEVERE; + default: return Toxicity.SAFE; + } + } + + @Override + public void addSecondaryEffects(EntityPlayer player, Toxicity toxicity, ItemStack stack) { + BlockDoublePlant.EnumPlantType type = getType(stack); + + if ((type == ROSE || type == FERN) + && player.world.rand.nextInt(30) == 0) { + player.addPotionEffect(new PotionEffect(MobEffects.INSTANT_DAMAGE, 1, 1)); + } + + if (type == GRASS) { + player.addPotionEffect(new PotionEffect(MobEffects.NAUSEA, 30, 1)); + } + + if (type == FERN) { + player.addPotionEffect(new PotionEffect(MobEffects.STRENGTH, 30, 1)); + } + } +} diff --git a/src/main/java/com/minelittlepony/unicopia/edibles/FlowerToxicityDeterminent.java b/src/main/java/com/minelittlepony/unicopia/edibles/FlowerToxicityDeterminent.java new file mode 100644 index 00000000..e191f42a --- /dev/null +++ b/src/main/java/com/minelittlepony/unicopia/edibles/FlowerToxicityDeterminent.java @@ -0,0 +1,52 @@ +package com.minelittlepony.unicopia.edibles; + +import net.minecraft.block.BlockFlower; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.init.MobEffects; +import net.minecraft.item.ItemStack; +import net.minecraft.potion.PotionEffect; + +import static net.minecraft.block.BlockFlower.EnumFlowerType.*; + +public class FlowerToxicityDeterminent implements IEdible { + + private final BlockFlower.EnumFlowerColor color; + + public FlowerToxicityDeterminent(BlockFlower.EnumFlowerColor color) { + this.color = color; + } + + BlockFlower.EnumFlowerType getType(ItemStack stack) { + return BlockFlower.EnumFlowerType.getType(color, stack.getMetadata()); + } + + @Override + public Toxicity getToxicityLevel(ItemStack stack) { + switch (getType(stack)) { + case DANDELION: + case PINK_TULIP: + case RED_TULIP: + case ORANGE_TULIP: + case HOUSTONIA: return Toxicity.SAFE; + case OXEYE_DAISY: + case POPPY: return Toxicity.SEVERE; + case BLUE_ORCHID: + case WHITE_TULIP: + case ALLIUM: return Toxicity.FAIR; + default: return Toxicity.SAFE; + } + } + + @Override + public void addSecondaryEffects(EntityPlayer player, Toxicity toxicity, ItemStack stack) { + BlockFlower.EnumFlowerType type = getType(stack); + + if (type == HOUSTONIA && player.world.rand.nextInt(30) == 0) { + player.addPotionEffect(new PotionEffect(MobEffects.GLOWING, 10, 1)); + } + + if (type == OXEYE_DAISY) { + player.addPotionEffect(new PotionEffect(MobEffects.BLINDNESS, 30, 1)); + } + } +} diff --git a/src/main/java/com/minelittlepony/unicopia/edibles/IEdible.java b/src/main/java/com/minelittlepony/unicopia/edibles/IEdible.java new file mode 100644 index 00000000..48f209e0 --- /dev/null +++ b/src/main/java/com/minelittlepony/unicopia/edibles/IEdible.java @@ -0,0 +1,16 @@ +package com.minelittlepony.unicopia.edibles; + +import javax.annotation.Nonnull; + +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.item.ItemStack; + +@FunctionalInterface +public interface IEdible { + Toxicity getToxicityLevel(ItemStack stack); + + @Nonnull + default void addSecondaryEffects(EntityPlayer player, Toxicity toxicity, ItemStack stack) { + + } +} diff --git a/src/main/java/com/minelittlepony/unicopia/edibles/ItemEdible.java b/src/main/java/com/minelittlepony/unicopia/edibles/ItemEdible.java new file mode 100644 index 00000000..6923ce4d --- /dev/null +++ b/src/main/java/com/minelittlepony/unicopia/edibles/ItemEdible.java @@ -0,0 +1,95 @@ +package com.minelittlepony.unicopia.edibles; + +import javax.annotation.Nonnull; + +import com.minelittlepony.unicopia.Race; +import com.minelittlepony.unicopia.player.PlayerSpeciesList; + +import net.minecraft.advancements.CriteriaTriggers; +import net.minecraft.entity.EntityLivingBase; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.entity.player.EntityPlayerMP; +import net.minecraft.init.MobEffects; +import net.minecraft.init.SoundEvents; +import net.minecraft.item.ItemFood; +import net.minecraft.item.ItemStack; +import net.minecraft.potion.PotionEffect; +import net.minecraft.stats.StatList; +import net.minecraft.util.ActionResult; +import net.minecraft.util.EnumActionResult; +import net.minecraft.util.EnumHand; +import net.minecraft.util.SoundCategory; +import net.minecraft.world.World; + +public class ItemEdible extends ItemFood implements IEdible { + + private final IEdible toxicityDeterminant; + + public ItemEdible(@Nonnull IEdible mapper) { + super(0, 0, false); + + toxicityDeterminant = mapper; + } + + protected void onFoodEaten(ItemStack stack, World worldIn, EntityPlayer player) { + Race race = PlayerSpeciesList.instance().getPlayer(player).getPlayerSpecies(); + Toxicity toxicity = (race.isDefault() || race == Race.CHANGELING) ? Toxicity.LETHAL : getToxicityLevel(stack); + + addSecondaryEffects(player, toxicity, stack); + } + + @Override + public ItemStack onItemUseFinish(ItemStack stack, World worldIn, EntityLivingBase entityLiving) { + if (entityLiving instanceof EntityPlayer) { + EntityPlayer entityplayer = (EntityPlayer)entityLiving; + entityplayer.getFoodStats().addStats(this, stack); + + worldIn.playSound(null, entityplayer.posX, entityplayer.posY, entityplayer.posZ, SoundEvents.ENTITY_PLAYER_BURP, SoundCategory.PLAYERS, 0.5F, worldIn.rand.nextFloat() * 0.1F + 0.9F); + + onFoodEaten(stack, worldIn, entityplayer); + + // replaced "this" with "stack.getItem()" + entityplayer.addStat(StatList.getObjectUseStats(stack.getItem())); + + if (entityplayer instanceof EntityPlayerMP) { + CriteriaTriggers.CONSUME_ITEM.trigger((EntityPlayerMP)entityplayer, stack); + } + } + + stack.shrink(1); + return stack; + } + + @Override + public ActionResult onItemRightClick(World world, EntityPlayer player, EnumHand hand) { + Race race = PlayerSpeciesList.instance().getPlayer(player).getPlayerSpecies(); + + if (race.isDefault() || race == Race.CHANGELING) { + return new ActionResult(EnumActionResult.FAIL, player.getHeldItem(hand)); + } + + return super.onItemRightClick(world, player, hand); + } + + @Override + public void addSecondaryEffects(EntityPlayer player, Toxicity toxicity, ItemStack stack) { + + if (toxicity.toxicWhenRaw()) { + player.addPotionEffect(toxicity.getPoisonEffect()); + } + + if (toxicity.isLethal()) { + player.addPotionEffect(new PotionEffect(MobEffects.NAUSEA, 300, 7)); + player.addPotionEffect(new PotionEffect(MobEffects.WITHER, 300, 7)); + } else if (toxicity.toxicWhenCooked()) { + player.addPotionEffect(new PotionEffect(MobEffects.NAUSEA, 3, 1)); + } + + toxicityDeterminant.addSecondaryEffects(player, toxicity, stack); + } + + @Override + public Toxicity getToxicityLevel(ItemStack stack) { + return toxicityDeterminant.getToxicityLevel(stack); + } +} diff --git a/src/main/java/com/minelittlepony/unicopia/edibles/Toxicity.java b/src/main/java/com/minelittlepony/unicopia/edibles/Toxicity.java new file mode 100644 index 00000000..94f901ad --- /dev/null +++ b/src/main/java/com/minelittlepony/unicopia/edibles/Toxicity.java @@ -0,0 +1,35 @@ +package com.minelittlepony.unicopia.edibles; + +import net.minecraft.init.MobEffects; +import net.minecraft.potion.PotionEffect; + +public enum Toxicity { + SAFE(0, 0), + FAIR(1, 30), + SEVERE(5, 160), + LETHAL(10, 900); + + private final int level; + private final int duration; + + Toxicity(int level, int duration) { + this.level = level; + this.duration = duration; + } + + public boolean toxicWhenRaw() { + return isLethal() || this != SAFE; + } + + public boolean toxicWhenCooked() { + return isLethal() || this == SEVERE; + } + + public boolean isLethal() { + return this == LETHAL; + } + + public PotionEffect getPoisonEffect() { + return new PotionEffect(MobEffects.POISON, duration, level); + } +} diff --git a/src/main/java/com/minelittlepony/unicopia/edibles/UItemFoodDelegate.java b/src/main/java/com/minelittlepony/unicopia/edibles/UItemFoodDelegate.java new file mode 100644 index 00000000..02b73635 --- /dev/null +++ b/src/main/java/com/minelittlepony/unicopia/edibles/UItemFoodDelegate.java @@ -0,0 +1,58 @@ +package com.minelittlepony.unicopia.edibles; + +import javax.annotation.Nonnull; + +import net.minecraft.block.Block; +import net.minecraft.entity.EntityLivingBase; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.item.EnumAction; +import net.minecraft.item.ItemFood; +import net.minecraft.item.ItemMultiTexture; +import net.minecraft.item.ItemStack; +import net.minecraft.util.ActionResult; +import net.minecraft.util.EnumHand; +import net.minecraft.world.World; + +public class UItemFoodDelegate extends ItemMultiTexture implements IEdible { + + @Nonnull + private ItemFood foodItem = new ItemFood(0, 0, false); + + public UItemFoodDelegate(Block block, ItemMultiTexture.Mapper mapper) { + super(block, block, mapper); + } + + public UItemFoodDelegate setFoodDelegate(@Nonnull ItemFood foodItem) { + this.foodItem = foodItem; + return this; + } + + @Override + public ItemStack onItemUseFinish(ItemStack stack, World worldIn, EntityLivingBase entityLiving) { + return foodItem.onItemUseFinish(stack, worldIn, entityLiving); + } + + @Override + public int getMaxItemUseDuration(ItemStack stack) { + return foodItem.getMaxItemUseDuration(stack); + } + + @Override + public EnumAction getItemUseAction(ItemStack stack) { + return foodItem.getItemUseAction(stack); + } + + @Override + public ActionResult onItemRightClick(World worldIn, EntityPlayer playerIn, EnumHand handIn) { + foodItem.setAlwaysEdible(); + return foodItem.onItemRightClick(worldIn, playerIn, handIn); + } + + @Override + public Toxicity getToxicityLevel(ItemStack stack) { + if (foodItem instanceof IEdible) { + return ((IEdible)foodItem).getToxicityLevel(stack); + } + return null; + } +}