All major flowers can now be eaten by the player

This commit is contained in:
Sollace 2019-01-29 13:53:43 +02:00
parent 435dc1c02d
commit 71b518702d
7 changed files with 327 additions and 0 deletions

View file

@ -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<Item> 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);

View file

@ -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));
}
}
}

View file

@ -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));
}
}
}

View file

@ -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) {
}
}

View file

@ -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<ItemStack> onItemRightClick(World world, EntityPlayer player, EnumHand hand) {
Race race = PlayerSpeciesList.instance().getPlayer(player).getPlayerSpecies();
if (race.isDefault() || race == Race.CHANGELING) {
return new ActionResult<ItemStack>(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);
}
}

View file

@ -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);
}
}

View file

@ -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<ItemStack> 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;
}
}