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
65 changes: 65 additions & 0 deletions EpicLoot/Container_Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,22 @@ namespace EpicLoot
[HarmonyPatch(typeof(Container), nameof(Container.AddDefaultItems))]
public static class Container_AddDefaultItems_Patch
{
// already created (on Awake call) default items should be removed first
public static void Prefix(Container __instance)
{
if (__instance == null || __instance.m_piece == null)
{
return;
}

var containerName = __instance.m_piece.name.Replace("(Clone)", "").Trim();
var lootTables = LootRoller.GetLootTable(containerName);
if (lootTables != null && lootTables.Count > 0)
{
__instance.m_inventory.RemoveAll();
}
}

public static void Postfix(Container __instance)
{
if (__instance == null || __instance.m_piece == null)
Expand All @@ -28,4 +44,53 @@ public static void Postfix(Container __instance)
}
}
}

// Looks like there is no need to change this since all containers are not empty initially
// [HarmonyPatch(typeof(Container), nameof(Container.GetHoverText))]

[HarmonyPatch(typeof(Container), nameof(Container.RPC_RequestOpen))]
public static class Container_RPC_RequestOpen_Patch
{
public static void Prefix(Container __instance, long uid, long playerID)
{
if (__instance == null || __instance.m_piece == null)
{
return;
}

if (__instance.m_nview.IsOwner() && !__instance.m_nview.GetZDO().GetBool("EL_container_items_rolled".GetStableHashCode()))
{
var containerName = __instance.m_piece.name.Replace("(Clone)", "").Trim();
var lootTables = LootRoller.GetLootTable(containerName);
if (lootTables != null && lootTables.Count > 0)
{
__instance.AddDefaultItems();
}
__instance.m_nview.GetZDO().Set("EL_container_items_rolled".GetStableHashCode(), value: true);
}
}
}

[HarmonyPatch(typeof(Container), nameof(Container.OnDestroyed))]
public static class Container_OnDestroyed_Patch
{
public static void Prefix(Container __instance)
{
if (__instance == null || __instance.m_piece == null)
{
return;
}

if (__instance.m_nview.IsOwner() && !__instance.m_nview.GetZDO().GetBool("EL_container_items_rolled".GetStableHashCode()))
{
var containerName = __instance.m_piece.name.Replace("(Clone)", "").Trim();
var lootTables = LootRoller.GetLootTable(containerName);
if (lootTables != null && lootTables.Count > 0)
{
__instance.AddDefaultItems();
}
__instance.m_nview.GetZDO().Set("EL_container_items_rolled".GetStableHashCode(), value: true);
}
}
}
}