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

@@ -25,10 +25,8 @@ namespace Content.Server.Inventory
private void OnExploded(Entity<InventoryComponent> ent, ref BeforeExplodeEvent args)
{
if (!TryGetContainerSlotEnumerator(ent, out var slots, ent.Comp))
return;
// explode each item in their inventory too
var slots = new InventorySlotEnumerator(ent);
while (slots.MoveNext(out var slot))
{
if (slot.ContainedEntity != null)
@@ -55,33 +53,16 @@ namespace Content.Server.Inventory
}
}
public void TransferEntityInventories(EntityUid uid, EntityUid target)
public void TransferEntityInventories(Entity<InventoryComponent?> source, Entity<InventoryComponent?> target)
{
if (!TryGetContainerSlotEnumerator(uid, out var enumerator))
if (!Resolve(source.Owner, ref source.Comp) || !Resolve(target.Owner, ref target.Comp))
return;
Dictionary<string, EntityUid> inventoryEntities = new();
var slots = GetSlots(uid);
while (enumerator.MoveNext(out var containerSlot))
var enumerator = new InventorySlotEnumerator(source.Comp);
while (enumerator.NextItem(out var item, out var slot))
{
//records all the entities stored in each of the target's slots
foreach (var slot in slots)
{
if (TryGetSlotContainer(target, slot.Name, out var conslot, out _) &&
conslot.ID == containerSlot.ID &&
containerSlot.ContainedEntity is { } containedEntity)
{
inventoryEntities.Add(slot.Name, containedEntity);
}
}
//drops everything in the target's inventory on the ground
TryUnequip(uid, containerSlot.ID, true, true);
}
// This takes the objects we removed and stored earlier
// and actually equips all of it to the new entity
foreach (var (slot, item) in inventoryEntities)
{
TryEquip(target, item, slot , true, true);
if (TryUnequip(source, slot.Name, true, true, inventory: source.Comp))
TryEquip(target, item, slot.Name , true, true, inventory: target.Comp);
}
}
}