WeatherSystem.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using Content.Server.Administration;
  2. using Content.Shared.Administration;
  3. using Content.Shared.Weather;
  4. using Robust.Shared.Console;
  5. using Robust.Shared.GameStates;
  6. using Robust.Shared.Map;
  7. using System.Linq;
  8. namespace Content.Server.Weather;
  9. public sealed class WeatherSystem : SharedWeatherSystem
  10. {
  11. [Dependency] private readonly IConsoleHost _console = default!;
  12. [Dependency] private readonly SharedMapSystem _mapSystem = default!;
  13. public override void Initialize()
  14. {
  15. base.Initialize();
  16. SubscribeLocalEvent<WeatherComponent, ComponentGetState>(OnWeatherGetState);
  17. _console.RegisterCommand("weather",
  18. Loc.GetString("cmd-weather-desc"),
  19. Loc.GetString("cmd-weather-help"),
  20. WeatherTwo,
  21. WeatherCompletion);
  22. }
  23. private void OnWeatherGetState(EntityUid uid, WeatherComponent component, ref ComponentGetState args)
  24. {
  25. args.State = new WeatherComponentState(component.Weather);
  26. }
  27. [AdminCommand(AdminFlags.Fun)]
  28. private void WeatherTwo(IConsoleShell shell, string argStr, string[] args)
  29. {
  30. if (args.Length < 2)
  31. {
  32. shell.WriteError(Loc.GetString("cmd-weather-error-no-arguments"));
  33. return;
  34. }
  35. if (!int.TryParse(args[0], out var mapInt))
  36. return;
  37. var mapId = new MapId(mapInt);
  38. if (!MapManager.MapExists(mapId))
  39. return;
  40. if (!_mapSystem.TryGetMap(mapId, out var mapUid))
  41. return;
  42. var weatherComp = EnsureComp<WeatherComponent>(mapUid.Value);
  43. //Weather Proto parsing
  44. WeatherPrototype? weather = null;
  45. if (!args[1].Equals("null"))
  46. {
  47. if (!ProtoMan.TryIndex(args[1], out weather))
  48. {
  49. shell.WriteError(Loc.GetString("cmd-weather-error-unknown-proto"));
  50. return;
  51. }
  52. }
  53. //Time parsing
  54. TimeSpan? endTime = null;
  55. if (args.Length == 3)
  56. {
  57. var curTime = Timing.CurTime;
  58. if (int.TryParse(args[2], out var durationInt))
  59. {
  60. endTime = curTime + TimeSpan.FromSeconds(durationInt);
  61. }
  62. else
  63. {
  64. shell.WriteError(Loc.GetString("cmd-weather-error-wrong-time"));
  65. }
  66. }
  67. SetWeather(mapId, weather, endTime);
  68. }
  69. private CompletionResult WeatherCompletion(IConsoleShell shell, string[] args)
  70. {
  71. if (args.Length == 1)
  72. return CompletionResult.FromHintOptions(CompletionHelper.MapIds(EntityManager), "Map Id");
  73. var a = CompletionHelper.PrototypeIDs<WeatherPrototype>(true, ProtoMan);
  74. var b = a.Concat(new[] { new CompletionOption("null", Loc.GetString("cmd-weather-null")) });
  75. return CompletionResult.FromHintOptions(b, Loc.GetString("cmd-weather-hint"));
  76. }
  77. }