SandboxSystem.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. using Content.Client.Administration.Managers;
  2. using Content.Client.Movement.Systems;
  3. using Content.Shared.Sandbox;
  4. using Robust.Client.Console;
  5. using Robust.Client.Placement;
  6. using Robust.Client.Placement.Modes;
  7. using Robust.Shared.Map;
  8. using Robust.Shared.Player;
  9. namespace Content.Client.Sandbox
  10. {
  11. public sealed class SandboxSystem : SharedSandboxSystem
  12. {
  13. [Dependency] private readonly IClientAdminManager _adminManager = default!;
  14. [Dependency] private readonly IClientConsoleHost _consoleHost = default!;
  15. [Dependency] private readonly IMapManager _map = default!;
  16. [Dependency] private readonly IPlacementManager _placement = default!;
  17. [Dependency] private readonly ContentEyeSystem _contentEye = default!;
  18. [Dependency] private readonly SharedTransformSystem _transform = default!;
  19. [Dependency] private readonly SharedMapSystem _mapSystem = default!;
  20. private bool _sandboxEnabled;
  21. public bool SandboxAllowed { get; private set; }
  22. public event Action? SandboxEnabled;
  23. public event Action? SandboxDisabled;
  24. public override void Initialize()
  25. {
  26. _adminManager.AdminStatusUpdated += CheckStatus;
  27. SubscribeNetworkEvent<MsgSandboxStatus>(OnSandboxStatus);
  28. }
  29. private void CheckStatus()
  30. {
  31. var currentStatus = _sandboxEnabled || _adminManager.IsActive();
  32. if (currentStatus == SandboxAllowed)
  33. return;
  34. SandboxAllowed = currentStatus;
  35. if (SandboxAllowed)
  36. {
  37. SandboxEnabled?.Invoke();
  38. }
  39. else
  40. {
  41. SandboxDisabled?.Invoke();
  42. }
  43. }
  44. public override void Shutdown()
  45. {
  46. _adminManager.AdminStatusUpdated -= CheckStatus;
  47. base.Shutdown();
  48. }
  49. private void OnSandboxStatus(MsgSandboxStatus ev)
  50. {
  51. SetAllowed(ev.SandboxAllowed);
  52. }
  53. private void SetAllowed(bool sandboxEnabled)
  54. {
  55. _sandboxEnabled = sandboxEnabled;
  56. CheckStatus();
  57. }
  58. public void Respawn()
  59. {
  60. RaiseNetworkEvent(new MsgSandboxRespawn());
  61. }
  62. public void GiveAdminAccess()
  63. {
  64. RaiseNetworkEvent(new MsgSandboxGiveAccess());
  65. }
  66. public void GiveAGhost()
  67. {
  68. RaiseNetworkEvent(new MsgSandboxGiveAghost());
  69. }
  70. public void Suicide()
  71. {
  72. RaiseNetworkEvent(new MsgSandboxSuicide());
  73. }
  74. public bool Copy(ICommonSession? session, EntityCoordinates coords, EntityUid uid)
  75. {
  76. if (!SandboxAllowed)
  77. return false;
  78. // Try copy entity.
  79. if (uid.IsValid()
  80. && EntityManager.TryGetComponent(uid, out MetaDataComponent? comp)
  81. && !comp.EntityDeleted)
  82. {
  83. if (comp.EntityPrototype == null || comp.EntityPrototype.HideSpawnMenu || comp.EntityPrototype.Abstract)
  84. return false;
  85. if (_placement.Eraser)
  86. _placement.ToggleEraser();
  87. _placement.BeginPlacing(new()
  88. {
  89. EntityType = comp.EntityPrototype.ID,
  90. IsTile = false,
  91. TileType = 0,
  92. PlacementOption = comp.EntityPrototype.PlacementMode
  93. });
  94. return true;
  95. }
  96. // Try copy tile.
  97. if (!_map.TryFindGridAt(_transform.ToMapCoordinates(coords), out var gridUid, out var grid) || !_mapSystem.TryGetTileRef(gridUid, grid, coords, out var tileRef))
  98. return false;
  99. if (_placement.Eraser)
  100. _placement.ToggleEraser();
  101. _placement.BeginPlacing(new()
  102. {
  103. EntityType = null,
  104. IsTile = true,
  105. TileType = tileRef.Tile.TypeId,
  106. PlacementOption = nameof(AlignTileAny)
  107. });
  108. return true;
  109. }
  110. // TODO: need to cleanup these
  111. public void ToggleLight()
  112. {
  113. _consoleHost.ExecuteCommand("togglelight");
  114. }
  115. public void ToggleFov()
  116. {
  117. _contentEye.RequestToggleFov();
  118. }
  119. public void ToggleShadows()
  120. {
  121. _consoleHost.ExecuteCommand("toggleshadows");
  122. }
  123. public void ToggleSubFloor()
  124. {
  125. _consoleHost.ExecuteCommand("showsubfloor");
  126. }
  127. public void ShowMarkers()
  128. {
  129. _consoleHost.ExecuteCommand("showmarkers");
  130. }
  131. public void ShowBb()
  132. {
  133. _consoleHost.ExecuteCommand("physics shapes");
  134. }
  135. }
  136. }