From eaa27901166f7bf8cda33455b51a65bd69e62d59 Mon Sep 17 00:00:00 2001 From: Sollace Date: Fri, 1 Feb 2019 10:38:45 +0200 Subject: [PATCH] Fixed and balanced food effects for changelings --- .../com/minelittlepony/unicopia/Unicopia.java | 23 ++++++++-- .../minelittlepony/unicopia/player/IFood.java | 11 +++++ .../unicopia/player/IPlayer.java | 8 +++- .../unicopia/player/PlayerCapabilities.java | 32 ++++++++++++-- .../unicopia/player/PlayerFood.java | 42 +++++++++++++++++++ 5 files changed, 109 insertions(+), 7 deletions(-) create mode 100644 src/main/java/com/minelittlepony/unicopia/player/IFood.java create mode 100644 src/main/java/com/minelittlepony/unicopia/player/PlayerFood.java diff --git a/src/main/java/com/minelittlepony/unicopia/Unicopia.java b/src/main/java/com/minelittlepony/unicopia/Unicopia.java index 12975abb..a55dc7da 100644 --- a/src/main/java/com/minelittlepony/unicopia/Unicopia.java +++ b/src/main/java/com/minelittlepony/unicopia/Unicopia.java @@ -6,7 +6,6 @@ import net.minecraft.block.state.IBlockState; import net.minecraft.entity.Entity; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.init.Blocks; -import net.minecraft.item.EnumAction; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.item.crafting.IRecipe; @@ -239,12 +238,30 @@ public class Unicopia implements IGuiHandler { Commands.init(event); } + @SubscribeEvent + public static void onItemUseBegin(LivingEntityUseItemEvent.Start event) { + Entity e = event.getEntity(); + + if (!event.isCanceled() && e instanceof EntityPlayer) { + PlayerSpeciesList.instance().getPlayer((EntityPlayer)e).getFood().begin(event.getItem()); + } + } + + @SubscribeEvent + public static void onItemUseCancel(LivingEntityUseItemEvent.Stop event) { + Entity e = event.getEntity(); + + if (!event.isCanceled() && e instanceof EntityPlayer) { + PlayerSpeciesList.instance().getPlayer((EntityPlayer)e).getFood().end(); + } + } + @SubscribeEvent public static void onItemUseFinish(LivingEntityUseItemEvent.Finish event) { Entity e = event.getEntity(); - if (!event.isCanceled() && e instanceof EntityPlayer && event.getItem().getItemUseAction() == EnumAction.EAT) { - PlayerSpeciesList.instance().getPlayer((EntityPlayer)e).onEntityEat(); + if (!event.isCanceled() && e instanceof EntityPlayer) { + PlayerSpeciesList.instance().getPlayer((EntityPlayer)e).getFood().finish(); } } diff --git a/src/main/java/com/minelittlepony/unicopia/player/IFood.java b/src/main/java/com/minelittlepony/unicopia/player/IFood.java new file mode 100644 index 00000000..c17ebda6 --- /dev/null +++ b/src/main/java/com/minelittlepony/unicopia/player/IFood.java @@ -0,0 +1,11 @@ +package com.minelittlepony.unicopia.player; + +import net.minecraft.item.ItemStack; + +public interface IFood { + void begin(ItemStack stack); + + void end(); + + void finish(); +} diff --git a/src/main/java/com/minelittlepony/unicopia/player/IPlayer.java b/src/main/java/com/minelittlepony/unicopia/player/IPlayer.java index 04f7f395..bd795642 100644 --- a/src/main/java/com/minelittlepony/unicopia/player/IPlayer.java +++ b/src/main/java/com/minelittlepony/unicopia/player/IPlayer.java @@ -2,6 +2,8 @@ package com.minelittlepony.unicopia.player; import java.util.UUID; +import javax.annotation.Nullable; + import com.minelittlepony.model.anim.IInterpolator; import com.minelittlepony.unicopia.UClient; import com.minelittlepony.unicopia.enchanting.IPageOwner; @@ -10,6 +12,8 @@ import com.minelittlepony.unicopia.spell.ICaster; import net.minecraft.entity.Entity; import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.item.ItemFood; +import net.minecraft.item.ItemStack; import net.minecraftforge.fml.common.FMLCommonHandler; public interface IPlayer extends ICaster, IRaceContainer, ITransmittable, IPageOwner { @@ -20,6 +24,8 @@ public interface IPlayer extends ICaster, IRaceContainer, IRaceContainer effectDelegate = new EffectSync<>(this, EFFECT); private final IInterpolator interpolator = new BasicEasingInterpolator(); @@ -224,12 +231,31 @@ class PlayerCapabilities implements IPlayer { } @Override - public void onEntityEat() { + public IFood getFood() { + return food; + } + + @Override + public void onEntityEat(ItemStack stack, @Nullable ItemFood food) { if (getPlayerSpecies() == Race.CHANGELING) { EntityPlayer player = getOwner(); - player.addPotionEffect(new PotionEffect(MobEffects.WEAKNESS, 2000, 2)); - player.addPotionEffect(new PotionEffect(MobEffects.NAUSEA, 2000, 2)); + if (food != null) { + int health = food.getHealAmount(stack); + float saturation = food.getSaturationModifier(stack); + + player.getFoodStats().addStats(-health/2, -saturation/2); + + player.addPotionEffect(new PotionEffect(MobEffects.NAUSEA, 100, 3, true, true)); + } else { + player.addPotionEffect(new PotionEffect(MobEffects.NAUSEA, 200, 3, true, true)); + } + + if (player.world.getDifficulty() != EnumDifficulty.PEACEFUL && player.world.rand.nextInt(20) == 0) { + player.addPotionEffect(new PotionEffect(UEffects.FOOD_POISONING, 300, 2, true, true)); + } + + player.addPotionEffect(new PotionEffect(MobEffects.WEAKNESS, 2000, 2, true, true)); } } diff --git a/src/main/java/com/minelittlepony/unicopia/player/PlayerFood.java b/src/main/java/com/minelittlepony/unicopia/player/PlayerFood.java new file mode 100644 index 00000000..00746a05 --- /dev/null +++ b/src/main/java/com/minelittlepony/unicopia/player/PlayerFood.java @@ -0,0 +1,42 @@ +package com.minelittlepony.unicopia.player; + +import net.minecraft.item.EnumAction; +import net.minecraft.item.ItemFood; +import net.minecraft.item.ItemStack; + +class PlayerFood implements IFood { + + private final IPlayer player; + + private ItemStack eatingStack = ItemStack.EMPTY; + + public PlayerFood(IPlayer player) { + this.player = player; + } + + @Override + public void begin(ItemStack stack) { + eatingStack = ItemStack.EMPTY; + + if (!stack.isEmpty()) { + EnumAction action = stack.getItemUseAction(); + + if (action == EnumAction.EAT && stack.getItem() instanceof ItemFood) { + eatingStack = stack.copy(); + } + } + } + + @Override + public void end() { + eatingStack = ItemStack.EMPTY; + } + + @Override + public void finish() { + if (!eatingStack.isEmpty()) { + player.onEntityEat(eatingStack, (ItemFood)eatingStack.getItem()); + } + } + +}