1
0

SetNutrit.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using Content.Server.Administration;
  2. using Content.Server.Database.Migrations.Postgres;
  3. using Content.Shared.Administration;
  4. using Content.Shared.Nutrition.Components;
  5. using Content.Shared.Nutrition.EntitySystems;
  6. using Robust.Shared.Console;
  7. using System.Linq;
  8. namespace Content.Server.Nutrition;
  9. [AdminCommand(AdminFlags.Debug)]
  10. public sealed class SetNutrit : LocalizedEntityCommands
  11. {
  12. public override string Command => "setnutrit";
  13. public override void Execute(IConsoleShell shell, string argStr, string[] args)
  14. {
  15. var player = shell.Player;
  16. if (player == null)
  17. {
  18. shell.WriteError(Loc.GetString("cmd-nutrition-error-player"));
  19. return;
  20. }
  21. if (player.AttachedEntity is not { Valid: true } playerEntity)
  22. {
  23. shell.WriteError(Loc.GetString("cmd-nutrition-error-entity"));
  24. return;
  25. }
  26. if (args.Length != 2)
  27. {
  28. shell.WriteError(Loc.GetString("shell-wrong-arguments-number-need-specific",
  29. ("properAmount", 2),
  30. ("currentAmount", args.Length)
  31. ));
  32. return;
  33. }
  34. var systemString = args[0];
  35. switch (systemString)
  36. {
  37. case "hunger":
  38. {
  39. if (!EntityManager.TryGetComponent(playerEntity, out HungerComponent? hunger))
  40. {
  41. shell.WriteError(Loc.GetString("cmd-nutrition-error-component", ("comp", nameof(HungerComponent))));
  42. return;
  43. }
  44. if (!Enum.TryParse(args[1], out HungerThreshold hungerThreshold))
  45. {
  46. shell.WriteError(Loc.GetString("cmd-setnutrit-error-invalid-threshold",
  47. ("thresholdType", nameof(HungerThreshold)),
  48. ("thresholdString", args[1])
  49. ));
  50. return;
  51. }
  52. var hungerValue = hunger.Thresholds[hungerThreshold];
  53. EntityManager.System<HungerSystem>().SetHunger(playerEntity, hungerValue, hunger);
  54. return;
  55. }
  56. case "thirst":
  57. {
  58. if (!EntityManager.TryGetComponent(playerEntity, out ThirstComponent? thirst))
  59. {
  60. shell.WriteError(Loc.GetString("cmd-nutrition-error-component", ("comp", nameof(ThirstComponent))));
  61. return;
  62. }
  63. if (!Enum.TryParse(args[1], out ThirstThreshold thirstThreshold))
  64. {
  65. shell.WriteError(Loc.GetString("cmd-setnutrit-error-invalid-threshold",
  66. ("thresholdType", nameof(ThirstThreshold)),
  67. ("thresholdString", args[1])
  68. ));
  69. return;
  70. }
  71. var thirstValue = thirst.ThirstThresholds[thirstThreshold];
  72. EntityManager.System<ThirstSystem>().SetThirst(playerEntity, thirst, thirstValue);
  73. return;
  74. }
  75. default:
  76. {
  77. shell.WriteError($"invalid nutrition system ${systemString}");
  78. return;
  79. }
  80. }
  81. }
  82. public override CompletionResult GetCompletion(IConsoleShell shell, string[] args)
  83. {
  84. switch (args.Length)
  85. {
  86. case 1:
  87. {
  88. string[] kinds = { "hunger", "thirst" };
  89. return CompletionResult.FromHintOptions(kinds, "nutrition system");
  90. }
  91. case 2:
  92. {
  93. return args[0] switch
  94. {
  95. "hunger" => CompletionResult.FromHintOptions(Enum.GetNames<HungerThreshold>(), nameof(HungerThreshold)),
  96. "thirst" => CompletionResult.FromHintOptions(Enum.GetNames<ThirstThreshold>(), nameof(ThirstThreshold)),
  97. _ => CompletionResult.Empty,
  98. };
  99. }
  100. default:
  101. return CompletionResult.Empty;
  102. }
  103. }
  104. }