1
0

AddObjectiveCommand.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using System.Linq;
  2. using Content.Server.Administration;
  3. using Content.Shared.Administration;
  4. using Content.Shared.Mind;
  5. using Content.Shared.Objectives.Components;
  6. using Content.Shared.Prototypes;
  7. using Robust.Server.Player;
  8. using Robust.Shared.Console;
  9. using Robust.Shared.Prototypes;
  10. namespace Content.Server.Objectives.Commands;
  11. [AdminCommand(AdminFlags.Admin)]
  12. public sealed class AddObjectiveCommand : LocalizedEntityCommands
  13. {
  14. [Dependency] private readonly IPlayerManager _players = default!;
  15. [Dependency] private readonly IPrototypeManager _prototypes = default!;
  16. [Dependency] private readonly SharedMindSystem _mind = default!;
  17. [Dependency] private readonly ObjectivesSystem _objectives = default!;
  18. public override string Command => "addobjective";
  19. public override void Execute(IConsoleShell shell, string argStr, string[] args)
  20. {
  21. if (args.Length != 2)
  22. {
  23. shell.WriteError(Loc.GetString(Loc.GetString("cmd-addobjective-invalid-args")));
  24. return;
  25. }
  26. if (!_players.TryGetSessionByUsername(args[0], out var data))
  27. {
  28. shell.WriteError(Loc.GetString("cmd-addobjective-player-not-found"));
  29. return;
  30. }
  31. if (!_mind.TryGetMind(data, out var mindId, out var mind))
  32. {
  33. shell.WriteError(Loc.GetString("cmd-addobjective-mind-not-found"));
  34. return;
  35. }
  36. if (!_prototypes.TryIndex<EntityPrototype>(args[1], out var proto) ||
  37. !proto.HasComponent<ObjectiveComponent>())
  38. {
  39. shell.WriteError(Loc.GetString("cmd-addobjective-objective-not-found", ("obj", args[1])));
  40. return;
  41. }
  42. if (!_mind.TryAddObjective(mindId, mind, args[1]))
  43. {
  44. // can fail for other reasons so dont pretend to be right
  45. shell.WriteError(Loc.GetString("cmd-addobjective-adding-failed"));
  46. }
  47. }
  48. public override CompletionResult GetCompletion(IConsoleShell shell, string[] args)
  49. {
  50. if (args.Length == 1)
  51. {
  52. var options = _players.Sessions.OrderBy(c => c.Name).Select(c => c.Name).ToArray();
  53. return CompletionResult.FromHintOptions(options, Loc.GetString("cmd-addobjective-player-completion"));
  54. }
  55. if (args.Length != 2)
  56. return CompletionResult.Empty;
  57. return CompletionResult.FromHintOptions(
  58. _objectives.Objectives(),
  59. Loc.GetString(Loc.GetString("cmd-add-objective-obj-completion")));
  60. }
  61. }