SharedGunSystem.Clothing.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using System.Diagnostics.CodeAnalysis;
  2. using Content.Shared.Inventory;
  3. using Content.Shared.Weapons.Ranged.Components;
  4. using Content.Shared.Weapons.Ranged.Events;
  5. namespace Content.Shared.Weapons.Ranged.Systems;
  6. public partial class SharedGunSystem
  7. {
  8. [Dependency] private readonly InventorySystem _inventory = default!;
  9. private void InitializeClothing()
  10. {
  11. SubscribeLocalEvent<ClothingSlotAmmoProviderComponent, TakeAmmoEvent>(OnClothingTakeAmmo);
  12. SubscribeLocalEvent<ClothingSlotAmmoProviderComponent, GetAmmoCountEvent>(OnClothingAmmoCount);
  13. }
  14. private void OnClothingTakeAmmo(EntityUid uid, ClothingSlotAmmoProviderComponent component, TakeAmmoEvent args)
  15. {
  16. if (!TryGetClothingSlotEntity(uid, component, out var entity))
  17. return;
  18. RaiseLocalEvent(entity.Value, args);
  19. }
  20. private void OnClothingAmmoCount(EntityUid uid, ClothingSlotAmmoProviderComponent component, ref GetAmmoCountEvent args)
  21. {
  22. if (!TryGetClothingSlotEntity(uid, component, out var entity))
  23. return;
  24. RaiseLocalEvent(entity.Value, ref args);
  25. }
  26. private bool TryGetClothingSlotEntity(EntityUid uid, ClothingSlotAmmoProviderComponent component, [NotNullWhen(true)] out EntityUid? slotEntity)
  27. {
  28. slotEntity = null;
  29. if (!Containers.TryGetContainingContainer((uid, null, null), out var container))
  30. return false;
  31. var user = container.Owner;
  32. if (!_inventory.TryGetContainerSlotEnumerator(user, out var enumerator, component.TargetSlot))
  33. return false;
  34. while (enumerator.NextItem(out var item))
  35. {
  36. if (_whitelistSystem.IsWhitelistFailOrNull(component.ProviderWhitelist, item))
  37. continue;
  38. slotEntity = item;
  39. return true;
  40. }
  41. return false;
  42. }
  43. }