SpawnAfterInteractSystem.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using Content.Server.Engineering.Components;
  2. using Content.Server.Stack;
  3. using Content.Shared.Coordinates.Helpers;
  4. using Content.Shared.DoAfter;
  5. using Content.Shared.Interaction;
  6. using Content.Shared.Maps;
  7. using Content.Shared.Physics;
  8. using Content.Shared.Stacks;
  9. using JetBrains.Annotations;
  10. using Robust.Shared.Map.Components;
  11. namespace Content.Server.Engineering.EntitySystems
  12. {
  13. [UsedImplicitly]
  14. public sealed class SpawnAfterInteractSystem : EntitySystem
  15. {
  16. [Dependency] private readonly SharedDoAfterSystem _doAfterSystem = default!;
  17. [Dependency] private readonly StackSystem _stackSystem = default!;
  18. [Dependency] private readonly TurfSystem _turfSystem = default!;
  19. public override void Initialize()
  20. {
  21. base.Initialize();
  22. SubscribeLocalEvent<SpawnAfterInteractComponent, AfterInteractEvent>(HandleAfterInteract);
  23. }
  24. private async void HandleAfterInteract(EntityUid uid, SpawnAfterInteractComponent component, AfterInteractEvent args)
  25. {
  26. if (!args.CanReach && !component.IgnoreDistance)
  27. return;
  28. if (string.IsNullOrEmpty(component.Prototype))
  29. return;
  30. if (!TryComp<MapGridComponent>(args.ClickLocation.GetGridUid(EntityManager), out var grid))
  31. return;
  32. if (!grid.TryGetTileRef(args.ClickLocation, out var tileRef))
  33. return;
  34. bool IsTileClear()
  35. {
  36. return tileRef.Tile.IsEmpty == false && !_turfSystem.IsTileBlocked(tileRef, CollisionGroup.MobMask);
  37. }
  38. if (!IsTileClear())
  39. return;
  40. if (component.DoAfterTime > 0)
  41. {
  42. var doAfterArgs = new DoAfterArgs(EntityManager, args.User, component.DoAfterTime, new AwaitedDoAfterEvent(), null)
  43. {
  44. BreakOnMove = true,
  45. };
  46. var result = await _doAfterSystem.WaitDoAfter(doAfterArgs);
  47. if (result != DoAfterStatus.Finished)
  48. return;
  49. }
  50. if (component.Deleted || !IsTileClear())
  51. return;
  52. if (EntityManager.TryGetComponent(uid, out StackComponent? stackComp)
  53. && component.RemoveOnInteract && !_stackSystem.Use(uid, 1, stackComp))
  54. {
  55. return;
  56. }
  57. EntityManager.SpawnEntity(component.Prototype, args.ClickLocation.SnapToGrid(grid));
  58. if (component.RemoveOnInteract && stackComp == null)
  59. TryQueueDel(uid);
  60. }
  61. }
  62. }