AddDecalCommand.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using System.Numerics;
  2. using Content.Server.Administration;
  3. using Content.Shared.Administration;
  4. using Content.Shared.Decals;
  5. using Content.Shared.Maps;
  6. using Robust.Server.GameObjects;
  7. using Robust.Shared.Console;
  8. using Robust.Shared.Map;
  9. using Robust.Shared.Map.Components;
  10. using Robust.Shared.Prototypes;
  11. namespace Content.Server.Decals.Commands
  12. {
  13. [AdminCommand(AdminFlags.Mapping)]
  14. public sealed class AddDecalCommand : IConsoleCommand
  15. {
  16. [Dependency] private readonly IEntityManager _entManager = default!;
  17. [Dependency] private readonly IPrototypeManager _protoManager = default!;
  18. public string Command => "adddecal";
  19. public string Description => "Creates a decal on the map";
  20. public string Help => $"{Command} <id> <x position> <y position> <gridId> [angle=<angle> zIndex=<zIndex> color=<color>]";
  21. public void Execute(IConsoleShell shell, string argStr, string[] args)
  22. {
  23. if (args.Length < 4 || args.Length > 7)
  24. {
  25. shell.WriteError($"Received invalid amount of arguments arguments. Expected 4 to 7, got {args.Length}.\nUsage: {Help}");
  26. return;
  27. }
  28. if (!_protoManager.HasIndex<DecalPrototype>(args[0]))
  29. {
  30. shell.WriteError($"Cannot find decalprototype '{args[0]}'.");
  31. }
  32. if (!float.TryParse(args[1], out var x))
  33. {
  34. shell.WriteError($"Failed parsing x-coordinate '{args[1]}'.");
  35. return;
  36. }
  37. if (!float.TryParse(args[2], out var y))
  38. {
  39. shell.WriteError($"Failed parsing y-coordinate'{args[2]}'.");
  40. return;
  41. }
  42. if (!NetEntity.TryParse(args[3], out var gridIdNet) ||
  43. !_entManager.TryGetEntity(gridIdNet, out var gridIdRaw) ||
  44. !_entManager.TryGetComponent(gridIdRaw, out MapGridComponent? grid))
  45. {
  46. shell.WriteError($"Failed parsing gridId '{args[3]}'.");
  47. return;
  48. }
  49. var mapSystem = _entManager.System<MapSystem>();
  50. var coordinates = new EntityCoordinates(gridIdRaw.Value, new Vector2(x, y));
  51. if (mapSystem.GetTileRef(gridIdRaw.Value, grid, coordinates).IsSpace())
  52. {
  53. shell.WriteError($"Cannot create decal on space tile at {coordinates}.");
  54. return;
  55. }
  56. Color? color = null;
  57. var zIndex = 0;
  58. Angle? rotation = null;
  59. if (args.Length > 4)
  60. {
  61. for (int i = 4; i < args.Length; i++)
  62. {
  63. var rawValue = args[i].Split('=');
  64. if (rawValue.Length != 2)
  65. {
  66. shell.WriteError($"Failed parsing parameter: '{args[i]}'");
  67. return;
  68. }
  69. switch (rawValue[0])
  70. {
  71. case "angle":
  72. if (!double.TryParse(rawValue[1], out var degrees))
  73. {
  74. shell.WriteError($"Failed parsing angle '{rawValue[1]}'.");
  75. return;
  76. }
  77. rotation = Angle.FromDegrees(degrees);
  78. break;
  79. case "zIndex":
  80. if (!int.TryParse(rawValue[1], out zIndex))
  81. {
  82. shell.WriteError($"Failed parsing zIndex '{rawValue[1]}'.");
  83. return;
  84. }
  85. break;
  86. case "color":
  87. if (!Color.TryFromName(rawValue[1], out var colorRaw))
  88. {
  89. shell.WriteError($"Failed parsing color '{rawValue[1]}'.");
  90. return;
  91. }
  92. color = colorRaw;
  93. break;
  94. default:
  95. shell.WriteError($"Unknown parameter key '{rawValue[0]}'.");
  96. return;
  97. }
  98. }
  99. }
  100. if (_entManager.System<DecalSystem>().TryAddDecal(args[0], coordinates, out var uid, color, rotation, zIndex))
  101. {
  102. shell.WriteLine($"Successfully created decal {uid}.");
  103. }
  104. else
  105. {
  106. shell.WriteError($"Failed adding decal.");
  107. }
  108. }
  109. }
  110. }