DeleteGasCommand.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. using Content.Server.Administration;
  2. using Content.Server.Atmos.EntitySystems;
  3. using Content.Shared.Administration;
  4. using Content.Shared.Atmos;
  5. using Robust.Shared.Console;
  6. using Robust.Shared.Map.Components;
  7. namespace Content.Server.Atmos.Commands
  8. {
  9. [AdminCommand(AdminFlags.Debug)]
  10. public sealed class DeleteGasCommand : IConsoleCommand
  11. {
  12. [Dependency] private readonly IEntityManager _entManager = default!;
  13. public string Command => "deletegas";
  14. public string Description => "Removes all gases from a grid, or just of one type if specified.";
  15. public string Help => $"Usage: {Command} <GridId> <Gas> / {Command} <GridId> / {Command} <Gas> / {Command}";
  16. public void Execute(IConsoleShell shell, string argStr, string[] args)
  17. {
  18. var player = shell.Player;
  19. EntityUid? gridId;
  20. Gas? gas = null;
  21. switch (args.Length)
  22. {
  23. case 0:
  24. {
  25. if (player == null)
  26. {
  27. shell.WriteLine("A grid must be specified when the command isn't used by a player.");
  28. return;
  29. }
  30. if (player.AttachedEntity is not {Valid: true} playerEntity)
  31. {
  32. shell.WriteLine("You have no entity to get a grid from.");
  33. return;
  34. }
  35. gridId = _entManager.GetComponent<TransformComponent>(playerEntity).GridUid;
  36. if (gridId == null)
  37. {
  38. shell.WriteLine("You aren't on a grid to delete gas from.");
  39. return;
  40. }
  41. break;
  42. }
  43. case 1:
  44. {
  45. if (!NetEntity.TryParse(args[0], out var numberEnt) || !_entManager.TryGetEntity(numberEnt, out var number))
  46. {
  47. // Argument is a gas
  48. if (player == null)
  49. {
  50. shell.WriteLine("A grid id must be specified if not using this command as a player.");
  51. return;
  52. }
  53. if (player.AttachedEntity is not {Valid: true} playerEntity)
  54. {
  55. shell.WriteLine("You have no entity from which to get a grid id.");
  56. return;
  57. }
  58. gridId = _entManager.GetComponent<TransformComponent>(playerEntity).GridUid;
  59. if (gridId == null)
  60. {
  61. shell.WriteLine("You aren't on a grid to delete gas from.");
  62. return;
  63. }
  64. if (!Enum.TryParse<Gas>(args[0], true, out var parsedGas))
  65. {
  66. shell.WriteLine($"{args[0]} is not a valid gas name.");
  67. return;
  68. }
  69. gas = parsedGas;
  70. break;
  71. }
  72. // Argument is a grid
  73. gridId = number;
  74. break;
  75. }
  76. case 2:
  77. {
  78. if (!NetEntity.TryParse(args[0], out var firstNet) || !_entManager.TryGetEntity(firstNet, out var first))
  79. {
  80. shell.WriteLine($"{args[0]} is not a valid integer for a grid id.");
  81. return;
  82. }
  83. gridId = first;
  84. if (gridId.Value.IsValid())
  85. {
  86. shell.WriteLine($"{gridId} is not a valid grid id.");
  87. return;
  88. }
  89. if (!Enum.TryParse<Gas>(args[1], true, out var parsedGas))
  90. {
  91. shell.WriteLine($"{args[1]} is not a valid gas.");
  92. return;
  93. }
  94. gas = parsedGas;
  95. break;
  96. }
  97. default:
  98. shell.WriteLine(Help);
  99. return;
  100. }
  101. if (!_entManager.TryGetComponent<MapGridComponent>(gridId, out _))
  102. {
  103. shell.WriteLine($"No grid exists with id {gridId}");
  104. return;
  105. }
  106. var atmosphereSystem = _entManager.System<AtmosphereSystem>();
  107. var tiles = 0;
  108. var moles = 0f;
  109. if (gas == null)
  110. {
  111. foreach (var tile in atmosphereSystem.GetAllMixtures(gridId.Value, true))
  112. {
  113. if (tile.Immutable)
  114. continue;
  115. tiles++;
  116. moles += tile.TotalMoles;
  117. tile.Clear();
  118. }
  119. }
  120. else
  121. {
  122. foreach (var tile in atmosphereSystem.GetAllMixtures(gridId.Value, true))
  123. {
  124. if (tile.Immutable)
  125. continue;
  126. tiles++;
  127. moles += tile.TotalMoles;
  128. tile.SetMoles(gas.Value, 0);
  129. }
  130. }
  131. if (gas == null)
  132. {
  133. shell.WriteLine($"Removed {moles} moles from {tiles} tiles.");
  134. return;
  135. }
  136. shell.WriteLine($"Removed {moles} moles of gas {gas} from {tiles} tiles.");
  137. }
  138. }
  139. }