AddUplinkCommand.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using Content.Server.Administration;
  2. using Content.Shared.Administration;
  3. using Content.Shared.CCVar;
  4. using Content.Shared.FixedPoint;
  5. using Robust.Server.Player;
  6. using Robust.Shared.Configuration;
  7. using Robust.Shared.Console;
  8. using Robust.Shared.Player;
  9. namespace Content.Server.Traitor.Uplink.Commands
  10. {
  11. [AdminCommand(AdminFlags.Admin)]
  12. public sealed class AddUplinkCommand : IConsoleCommand
  13. {
  14. [Dependency] private readonly IEntityManager _entManager = default!;
  15. [Dependency] private readonly IPlayerManager _playerManager = default!;
  16. public string Command => "adduplink";
  17. public string Description => Loc.GetString("add-uplink-command-description");
  18. public string Help => Loc.GetString("add-uplink-command-help");
  19. public CompletionResult GetCompletion(IConsoleShell shell, string[] args)
  20. {
  21. return args.Length switch
  22. {
  23. 1 => CompletionResult.FromHintOptions(CompletionHelper.SessionNames(), Loc.GetString("add-uplink-command-completion-1")),
  24. 2 => CompletionResult.FromHint(Loc.GetString("add-uplink-command-completion-2")),
  25. 3 => CompletionResult.FromHint(Loc.GetString("add-uplink-command-completion-3")),
  26. _ => CompletionResult.Empty
  27. };
  28. }
  29. public void Execute(IConsoleShell shell, string argStr, string[] args)
  30. {
  31. if (args.Length > 3)
  32. {
  33. shell.WriteError(Loc.GetString("shell-wrong-arguments-number"));
  34. return;
  35. }
  36. ICommonSession? session;
  37. if (args.Length > 0)
  38. {
  39. // Get player entity
  40. if (!_playerManager.TryGetSessionByUsername(args[0], out session))
  41. {
  42. shell.WriteLine(Loc.GetString("shell-target-player-does-not-exist"));
  43. return;
  44. }
  45. }
  46. else
  47. {
  48. session = shell.Player;
  49. }
  50. if (session?.AttachedEntity is not { } user)
  51. {
  52. shell.WriteLine(Loc.GetString("add-uplink-command-error-1"));
  53. return;
  54. }
  55. // Get target item
  56. EntityUid? uplinkEntity = null;
  57. if (args.Length >= 2)
  58. {
  59. if (!int.TryParse(args[1], out var itemID))
  60. {
  61. shell.WriteLine(Loc.GetString("shell-entity-uid-must-be-number"));
  62. return;
  63. }
  64. var eNet = new NetEntity(itemID);
  65. if (!_entManager.TryGetEntity(eNet, out var eUid))
  66. {
  67. shell.WriteLine(Loc.GetString("shell-invalid-entity-id"));
  68. return;
  69. }
  70. uplinkEntity = eUid;
  71. }
  72. bool isDiscounted = false;
  73. if (args.Length >= 3)
  74. {
  75. if (!bool.TryParse(args[2], out isDiscounted))
  76. {
  77. shell.WriteLine(Loc.GetString("shell-invalid-bool"));
  78. return;
  79. }
  80. }
  81. // Finally add uplink
  82. var uplinkSys = _entManager.System<UplinkSystem>();
  83. if (!uplinkSys.AddUplink(user, 20, uplinkEntity: uplinkEntity, giveDiscounts: isDiscounted))
  84. {
  85. shell.WriteLine(Loc.GetString("add-uplink-command-error-2"));
  86. }
  87. }
  88. }
  89. }