InvokeVerbCommand.cs 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using System.Linq;
  2. using Content.Server.Administration;
  3. using Content.Shared.Administration;
  4. using Content.Shared.Verbs;
  5. using Robust.Shared.Console;
  6. namespace Content.Server.Verbs.Commands
  7. {
  8. [AdminCommand(AdminFlags.Admin)]
  9. public sealed class InvokeVerbCommand : IConsoleCommand
  10. {
  11. [Dependency] private readonly IEntityManager _entManager = default!;
  12. public string Command => "invokeverb";
  13. public string Description => Loc.GetString("invoke-verb-command-description");
  14. public string Help => Loc.GetString("invoke-verb-command-help");
  15. public void Execute(IConsoleShell shell, string argStr, string[] args)
  16. {
  17. if (args.Length != 3)
  18. {
  19. shell.WriteLine(Loc.GetString("invoke-verb-command-invalid-args"));
  20. return;
  21. }
  22. var verbSystem = _entManager.System<SharedVerbSystem>();
  23. // get the 'player' entity (defaulting to command user, otherwise uses a uid)
  24. EntityUid? playerEntity = null;
  25. if (!int.TryParse(args[0], out var intPlayerUid))
  26. {
  27. if (args[0] == "self" && shell.Player?.AttachedEntity != null)
  28. {
  29. playerEntity = shell.Player.AttachedEntity.Value;
  30. }
  31. else
  32. {
  33. shell.WriteError(Loc.GetString("invoke-verb-command-invalid-player-uid"));
  34. return;
  35. }
  36. }
  37. else
  38. {
  39. _entManager.TryGetEntity(new NetEntity(intPlayerUid), out playerEntity);
  40. }
  41. // gets the target entity
  42. if (!int.TryParse(args[1], out var intUid))
  43. {
  44. shell.WriteError(Loc.GetString("invoke-verb-command-invalid-target-uid"));
  45. return;
  46. }
  47. if (playerEntity == null)
  48. {
  49. shell.WriteError(Loc.GetString("invoke-verb-command-invalid-player-entity"));
  50. return;
  51. }
  52. var targetNet = new NetEntity(intUid);
  53. if (!_entManager.TryGetEntity(targetNet, out var target))
  54. {
  55. shell.WriteError(Loc.GetString("invoke-verb-command-invalid-target-entity"));
  56. return;
  57. }
  58. var verbName = args[2].ToLowerInvariant();
  59. var verbs = verbSystem.GetLocalVerbs(target.Value, playerEntity.Value, Verb.VerbTypes, true);
  60. // if the "verb name" is actually a verb-type, try run any verb of that type.
  61. var verbType = Verb.VerbTypes.FirstOrDefault(x => x.Name == verbName);
  62. if (verbType != null)
  63. {
  64. var verb = verbs.FirstOrDefault(v => v.GetType() == verbType);
  65. if (verb != null)
  66. {
  67. verbSystem.ExecuteVerb(verb, playerEntity.Value, target.Value, forced: true);
  68. shell.WriteLine(Loc.GetString("invoke-verb-command-success", ("verb", verbName), ("target", target), ("player", playerEntity)));
  69. return;
  70. }
  71. }
  72. foreach (var verb in verbs)
  73. {
  74. if (verb.Text.ToLowerInvariant() == verbName)
  75. {
  76. verbSystem.ExecuteVerb(verb, playerEntity.Value, target.Value, forced: true);
  77. shell.WriteLine(Loc.GetString("invoke-verb-command-success", ("verb", verb.Text), ("target", target), ("player", playerEntity)));
  78. return;
  79. }
  80. }
  81. // found nothing
  82. shell.WriteError(Loc.GetString("invoke-verb-command-verb-not-found", ("verb", verbName), ("target", target)));
  83. }
  84. }
  85. }