Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions Common/Collectible/Collectible.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1757,13 +1757,21 @@ public virtual bool OnHeldInteractCancel(float secondsUsed, ItemSlot slot, Entit
return true;
}

/// <summary>
/// Let items with nutrition decide if they're actually edible. Prevents oddities like showing an eating animation that does nothing.
/// For example, pies cannot be eaten unless they're both sliced and not raw even though they have nutrition properties.
/// </summary>
public virtual bool CanEat(ItemSlot slot, EntityAgent byEntity)
{
return !slot.Empty && GetNutritionProperties(byEntity?.World, slot.Itemstack, byEntity) != null;
}

/// <summary>
/// Tries to eat the contents in the slot, first call
/// </summary>
protected virtual void tryEatBegin(ItemSlot slot, EntityAgent byEntity, ref EnumHandHandling handling, string eatSound = "eat", int eatSoundRepeats = 1)
{
if (!slot.Empty && GetNutritionProperties(byEntity.World, slot.Itemstack, byEntity) != null)
if (CanEat(slot, byEntity))
{
byEntity.World.RegisterCallback((dt) => playEatSound(byEntity, eatSound, eatSoundRepeats), 500);

Expand Down Expand Up @@ -1799,7 +1807,7 @@ protected void playEatSound(EntityAgent byEntity, string eatSound = "eat", int e
/// <param name="spawnParticleStack"></param>
protected virtual bool tryEatStep(float secondsUsed, ItemSlot slot, EntityAgent byEntity, ItemStack spawnParticleStack = null)
{
if (GetNutritionProperties(byEntity.World, slot.Itemstack, byEntity) == null) return false;
if (!CanEat(slot, byEntity)) return false;

Vec3d pos = byEntity.Pos.AheadCopy(0.4f).XYZ;
pos.X += byEntity.LocalEyePos.X;
Expand Down Expand Up @@ -1830,7 +1838,7 @@ protected virtual void tryEatStop(float secondsUsed, ItemSlot slot, EntityAgent
{
FoodNutritionProperties nutriProps = GetNutritionProperties(byEntity.World, slot.Itemstack, byEntity);

if (byEntity.World is IServerWorldAccessor && nutriProps != null && secondsUsed >= 0.95f)
if (byEntity.World is IServerWorldAccessor && nutriProps != null && CanEat(slot, byEntity) && secondsUsed >= 0.95f)
{
TransitionState state = UpdateAndGetTransitionState(api.World, slot, EnumTransitionType.Perish);
float spoilState = state != null ? state.TransitionLevel : 0;
Expand Down Expand Up @@ -2143,7 +2151,7 @@ public virtual WorldInteraction[] GetHeldInteractionHelp(ItemSlot inSlot)
{
WorldInteraction[] interactions;

if (GetNutritionProperties(api.World, inSlot.Itemstack, null) != null)
if (CanEat(inSlot, null))
{
interactions = new WorldInteraction[]
{
Expand Down