Files
OldThink/Content.Server/Radio/EntitySystems/HeadsetSystem.cs

115 lines
4.0 KiB
C#
Raw Normal View History

2022-11-15 17:09:27 +13:00
using Content.Server.Chat.Systems;
using Content.Server.Emp;
2023-02-19 06:27:56 +13:00
using Content.Server.Radio.Components;
2022-11-15 17:09:27 +13:00
using Content.Shared.Inventory.Events;
using Content.Shared.Radio;
2023-02-19 06:27:56 +13:00
using Content.Shared.Radio.Components;
using Content.Shared.Radio.EntitySystems;
2022-11-15 17:09:27 +13:00
using Robust.Shared.Network;
2023-10-29 04:21:02 +11:00
using Robust.Shared.Player;
2022-11-15 17:09:27 +13:00
namespace Content.Server.Radio.EntitySystems;
2023-02-19 06:27:56 +13:00
public sealed class HeadsetSystem : SharedHeadsetSystem
2022-11-15 17:09:27 +13:00
{
[Dependency] private readonly INetManager _netMan = default!;
[Dependency] private readonly RadioSystem _radio = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<HeadsetComponent, RadioReceiveEvent>(OnHeadsetReceive);
2023-02-19 06:27:56 +13:00
SubscribeLocalEvent<HeadsetComponent, EncryptionChannelsChangedEvent>(OnKeysChanged);
2022-11-15 17:09:27 +13:00
SubscribeLocalEvent<WearingHeadsetComponent, EntitySpokeEvent>(OnSpeak);
SubscribeLocalEvent<HeadsetComponent, EmpPulseEvent>(OnEmpPulse);
2023-02-19 06:27:56 +13:00
}
private void OnKeysChanged(EntityUid uid, HeadsetComponent component, EncryptionChannelsChangedEvent args)
{
UpdateRadioChannels(uid, component, args.Component);
}
private void UpdateRadioChannels(EntityUid uid, HeadsetComponent headset, EncryptionKeyHolderComponent? keyHolder = null)
{
// make sure to not add ActiveRadioComponent when headset is being deleted
if (!headset.Enabled || MetaData(uid).EntityLifeStage >= EntityLifeStage.Terminating)
2023-02-19 06:27:56 +13:00
return;
if (!Resolve(uid, ref keyHolder))
return;
2023-01-29 00:53:08 +00:00
2023-02-19 06:27:56 +13:00
if (keyHolder.Channels.Count == 0)
RemComp<ActiveRadioComponent>(uid);
else
EnsureComp<ActiveRadioComponent>(uid).Channels = new(keyHolder.Channels);
2022-11-15 17:09:27 +13:00
}
private void OnSpeak(EntityUid uid, WearingHeadsetComponent component, EntitySpokeEvent args)
{
if (args.Channel != null
2023-02-19 06:27:56 +13:00
&& TryComp(component.Headset, out EncryptionKeyHolderComponent? keys)
&& keys.Channels.Contains(args.Channel.ID))
2022-11-15 17:09:27 +13:00
{
_radio.SendRadioMessage(uid, args.Message, args.Channel, component.Headset);
2022-11-15 17:09:27 +13:00
args.Channel = null; // prevent duplicate messages from other listeners.
}
}
2023-02-19 06:27:56 +13:00
protected override void OnGotEquipped(EntityUid uid, HeadsetComponent component, GotEquippedEvent args)
2022-11-15 17:09:27 +13:00
{
2023-02-19 06:27:56 +13:00
base.OnGotEquipped(uid, component, args);
2022-11-15 17:09:27 +13:00
if (component.IsEquipped && component.Enabled)
{
EnsureComp<WearingHeadsetComponent>(args.Equipee).Headset = uid;
2023-02-19 06:27:56 +13:00
UpdateRadioChannels(uid, component);
2022-11-15 17:09:27 +13:00
}
}
2023-02-19 06:27:56 +13:00
protected override void OnGotUnequipped(EntityUid uid, HeadsetComponent component, GotUnequippedEvent args)
2022-11-15 17:09:27 +13:00
{
2023-02-19 06:27:56 +13:00
base.OnGotUnequipped(uid, component, args);
2022-11-15 17:09:27 +13:00
component.IsEquipped = false;
2022-11-15 17:30:37 +13:00
RemComp<ActiveRadioComponent>(uid);
RemComp<WearingHeadsetComponent>(args.Equipee);
2022-11-15 17:09:27 +13:00
}
public void SetEnabled(EntityUid uid, bool value, HeadsetComponent? component = null)
{
if (!Resolve(uid, ref component))
return;
if (component.Enabled == value)
return;
if (!value)
{
RemCompDeferred<ActiveRadioComponent>(uid);
if (component.IsEquipped)
RemCompDeferred<WearingHeadsetComponent>(Transform(uid).ParentUid);
}
else if (component.IsEquipped)
{
EnsureComp<WearingHeadsetComponent>(Transform(uid).ParentUid).Headset = uid;
2023-02-19 06:27:56 +13:00
UpdateRadioChannels(uid, component);
2022-11-15 17:09:27 +13:00
}
}
2023-03-24 03:02:41 +03:00
private void OnHeadsetReceive(EntityUid uid, HeadsetComponent component, ref RadioReceiveEvent args)
2022-11-15 17:09:27 +13:00
{
if (TryComp(Transform(uid).ParentUid, out ActorComponent? actor))
_netMan.ServerSendMessage(args.ChatMsg, actor.PlayerSession.ConnectedClient);
}
private void OnEmpPulse(EntityUid uid, HeadsetComponent component, ref EmpPulseEvent args)
{
if (component.Enabled)
{
args.Affected = true;
args.Disabled = true;
}
}
2022-11-15 17:09:27 +13:00
}