SpawnPrototype.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using Content.Server.Stack;
  2. using Content.Shared.Construction;
  3. using Content.Shared.Prototypes;
  4. using Content.Shared.Stacks;
  5. using JetBrains.Annotations;
  6. using Robust.Shared.Prototypes;
  7. using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
  8. namespace Content.Server.Construction.Completions
  9. {
  10. [UsedImplicitly]
  11. [DataDefinition]
  12. public sealed partial class SpawnPrototype : IGraphAction
  13. {
  14. [DataField("prototype", customTypeSerializer:typeof(PrototypeIdSerializer<EntityPrototype>))]
  15. public string Prototype { get; private set; } = string.Empty;
  16. [DataField("amount")]
  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. var coordinates = entityManager.GetComponent<TransformComponent>(uid).Coordinates;
  23. if (EntityPrototypeHelpers.HasComponent<StackComponent>(Prototype))
  24. {
  25. var stackEnt = entityManager.SpawnEntity(Prototype, coordinates);
  26. var stack = entityManager.GetComponent<StackComponent>(stackEnt);
  27. entityManager.EntitySysManager.GetEntitySystem<StackSystem>().SetCount(stackEnt, Amount, stack);
  28. }
  29. else
  30. {
  31. for (var i = 0; i < Amount; i++)
  32. {
  33. entityManager.SpawnEntity(Prototype, coordinates);
  34. }
  35. }
  36. }
  37. }
  38. }