From 7ceb2d250799474c6c627121a0cfde7bf90379d4 Mon Sep 17 00:00:00 2001
From: ShadowCommander <10494922+ShadowCommander@users.noreply.github.com>
Date: Wed, 18 Sep 2024 16:55:53 -0700
Subject: [PATCH] Add a method to get the first available ItemSlot (#29846)
* Add a method to get the first available ItemSlot
* Make TryInsertEmpty use TryGetAvailableSlot
* Add param doc comments
---
.../Containers/ItemSlot/ItemSlotsSystem.cs | 69 ++++++++++++++-----
1 file changed, 53 insertions(+), 16 deletions(-)
diff --git a/Content.Shared/Containers/ItemSlot/ItemSlotsSystem.cs b/Content.Shared/Containers/ItemSlot/ItemSlotsSystem.cs
index f41fa2b22d..f25273f403 100644
--- a/Content.Shared/Containers/ItemSlot/ItemSlotsSystem.cs
+++ b/Content.Shared/Containers/ItemSlot/ItemSlotsSystem.cs
@@ -359,39 +359,76 @@ namespace Content.Shared.Containers.ItemSlots
/// Useful for predicted interactions
///
/// False if failed to insert item
- public bool TryInsertEmpty(Entity ent, EntityUid item, EntityUid? user, bool excludeUserAudio = false)
+ public bool TryInsertEmpty(Entity ent,
+ EntityUid item,
+ EntityUid? user,
+ bool excludeUserAudio = false)
{
+ if (!Resolve(ent, ref ent.Comp, false))
+ return false;
+
+ TryComp(user, out HandsComponent? handsComp);
+
+ if (!TryGetAvailableSlot(ent,
+ item,
+ user == null ? null : (user.Value, handsComp),
+ out var itemSlot,
+ emptyOnly: true))
+ return false;
+
+ if (user != null && !_handsSystem.TryDrop(user.Value, item, handsComp: handsComp))
+ return false;
+
+ Insert(ent, itemSlot, item, user, excludeUserAudio: excludeUserAudio);
+ return true;
+ }
+
+ ///
+ /// Tries to get any slot that the can be inserted into.
+ ///
+ /// Entity that is being inserted into.
+ /// Entity being inserted into .
+ /// Entity inserting into .
+ /// The ItemSlot on to insert into.
+ /// True only returns slots that are empty.
+ /// False returns any slot that is able to receive .
+ /// True when a slot is found. Otherwise, false.
+ public bool TryGetAvailableSlot(Entity ent,
+ EntityUid item,
+ Entity? userEnt,
+ [NotNullWhen(true)] out ItemSlot? itemSlot,
+ bool emptyOnly = false)
+ {
+ itemSlot = null;
+
+ if (userEnt is { } user
+ && Resolve(user, ref user.Comp)
+ && _handsSystem.IsHolding(user, item))
+ {
+ if (!_handsSystem.CanDrop(user, item, user.Comp))
+ return false;
+ }
+
if (!Resolve(ent, ref ent.Comp, false))
return false;
var slots = new List();
foreach (var slot in ent.Comp.Slots.Values)
{
- if (slot.ContainerSlot?.ContainedEntity != null)
+ if (emptyOnly && slot.ContainerSlot?.ContainedEntity != null)
continue;
- if (CanInsert(ent, item, user, slot))
+ if (CanInsert(ent, item, userEnt, slot))
slots.Add(slot);
}
if (slots.Count == 0)
return false;
- if (user != null && _handsSystem.IsHolding(user.Value, item))
- {
- if (!_handsSystem.TryDrop(user.Value, item))
- return false;
- }
-
slots.Sort(SortEmpty);
- foreach (var slot in slots)
- {
- if (TryInsert(ent, slot, item, user, excludeUserAudio: excludeUserAudio))
- return true;
- }
-
- return false;
+ itemSlot = slots[0];
+ return true;
}
private static int SortEmpty(ItemSlot a, ItemSlot b)
--
2.52.0