HeadsetSystem.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using Content.Server.Chat.Systems;
  2. using Content.Server.Emp;
  3. using Content.Server.Radio.Components;
  4. using Content.Shared.Inventory.Events;
  5. using Content.Shared.Radio;
  6. using Content.Shared.Radio.Components;
  7. using Content.Shared.Radio.EntitySystems;
  8. using Robust.Shared.Network;
  9. using Robust.Shared.Player;
  10. namespace Content.Server.Radio.EntitySystems;
  11. public sealed class HeadsetSystem : SharedHeadsetSystem
  12. {
  13. [Dependency] private readonly INetManager _netMan = default!;
  14. [Dependency] private readonly RadioSystem _radio = default!;
  15. public override void Initialize()
  16. {
  17. base.Initialize();
  18. SubscribeLocalEvent<HeadsetComponent, RadioReceiveEvent>(OnHeadsetReceive);
  19. SubscribeLocalEvent<HeadsetComponent, EncryptionChannelsChangedEvent>(OnKeysChanged);
  20. SubscribeLocalEvent<WearingHeadsetComponent, EntitySpokeEvent>(OnSpeak);
  21. SubscribeLocalEvent<HeadsetComponent, EmpPulseEvent>(OnEmpPulse);
  22. }
  23. private void OnKeysChanged(EntityUid uid, HeadsetComponent component, EncryptionChannelsChangedEvent args)
  24. {
  25. UpdateRadioChannels(uid, component, args.Component);
  26. }
  27. private void UpdateRadioChannels(EntityUid uid, HeadsetComponent headset, EncryptionKeyHolderComponent? keyHolder = null)
  28. {
  29. // make sure to not add ActiveRadioComponent when headset is being deleted
  30. if (!headset.Enabled || MetaData(uid).EntityLifeStage >= EntityLifeStage.Terminating)
  31. return;
  32. if (!Resolve(uid, ref keyHolder))
  33. return;
  34. if (keyHolder.Channels.Count == 0)
  35. RemComp<ActiveRadioComponent>(uid);
  36. else
  37. EnsureComp<ActiveRadioComponent>(uid).Channels = new(keyHolder.Channels);
  38. }
  39. private void OnSpeak(EntityUid uid, WearingHeadsetComponent component, EntitySpokeEvent args)
  40. {
  41. if (args.Channel != null
  42. && TryComp(component.Headset, out EncryptionKeyHolderComponent? keys)
  43. && keys.Channels.Contains(args.Channel.ID))
  44. {
  45. _radio.SendRadioMessage(uid, args.Message, args.Channel, component.Headset);
  46. args.Channel = null; // prevent duplicate messages from other listeners.
  47. }
  48. }
  49. protected override void OnGotEquipped(EntityUid uid, HeadsetComponent component, GotEquippedEvent args)
  50. {
  51. base.OnGotEquipped(uid, component, args);
  52. if (component.IsEquipped && component.Enabled)
  53. {
  54. EnsureComp<WearingHeadsetComponent>(args.Equipee).Headset = uid;
  55. UpdateRadioChannels(uid, component);
  56. }
  57. }
  58. protected override void OnGotUnequipped(EntityUid uid, HeadsetComponent component, GotUnequippedEvent args)
  59. {
  60. base.OnGotUnequipped(uid, component, args);
  61. component.IsEquipped = false;
  62. RemComp<ActiveRadioComponent>(uid);
  63. RemComp<WearingHeadsetComponent>(args.Equipee);
  64. }
  65. public void SetEnabled(EntityUid uid, bool value, HeadsetComponent? component = null)
  66. {
  67. if (!Resolve(uid, ref component))
  68. return;
  69. if (component.Enabled == value)
  70. return;
  71. if (!value)
  72. {
  73. RemCompDeferred<ActiveRadioComponent>(uid);
  74. if (component.IsEquipped)
  75. RemCompDeferred<WearingHeadsetComponent>(Transform(uid).ParentUid);
  76. }
  77. else if (component.IsEquipped)
  78. {
  79. EnsureComp<WearingHeadsetComponent>(Transform(uid).ParentUid).Headset = uid;
  80. UpdateRadioChannels(uid, component);
  81. }
  82. }
  83. private void OnHeadsetReceive(EntityUid uid, HeadsetComponent component, ref RadioReceiveEvent args)
  84. {
  85. if (TryComp(Transform(uid).ParentUid, out ActorComponent? actor))
  86. _netMan.ServerSendMessage(args.ChatMsg, actor.PlayerSession.Channel);
  87. }
  88. private void OnEmpPulse(EntityUid uid, HeadsetComponent component, ref EmpPulseEvent args)
  89. {
  90. if (component.Enabled)
  91. {
  92. args.Affected = true;
  93. args.Disabled = true;
  94. }
  95. }
  96. }