Trenta seeds, Draugh raw flesh and Aswid meals each go through an EatFood prefix that, with three slots full and none holding the same food, answered false. Vanilla would replace the most depleted meal past half its time. The UseItem callers also never showed why: EatFood says nothing on a refusal, so the click did nothing at all. That is the Reddit report of a Trenta who could not eat any seeds. The prefixes now hand that case to native EatFood, the Aswid's CanConsumeItem postfix keeps vanilla's answer instead of forcing false, and the Trenta and Draugh callers ask CanEat(item, true) for vanilla's own message when a meal is refused.
205 lines
9.2 KiB
C#
205 lines
9.2 KiB
C#
using HarmonyLib;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using Jotunn.Entities;
|
|
using Jotunn.Managers;
|
|
|
|
namespace RacesOfValheim
|
|
{
|
|
[HarmonyPatch]
|
|
public static class DraughFoodPatches
|
|
{
|
|
private static List<object> hiddenFoods = new List<object>();
|
|
|
|
// Mapping from raw prefab/item name to the cooked prefab name
|
|
public static readonly Dictionary<string, string> rawToCookedMapping = new Dictionary<string, string>
|
|
{
|
|
{ "RawMeat", "CookedMeat" },
|
|
{ "NeckTail", "NeckTailGrilled" },
|
|
{ "FishRaw", "FishCooked" },
|
|
{ "LoxMeat", "CookedLoxMeat" },
|
|
{ "SerpentMeat", "SerpentMeatCooked" },
|
|
{ "WolfMeat", "CookedWolfMeat" },
|
|
{ "DeerMeat", "CookedDeerMeat" },
|
|
{ "HareMeat", "CookedHareMeat" },
|
|
{ "ChickenMeat", "CookedChickenMeat" },
|
|
{ "BugMeat", "CookedBugMeat" },
|
|
{ "VoltureMeat", "RoastedVoltureMeat" },
|
|
{ "GreydwarfEye", "DeerStew" },
|
|
{ "Entrails", "Sausages" },
|
|
{ "Bloodbag", "BloodPudding" },
|
|
{ "MooseMeat", "CookedMooseMeat" },
|
|
// Raw blubber keeps its own name and icon (EdibleUncooked_ copies them from the raw
|
|
// item) and gives the stats of Seal Meat Soup.
|
|
{ "SealBlubber", "SealSoup" }
|
|
};
|
|
|
|
[HarmonyPatch(typeof(Humanoid), "UseItem")]
|
|
[HarmonyPrefix]
|
|
public static bool UseItem_Prefix(Humanoid __instance, Inventory inventory, ItemDrop.ItemData item, bool fromInventoryGui)
|
|
{
|
|
Player player = __instance as Player;
|
|
if (player == null || player != Player.m_localPlayer || item == null || item.m_shared == null) return true;
|
|
|
|
// Prevent non-Draughs from eating Human Meat
|
|
if (item.m_dropPrefab != null && item.m_dropPrefab.name == "HumanMeat")
|
|
{
|
|
if (!DraughRace.IsDraugh())
|
|
{
|
|
player.Message(MessageHud.MessageType.Center, ModText.Get("rov_msg_draugh_this_rotting_flesh_fatal_living"));
|
|
return false; // Block consumption
|
|
}
|
|
// If it IS a Draugh, let them consume it via native mechanics
|
|
return true;
|
|
}
|
|
|
|
// Normal Draugh Food restrictions
|
|
if (!DraughRace.IsDraugh()) return true;
|
|
|
|
// Intercept right click on raw items.
|
|
// In UseItem, if it's a Consumable, Valheim will process it. If it's our EdibleUncooked_, it's a Consumable!
|
|
if (item.m_shared.m_name.StartsWith("$item_edibleuncooked_") || item.m_dropPrefab?.name.StartsWith("EdibleUncooked_") == true)
|
|
{
|
|
return true; // Let native EatFood process our custom item
|
|
}
|
|
|
|
string prefabName = item.m_dropPrefab != null ? item.m_dropPrefab.name : "";
|
|
string cleanName = prefabName;
|
|
if (string.IsNullOrEmpty(cleanName)) cleanName = item.m_shared.m_name.Replace("$item_", "");
|
|
|
|
// Is it one of the raw items in our mapping?
|
|
if (rawToCookedMapping.ContainsKey(cleanName))
|
|
{
|
|
// We want to seamlessly swap it with EdibleUncooked_<cleanName>
|
|
string customPrefabName = "EdibleUncooked_" + cleanName;
|
|
GameObject customPrefab = ObjectDB.instance.GetItemPrefab(customPrefabName);
|
|
if (customPrefab != null)
|
|
{
|
|
ItemDrop customItemDrop = customPrefab.GetComponent<ItemDrop>();
|
|
if (customItemDrop != null)
|
|
{
|
|
// Check if we can eat it (duplicate check bypass logic handled inside EatFood)
|
|
bool ate = player.EatFood(customItemDrop.m_itemData);
|
|
if (ate)
|
|
{
|
|
inventory.RemoveItem(item, 1);
|
|
|
|
var consumeEffects = Traverse.Create(player).Field("m_consumeItemEffects").GetValue<EffectList>();
|
|
if (consumeEffects != null) consumeEffects.Create(player.transform.position, Quaternion.identity, player.transform, 1f, -1);
|
|
|
|
var zanim = Traverse.Create(player).Field("m_zanim").GetValue<ZSyncAnimation>();
|
|
if (zanim != null) zanim.SetTrigger("eat");
|
|
}
|
|
else
|
|
{
|
|
// Vanilla's refusal message, which EatFood never shows by itself
|
|
player.CanEat(customItemDrop.m_itemData, true);
|
|
}
|
|
return false; // Skip native UseItem
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Jotunn.Logger.LogWarning($"[DraughFood] Could not find custom prefab: {customPrefabName}");
|
|
}
|
|
}
|
|
|
|
// If it's a normal consumable (like standard cooked food or berries), we should block it!
|
|
if (item.m_shared.m_itemType == ItemDrop.ItemData.ItemType.Consumable)
|
|
{
|
|
bool isFood = item.m_shared.m_food > 0 || item.m_shared.m_foodStamina > 0 || item.m_shared.m_foodEitr > 0;
|
|
if (isFood)
|
|
{
|
|
player.Message(MessageHud.MessageType.Center, ModText.Get("rov_msg_draugh_draugr_consume_only_raw_flesh"));
|
|
return false; // Block it
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
[HarmonyPatch(typeof(Player), "EatFood")]
|
|
[HarmonyPrefix]
|
|
public static bool EatFood_Prefix(Player __instance, ItemDrop.ItemData item, ref bool __result)
|
|
{
|
|
hiddenFoods.Clear();
|
|
|
|
if (__instance != Player.m_localPlayer || !DraughRace.IsDraugh()) return true;
|
|
if (item == null || item.m_shared == null) return true;
|
|
|
|
// Handle the bypass logic (duplicate food) ONLY for our custom uncooked foods
|
|
string prefabName = item.m_dropPrefab != null ? item.m_dropPrefab.name : "";
|
|
if (prefabName.StartsWith("EdibleUncooked_"))
|
|
{
|
|
var m_foods = Traverse.Create(__instance).Field("m_foods").GetValue<System.Collections.IList>();
|
|
if (m_foods != null)
|
|
{
|
|
if (m_foods.Count >= 3)
|
|
{
|
|
// Check if we can refresh an existing identical food (if time is low)
|
|
bool refreshed = false;
|
|
foreach (object foodObj in m_foods)
|
|
{
|
|
Player.Food f = foodObj as Player.Food;
|
|
if (f != null && f.m_item.m_shared.m_name == item.m_shared.m_name && f.CanEatAgain())
|
|
{
|
|
// Was `f.m_time = 0f`, which expired the flesh instead of renewing
|
|
// it. This is the original of the copy the Aswid and the Trenta
|
|
// both inherited - see RaceFoodRefresh.
|
|
RaceFoodRefresh.Refresh(__instance, f, item);
|
|
|
|
refreshed = true;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (refreshed)
|
|
{
|
|
__result = true;
|
|
return false; // Skip native EatFood
|
|
}
|
|
|
|
// Full, and no slot holds this flesh. Native EatFood replaces the most
|
|
// depleted meal past half its time, or refuses - vanilla's own rule. This
|
|
// used to refuse outright, with no message.
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
// Temporarily hide duplicates to force Valheim to add a new slot
|
|
for (int i = m_foods.Count - 1; i >= 0; i--)
|
|
{
|
|
var foodObj = m_foods[i];
|
|
var foodItem = Traverse.Create(foodObj).Field("m_item").GetValue<ItemDrop.ItemData>();
|
|
if (foodItem != null && foodItem.m_shared.m_name == item.m_shared.m_name)
|
|
{
|
|
hiddenFoods.Add(foodObj);
|
|
m_foods.RemoveAt(i);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
[HarmonyPatch(typeof(Player), "EatFood")]
|
|
[HarmonyPostfix]
|
|
public static void EatFood_Postfix(Player __instance, ItemDrop.ItemData item, ref bool __result)
|
|
{
|
|
if (hiddenFoods.Count > 0)
|
|
{
|
|
var m_foods = Traverse.Create(__instance).Field("m_foods").GetValue<System.Collections.IList>();
|
|
if (m_foods != null)
|
|
{
|
|
foreach (var h in hiddenFoods)
|
|
{
|
|
m_foods.Add(h);
|
|
}
|
|
}
|
|
hiddenFoods.Clear();
|
|
}
|
|
}
|
|
}
|
|
}
|