RacesOfValheim/JotunnModStub/Draugh/DraughFoodPatches.cs
2026-08-02 08:30:53 +02:00

201 lines
9 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" },
{ "SerpentMeatRaw", "SerpentMeatCooked" },
{ "WolfMeat", "CookedWolfMeat" },
{ "DeerMeat", "CookedDeerMeat" },
{ "HareMeat", "CookedHareMeat" },
{ "ChickenMeat", "CookedChickenMeat" },
{ "BugMeat", "CookedBugMeat" },
{ "VoltureMeat", "RoastedVoltureMeat" },
{ "GreydwarfEye", "DeerStew" },
{ "Entrails", "Sausages" },
{ "Bloodbag", "BloodPudding" }
};
[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, "This rotting flesh is fatal to the 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");
}
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, "Draugr consume only raw flesh, eyes, entrails or blood.");
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())
{
f.m_time = 0f;
f.m_item = item.Clone();
var consumeEffects = Traverse.Create(__instance).Field("m_consumeItemEffects").GetValue<EffectList>();
if (consumeEffects != null) consumeEffects.Create(__instance.transform.position, Quaternion.identity, __instance.transform, 1f, -1);
var zanim = Traverse.Create(__instance).Field("m_zanim").GetValue<ZSyncAnimation>();
if (zanim != null) zanim.SetTrigger("eat");
if (item.m_shared.m_food > 0f) __instance.Heal(item.m_shared.m_food, true);
refreshed = true;
break;
}
}
if (refreshed)
{
__result = true;
return false; // Skip native EatFood
}
// Inventory full
__result = false;
return false;
}
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();
}
}
}
}