diff --git a/EpicLoot/Container_Patch.cs b/EpicLoot/Container_Patch.cs index 4b7c5efa4..fabb227a3 100644 --- a/EpicLoot/Container_Patch.cs +++ b/EpicLoot/Container_Patch.cs @@ -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) @@ -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); + } + } + } }