Inventory slot enumerator rejig (#21788)

This commit is contained in:
Leon Friedrich
2023-12-07 16:20:51 -05:00
committed by GitHub
parent 445c474c2c
commit 287d22cc49
18 changed files with 238 additions and 342 deletions

View File

@@ -10,11 +10,11 @@ public partial class InventorySystem
/// <summary>
/// Yields all entities in hands or inventory slots with the specific flags.
/// </summary>
public IEnumerable<EntityUid> GetHandOrInventoryEntities(EntityUid user, SlotFlags flags = SlotFlags.All)
public IEnumerable<EntityUid> GetHandOrInventoryEntities(Entity<HandsComponent?, InventoryComponent?> user, SlotFlags flags = SlotFlags.All)
{
if (TryComp<HandsComponent>(user, out var handsComp))
if (Resolve(user.Owner, ref user.Comp1, false))
{
foreach (var hand in handsComp.Hands.Values)
foreach (var hand in user.Comp1.Hands.Values)
{
if (hand.HeldEntity == null)
continue;
@@ -23,27 +23,22 @@ public partial class InventorySystem
}
}
if (TryComp<InventoryComponent>(user, out var inventoryComp))
if (!Resolve(user.Owner, ref user.Comp2, false))
yield break;
var slotEnumerator = new InventorySlotEnumerator(user.Comp2, flags);
while (slotEnumerator.NextItem(out var item))
{
var slotEnumerator = new ContainerSlotEnumerator(user, inventoryComp.TemplateId,
_prototypeManager, this, flags);
while (slotEnumerator.MoveNext(out var slot))
{
if (slot.ContainedEntity == null)
continue;
yield return slot.ContainedEntity.Value;
}
yield return item;
}
}
/// <summary>
/// Returns the definition of the inventory slot that the given entity is currently in..
/// </summary>
public bool TryGetContainingSlot(EntityUid uid, [NotNullWhen(true)] out SlotDefinition? slot)
public bool TryGetContainingSlot(Entity<TransformComponent?, MetaDataComponent?> entity, [NotNullWhen(true)] out SlotDefinition? slot)
{
if (!_containerSystem.TryGetContainingContainer(uid, out var container))
if (!_containerSystem.TryGetContainingContainer(entity.Owner, out var container, entity.Comp2, entity.Comp1))
{
slot = null;
return false;
@@ -55,9 +50,10 @@ public partial class InventorySystem
/// <summary>
/// Returns true if the given entity is equipped to an inventory slot with the given inventory slot flags.
/// </summary>
public bool InSlotWithFlags(EntityUid uid, SlotFlags flags)
public bool InSlotWithFlags(Entity<TransformComponent?, MetaDataComponent?> entity, SlotFlags flags)
{
return TryGetContainingSlot(uid, out var slot) && ((slot.SlotFlags & flags) == flags);
return TryGetContainingSlot(entity, out var slot)
&& (slot.SlotFlags & flags) == flags;
}
public bool SpawnItemInSlot(EntityUid uid, string slot, string prototype, bool silent = false, bool force = false, InventoryComponent? inventory = null)