ServerInventorySystem.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. using Content.Shared.Explosion;
  2. using Content.Shared.Inventory;
  3. namespace Content.Server.Inventory
  4. {
  5. public sealed class ServerInventorySystem : InventorySystem
  6. {
  7. public override void Initialize()
  8. {
  9. base.Initialize();
  10. SubscribeLocalEvent<InventoryComponent, BeforeExplodeEvent>(OnExploded);
  11. }
  12. private void OnExploded(Entity<InventoryComponent> ent, ref BeforeExplodeEvent args)
  13. {
  14. // explode each item in their inventory too
  15. var slots = new InventorySlotEnumerator(ent);
  16. while (slots.MoveNext(out var slot))
  17. {
  18. if (slot.ContainedEntity != null)
  19. args.Contents.Add(slot.ContainedEntity.Value);
  20. }
  21. }
  22. public void TransferEntityInventories(Entity<InventoryComponent?> source, Entity<InventoryComponent?> target)
  23. {
  24. if (!Resolve(source.Owner, ref source.Comp) || !Resolve(target.Owner, ref target.Comp))
  25. return;
  26. var enumerator = new InventorySlotEnumerator(source.Comp);
  27. while (enumerator.NextItem(out var item, out var slot))
  28. {
  29. if (TryUnequip(source, slot.Name, true, true, inventory: source.Comp))
  30. TryEquip(target, item, slot.Name , true, true, inventory: target.Comp);
  31. }
  32. }
  33. }
  34. }