TileReplaceCommand.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using Content.Server.Administration;
  2. using Content.Shared.Administration;
  3. using Robust.Shared.Console;
  4. using Robust.Shared.Map;
  5. using Robust.Shared.Map.Components;
  6. namespace Content.Server.Construction.Commands;
  7. [AdminCommand(AdminFlags.Mapping)]
  8. public sealed class TileReplaceCommand : IConsoleCommand
  9. {
  10. [Dependency] private readonly IEntityManager _entManager = default!;
  11. [Dependency] private readonly ITileDefinitionManager _tileDef = default!;
  12. // ReSharper disable once StringLiteralTypo
  13. public string Command => "tilereplace";
  14. public string Description => "Replaces one tile with another.";
  15. public string Help => $"Usage: {Command} [<gridId>] <src> <dst>";
  16. public void Execute(IConsoleShell shell, string argStr, string[] args)
  17. {
  18. var player = shell.Player;
  19. EntityUid? gridId;
  20. string tileIdA;
  21. string tileIdB;
  22. switch (args.Length)
  23. {
  24. case 2:
  25. if (player?.AttachedEntity is not { Valid: true } playerEntity)
  26. {
  27. shell.WriteError("Only a player can run this command without a grid ID.");
  28. return;
  29. }
  30. gridId = _entManager.GetComponent<TransformComponent>(playerEntity).GridUid;
  31. tileIdA = args[0];
  32. tileIdB = args[1];
  33. break;
  34. case 3:
  35. if (!NetEntity.TryParse(args[0], out var idNet) ||
  36. !_entManager.TryGetEntity(idNet, out var id))
  37. {
  38. shell.WriteError($"{args[0]} is not a valid entity.");
  39. return;
  40. }
  41. gridId = id;
  42. tileIdA = args[1];
  43. tileIdB = args[2];
  44. break;
  45. default:
  46. shell.WriteLine(Help);
  47. return;
  48. }
  49. var tileA = _tileDef[tileIdA];
  50. var tileB = _tileDef[tileIdB];
  51. if (!_entManager.TryGetComponent(gridId, out MapGridComponent? grid))
  52. {
  53. shell.WriteError($"No grid exists with id {gridId}");
  54. return;
  55. }
  56. if (!_entManager.EntityExists(gridId))
  57. {
  58. shell.WriteError($"Grid {gridId} doesn't have an associated grid entity.");
  59. return;
  60. }
  61. var mapSystem = _entManager.System<SharedMapSystem>();
  62. var changed = 0;
  63. foreach (var tile in mapSystem.GetAllTiles(gridId.Value, grid))
  64. {
  65. var tileContent = tile.Tile;
  66. if (tileContent.TypeId == tileA.TileId)
  67. {
  68. mapSystem.SetTile(gridId.Value, grid, tile.GridIndices, new Tile(tileB.TileId));
  69. changed++;
  70. }
  71. }
  72. shell.WriteLine($"Changed {changed} tiles.");
  73. }
  74. }