Skip to content
Merged
Show file tree
Hide file tree
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
23 changes: 23 additions & 0 deletions src/ShipInventoryUpdated/Patches/RoundManager_Patches.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using HarmonyLib;
using ShipInventoryUpdated.Helpers;
using ShipInventoryUpdated.Scripts;

// ReSharper disable InconsistentNaming

namespace ShipInventoryUpdated.Patches;

[HarmonyPatch(typeof(RoundManager))]
internal class RoundManager_Patches
{
[HarmonyPatch(nameof(RoundManager.DespawnPropsAtEndOfRound))]
[HarmonyPostfix]
private static void DespawnPropsAtEndOfRound_Postfix(RoundManager __instance)
{
if (!__instance.IsServer)
return;

var itemsToUpdate = Inventory.Items.Where(item => !item.PERSISTED_THROUGH_ROUNDS).ToArray();

Inventory.MarkPersisted(itemsToUpdate);
}
}
31 changes: 31 additions & 0 deletions src/ShipInventoryUpdated/Scripts/Inventory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,20 @@ public static ItemData[] Items
}
}

/// <summary>
/// Marks the items as they persisted through rounds
/// </summary>
public static void MarkPersisted(ItemData[] items)
{
if (_instance == null)
{
Logger.Warn("Tried to modify items in the inventory, but no instance was defined.");
return;
}

_instance.MarkPersistedServerRpc(items);
}

#endregion

#region Unity
Expand Down Expand Up @@ -133,5 +147,22 @@ private void ClearServerRpc()
_storedItems.Clear();
}

[ServerRpc(RequireOwnership = false)]
private void MarkPersistedServerRpc(params ItemData[] items)
{
var newItems = new HashSet<ItemData>(items);

for (var i = 0; i < _storedItems.Count; i++)
{
var item = _storedItems[i];

if (!newItems.Contains(item))
continue;

item.PERSISTED_THROUGH_ROUNDS = true;
_storedItems[i] = item;
}
}

#endregion
}
1 change: 1 addition & 0 deletions src/ShipInventoryUpdated/ShipInventoryUpdated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ private void Patch()
{
_harmony = new Harmony(MyPluginInfo.PLUGIN_GUID);
_harmony.PatchAll(typeof(Patches.GameNetworkManager_Patches));
_harmony.PatchAll(typeof(Patches.RoundManager_Patches));
_harmony.PatchAll(typeof(Patches.StartOfRound_Patches));
_harmony.PatchAll(typeof(Patches.Terminal_Patches));
}
Expand Down