GivePrototype.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using Content.Server.Stack;
  2. using Content.Shared.Construction;
  3. using Content.Shared.Hands.Components;
  4. using Content.Shared.Hands.EntitySystems;
  5. using Content.Shared.Prototypes;
  6. using Content.Shared.Stacks;
  7. using JetBrains.Annotations;
  8. using Robust.Shared.Prototypes;
  9. namespace Content.Server.Construction.Completions;
  10. [UsedImplicitly]
  11. [DataDefinition]
  12. public sealed partial class GivePrototype : IGraphAction
  13. {
  14. [DataField]
  15. public EntProtoId Prototype { get; private set; } = string.Empty;
  16. [DataField]
  17. public int Amount { get; private set; } = 1;
  18. public void PerformAction(EntityUid uid, EntityUid? userUid, IEntityManager entityManager)
  19. {
  20. if (string.IsNullOrEmpty(Prototype))
  21. return;
  22. if (EntityPrototypeHelpers.HasComponent<StackComponent>(Prototype))
  23. {
  24. var stackSystem = entityManager.EntitySysManager.GetEntitySystem<StackSystem>();
  25. var stacks = stackSystem.SpawnMultiple(Prototype, Amount, userUid ?? uid);
  26. if (userUid is null || !entityManager.TryGetComponent(userUid, out HandsComponent? handsComp))
  27. return;
  28. foreach (var item in stacks)
  29. {
  30. stackSystem.TryMergeToHands(item, userUid.Value, hands: handsComp);
  31. }
  32. }
  33. else
  34. {
  35. var handsSystem = entityManager.EntitySysManager.GetEntitySystem<SharedHandsSystem>();
  36. var handsComp = userUid is not null ? entityManager.GetComponent<HandsComponent>(userUid.Value) : null;
  37. for (var i = 0; i < Amount; i++)
  38. {
  39. var item = entityManager.SpawnNextToOrDrop(Prototype, userUid ?? uid);
  40. handsSystem.PickupOrDrop(userUid, item, handsComp: handsComp);
  41. }
  42. }
  43. }
  44. }