1
0

MOTDSystem.cs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using Content.Server.Chat.Managers;
  2. using Content.Server.GameTicking;
  3. using Content.Shared.CCVar;
  4. using Content.Shared.Chat;
  5. using Robust.Shared.Console;
  6. using Robust.Shared.Configuration;
  7. using Robust.Shared.Player;
  8. namespace Content.Server.Motd;
  9. /// <summary>
  10. /// The system that handles broadcasting the Message Of The Day to players when they join the lobby/the MOTD changes/they ask for it to be printed.
  11. /// </summary>
  12. public sealed class MOTDSystem : EntitySystem
  13. {
  14. [Dependency] private readonly IChatManager _chatManager = default!;
  15. [Dependency] private readonly IConfigurationManager _configurationManager = default!;
  16. /// <summary>
  17. /// The cached value of the Message of the Day. Used for fast access.
  18. /// </summary>
  19. private string _messageOfTheDay = "";
  20. public override void Initialize()
  21. {
  22. base.Initialize();
  23. Subs.CVar(_configurationManager, CCVars.MOTD, OnMOTDChanged, invokeImmediately: true);
  24. SubscribeLocalEvent<PlayerJoinedLobbyEvent>(OnPlayerJoinedLobby);
  25. }
  26. /// <summary>
  27. /// Sends the Message Of The Day, if any, to all connected players.
  28. /// </summary>
  29. public void TrySendMOTD()
  30. {
  31. if (string.IsNullOrEmpty(_messageOfTheDay))
  32. return;
  33. var wrappedMessage = Loc.GetString("motd-wrap-message", ("motd", _messageOfTheDay));
  34. _chatManager.ChatMessageToAll(ChatChannel.Server, _messageOfTheDay, wrappedMessage, source: EntityUid.Invalid, hideChat: false, recordReplay: true);
  35. }
  36. /// <summary>
  37. /// Sends the Message Of The Day, if any, to a specific player.
  38. /// </summary>
  39. public void TrySendMOTD(ICommonSession player)
  40. {
  41. if (string.IsNullOrEmpty(_messageOfTheDay))
  42. return;
  43. var wrappedMessage = Loc.GetString("motd-wrap-message", ("motd", _messageOfTheDay));
  44. _chatManager.ChatMessageToOne(ChatChannel.Server, _messageOfTheDay, wrappedMessage, source: EntityUid.Invalid, hideChat: false, client: player.Channel);
  45. }
  46. /// <summary>
  47. /// Sends the Message Of The Day, if any, to a specific player's console and chat.
  48. /// </summary>
  49. /// <remarks>
  50. /// This is used by the MOTD console command because we can't tell whether the player is using `console or /console so we send the message to both.
  51. /// </remarks>
  52. public void TrySendMOTD(IConsoleShell shell)
  53. {
  54. if (string.IsNullOrEmpty(_messageOfTheDay))
  55. return;
  56. var wrappedMessage = Loc.GetString("motd-wrap-message", ("motd", _messageOfTheDay));
  57. shell.WriteLine(wrappedMessage);
  58. if (shell.Player is { } player)
  59. _chatManager.ChatMessageToOne(ChatChannel.Server, _messageOfTheDay, wrappedMessage, source: EntityUid.Invalid, hideChat: false, client: player.Channel);
  60. }
  61. #region Event Handlers
  62. /// <summary>
  63. /// Posts the Message Of The Day to any players who join the lobby.
  64. /// </summary>
  65. private void OnPlayerJoinedLobby(PlayerJoinedLobbyEvent ev)
  66. {
  67. TrySendMOTD(ev.PlayerSession);
  68. }
  69. /// <summary>
  70. /// Broadcasts changes to the Message Of The Day to all players.
  71. /// </summary>
  72. private void OnMOTDChanged(string val)
  73. {
  74. if (val == _messageOfTheDay)
  75. return;
  76. _messageOfTheDay = val;
  77. TrySendMOTD();
  78. }
  79. #endregion Event Handlers
  80. }