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

@@ -12,7 +12,6 @@ using Content.Shared.Slippery;
using Content.Shared.Strip.Components;
using Content.Shared.Temperature;
using Content.Shared.Verbs;
using Robust.Shared.Containers;
namespace Content.Shared.Inventory;
@@ -59,17 +58,15 @@ public partial class InventorySystem
public void RelayEvent<T>(Entity<InventoryComponent> inventory, ref T args) where T : IInventoryRelayEvent
{
var containerEnumerator = new ContainerSlotEnumerator(inventory, inventory.Comp.TemplateId, _prototypeManager, this, args.TargetSlots);
if (args.TargetSlots == SlotFlags.NONE)
return;
// this copies the by-ref event if it is a struct
var ev = new InventoryRelayedEvent<T>(args);
while (containerEnumerator.MoveNext(out var container))
var enumerator = new InventorySlotEnumerator(inventory, args.TargetSlots);
while (enumerator.NextItem(out var item))
{
if (!container.ContainedEntity.HasValue)
continue;
RaiseLocalEvent(container.ContainedEntity.Value, ev);
RaiseLocalEvent(item, ev);
}
// and now we copy it back
@@ -81,40 +78,23 @@ public partial class InventorySystem
if (args.TargetSlots == SlotFlags.NONE)
return;
var containerEnumerator = new ContainerSlotEnumerator(inventory, inventory.Comp.TemplateId, _prototypeManager, this, args.TargetSlots);
var ev = new InventoryRelayedEvent<T>(args);
while (containerEnumerator.MoveNext(out var container))
var enumerator = new InventorySlotEnumerator(inventory, args.TargetSlots);
while (enumerator.NextItem(out var item))
{
if (!container.ContainedEntity.HasValue)
continue;
RaiseLocalEvent(container.ContainedEntity.Value, ev);
RaiseLocalEvent(item, ev);
}
}
private void OnGetEquipmentVerbs(EntityUid uid, InventoryComponent component, GetVerbsEvent<EquipmentVerb> args)
{
// Automatically relay stripping related verbs to all equipped clothing.
if (!_prototypeManager.TryIndex(component.TemplateId, out InventoryTemplatePrototype? proto))
return;
if (!TryComp(uid, out ContainerManagerComponent? containers))
return;
var ev = new InventoryRelayedEvent<GetVerbsEvent<EquipmentVerb>>(args);
foreach (var slotDef in proto.Slots)
var enumerator = new InventorySlotEnumerator(component);
while (enumerator.NextItem(out var item, out var slotDef))
{
if (slotDef.StripHidden && args.User != uid)
continue;
if (!containers.TryGetContainer(slotDef.Name, out var container))
continue;
if (container is not ContainerSlot slot || slot.ContainedEntity is not { } ent)
continue;
RaiseLocalEvent(ent, ev);
if (!slotDef.StripHidden || args.User == uid)
RaiseLocalEvent(item, ev);
}
}