Files
tbd-station-14/Content.Shared/Weapons/Ranged/Systems/SharedGunSystem.Clothing.cs
slarticodefast bb2981400c replace all uses of TryGetContainingContainer with non-obsolete overload (#30583)
* replace all uses of TryGetContainerContainer with non-obsolete overload

* rerun
2024-08-04 15:38:53 +10:00

55 lines
1.8 KiB
C#

using System.Diagnostics.CodeAnalysis;
using Content.Shared.Inventory;
using Content.Shared.Weapons.Ranged.Components;
using Content.Shared.Weapons.Ranged.Events;
namespace Content.Shared.Weapons.Ranged.Systems;
public partial class SharedGunSystem
{
[Dependency] private readonly InventorySystem _inventory = default!;
private void InitializeClothing()
{
SubscribeLocalEvent<ClothingSlotAmmoProviderComponent, TakeAmmoEvent>(OnClothingTakeAmmo);
SubscribeLocalEvent<ClothingSlotAmmoProviderComponent, GetAmmoCountEvent>(OnClothingAmmoCount);
}
private void OnClothingTakeAmmo(EntityUid uid, ClothingSlotAmmoProviderComponent component, TakeAmmoEvent args)
{
if (!TryGetClothingSlotEntity(uid, component, out var entity))
return;
RaiseLocalEvent(entity.Value, args);
}
private void OnClothingAmmoCount(EntityUid uid, ClothingSlotAmmoProviderComponent component, ref GetAmmoCountEvent args)
{
if (!TryGetClothingSlotEntity(uid, component, out var entity))
return;
RaiseLocalEvent(entity.Value, ref args);
}
private bool TryGetClothingSlotEntity(EntityUid uid, ClothingSlotAmmoProviderComponent component, [NotNullWhen(true)] out EntityUid? slotEntity)
{
slotEntity = null;
if (!Containers.TryGetContainingContainer((uid, null, null), out var container))
return false;
var user = container.Owner;
if (!_inventory.TryGetContainerSlotEnumerator(user, out var enumerator, component.TargetSlot))
return false;
while (enumerator.NextItem(out var item))
{
if (_whitelistSystem.IsWhitelistFailOrNull(component.ProviderWhitelist, item))
continue;
slotEntity = item;
return true;
}
return false;
}
}