Prevent inventory events from being relayed to pockets (#6074)
Co-authored-by: ShadowCommander <10494922+ShadowCommander@users.noreply.github.com>
This commit is contained in:
@@ -16,9 +16,9 @@ public partial class InventorySystem
|
||||
SubscribeLocalEvent<InventoryComponent, RefreshMovementSpeedModifiersEvent>(RelayInventoryEvent);
|
||||
}
|
||||
|
||||
protected void RelayInventoryEvent<T>(EntityUid uid, InventoryComponent component, T args) where T : EntityEventArgs
|
||||
protected void RelayInventoryEvent<T>(EntityUid uid, InventoryComponent component, T args) where T : EntityEventArgs, IInventoryRelayEvent
|
||||
{
|
||||
var containerEnumerator = new ContainerSlotEnumerator(uid, component.TemplateId, _prototypeManager, this);
|
||||
var containerEnumerator = new ContainerSlotEnumerator(uid, component.TemplateId, _prototypeManager, this, args.TargetSlots);
|
||||
while(containerEnumerator.MoveNext(out var container))
|
||||
{
|
||||
if(!container.ContainedEntity.HasValue) continue;
|
||||
@@ -26,3 +26,18 @@ public partial class InventorySystem
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Events that should be relayed to inventory slots should implement this interface.
|
||||
/// </summary>
|
||||
public interface IInventoryRelayEvent
|
||||
{
|
||||
/// <summary>
|
||||
/// What inventory slots should this event be relayed to, if any?
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// In general you may want to exclude <see cref="SlotFlags.POCKET"/>, given that those items are not truly
|
||||
/// "equipped" by the user.
|
||||
/// </remarks>
|
||||
public SlotFlags TargetSlots { get; }
|
||||
}
|
||||
|
||||
@@ -86,32 +86,38 @@ public partial class InventorySystem : EntitySystem
|
||||
private readonly InventorySystem _inventorySystem;
|
||||
private readonly EntityUid _uid;
|
||||
private readonly SlotDefinition[] _slots;
|
||||
private int _nextIdx = int.MaxValue;
|
||||
private readonly SlotFlags _flags;
|
||||
private int _nextIdx = 0;
|
||||
|
||||
public ContainerSlotEnumerator(EntityUid uid, string prototypeId, IPrototypeManager prototypeManager, InventorySystem inventorySystem)
|
||||
public ContainerSlotEnumerator(EntityUid uid, string prototypeId, IPrototypeManager prototypeManager, InventorySystem inventorySystem, SlotFlags flags = SlotFlags.All)
|
||||
{
|
||||
_uid = uid;
|
||||
_inventorySystem = inventorySystem;
|
||||
_flags = flags;
|
||||
|
||||
if (prototypeManager.TryIndex<InventoryTemplatePrototype>(prototypeId, out var prototype))
|
||||
{
|
||||
_slots = prototype.Slots;
|
||||
if(_slots.Length > 0)
|
||||
_nextIdx = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
_slots = Array.Empty<SlotDefinition>();
|
||||
}
|
||||
}
|
||||
|
||||
public bool MoveNext([NotNullWhen(true)] out ContainerSlot? container)
|
||||
{
|
||||
container = null;
|
||||
if (_nextIdx >= _slots.Length) return false;
|
||||
|
||||
while (_nextIdx < _slots.Length && !_inventorySystem.TryGetSlotContainer(_uid, _slots[_nextIdx++].Name, out container, out _)) { }
|
||||
while (_nextIdx < _slots.Length)
|
||||
{
|
||||
var slot = _slots[_nextIdx];
|
||||
_nextIdx++;
|
||||
|
||||
return container != null;
|
||||
if ((slot.SlotFlags & _flags) == 0)
|
||||
continue;
|
||||
|
||||
if (_inventorySystem.TryGetSlotContainer(_uid, slot.Name, out container, out _))
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,4 +26,5 @@ public enum SlotFlags
|
||||
POCKET = 1 << 12,
|
||||
LEGS = 1 << 13,
|
||||
FEET = 1 << 14,
|
||||
All = ~NONE,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user