RacesOfValheim/JotunnModStub/TrolsHammer/TrolsHammerItem.cs
2026-08-13 19:13:24 +02:00

97 lines
4.4 KiB
C#

using Jotunn.Configs;
using Jotunn.Entities;
using Jotunn.Managers;
using UnityEngine;
namespace RacesOfValheim
{
/// <summary>
/// Trol's Hammer: the builder's hammer, one size up.
///
/// It is a straight clone of the vanilla Hammer and it deliberately keeps the ONE field that
/// makes it a hammer at all - m_buildPieces, the PieceTable. Sharing the vanilla table rather
/// than cloning it is the whole trick behind "exactly the same things to build": every piece
/// vanilla puts in the hammer's menu is there, at the same cost and with the same drop, and so
/// is every piece another mod adds to that table later. A cloned table would have been a
/// snapshot of the build menu taken at startup and would have quietly gone stale.
///
/// Nothing about the piece prefabs is touched, so the size does not live on the item: it lives
/// on the placement, and TrolsHammerPatches is where that happens.
///
/// The recipe (1 core wood, 2 fine wood) is on a workbench rather than free-hand like the
/// vanilla hammer, because both materials already require one to exist.
/// </summary>
public static class TrolsHammerItem
{
public const string PrefabName = "TrolsHammer";
public const string DisplayName = "Trol's Hammer";
private const string SourcePrefab = "Hammer";
private const string Label = "TrolsHammer";
/// <summary>Core wood. The item id is RoundLog - a display name that does not predict the id.</summary>
private const string CoreWood = "RoundLog";
private const string FineWood = "FineWood";
public static void Register()
{
GameObject prefab = PrefabManager.Instance.CreateClonedPrefab(PrefabName, SourcePrefab);
if (prefab == null)
{
Jotunn.Logger.LogWarning($"[{Label}] Could not clone {SourcePrefab}; {DisplayName} not registered.");
return;
}
ItemDrop drop = prefab.GetComponent<ItemDrop>();
if (drop == null || drop.m_itemData == null || drop.m_itemData.m_shared == null)
{
Jotunn.Logger.LogWarning($"[{Label}] {SourcePrefab} clone has no usable ItemDrop; {DisplayName} not registered.");
return;
}
ItemDrop.ItemData.SharedData shared = drop.m_itemData.m_shared;
shared.m_name = DisplayName;
shared.m_description = "A hammer swung by something that never had to duck through a " +
"doorway. It builds everything the ordinary one builds, out of the " +
"same materials, and simply builds it bigger.";
if (shared.m_buildPieces == null)
{
// Not fatal, but worth saying out loud: without a piece table the item is a stick.
Jotunn.Logger.LogWarning($"[{Label}] The {SourcePrefab} clone carries no build piece table; " +
$"{DisplayName} will open an empty build menu.");
}
ItemManager.Instance.AddItem(new CustomItem(prefab, fixReference: false));
ItemManager.Instance.AddRecipe(new CustomRecipe(new RecipeConfig
{
Item = PrefabName,
Amount = 1,
CraftingStation = CraftingStations.Workbench,
MinStationLevel = 1,
Requirements = new[]
{
new RequirementConfig(CoreWood, 1, 0, true),
new RequirementConfig(FineWood, 2, 0, true),
},
}));
Jotunn.Logger.LogInfo($"[{Label}] {DisplayName} registered on the vanilla hammer's piece table.");
}
/// <summary>
/// Whether an item in hand is this hammer.
///
/// m_dropPrefab is the reliable identity - it is the prefab the ItemData came from - but it
/// is null on an ItemData built before ObjectDB finished, so the display name is kept as a
/// second reading rather than as the only one.
/// </summary>
public static bool IsTrolsHammer(ItemDrop.ItemData item)
{
if (item == null) return false;
if (item.m_dropPrefab != null) return item.m_dropPrefab.name == PrefabName;
return item.m_shared != null && item.m_shared.m_name == DisplayName;
}
}
}