| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- using Content.Shared.Actions;
- using Content.Shared.Administration.Logs;
- using Content.Shared.Chat;
- using Content.Shared.Clothing;
- using Content.Shared.Database;
- using Content.Shared.Inventory;
- using Content.Shared.Popups;
- using Content.Shared.Preferences;
- using Content.Shared.Speech;
- using Content.Shared.VoiceMask;
- using Robust.Shared.Prototypes;
- namespace Content.Server.VoiceMask;
- public sealed partial class VoiceMaskSystem : EntitySystem
- {
- [Dependency] private readonly SharedUserInterfaceSystem _uiSystem = default!;
- [Dependency] private readonly SharedPopupSystem _popupSystem = default!;
- [Dependency] private readonly ISharedAdminLogManager _adminLogger = default!;
- [Dependency] private readonly IPrototypeManager _proto = default!;
- [Dependency] private readonly SharedActionsSystem _actions = default!;
- public override void Initialize()
- {
- base.Initialize();
- SubscribeLocalEvent<VoiceMaskComponent, InventoryRelayedEvent<TransformSpeakerNameEvent>>(OnTransformSpeakerName);
- SubscribeLocalEvent<VoiceMaskComponent, VoiceMaskChangeNameMessage>(OnChangeName);
- SubscribeLocalEvent<VoiceMaskComponent, VoiceMaskChangeVerbMessage>(OnChangeVerb);
- SubscribeLocalEvent<VoiceMaskComponent, ClothingGotEquippedEvent>(OnEquip);
- SubscribeLocalEvent<VoiceMaskSetNameEvent>(OpenUI);
- }
- private void OnTransformSpeakerName(Entity<VoiceMaskComponent> entity, ref InventoryRelayedEvent<TransformSpeakerNameEvent> args)
- {
- args.Args.VoiceName = GetCurrentVoiceName(entity);
- args.Args.SpeechVerb = entity.Comp.VoiceMaskSpeechVerb ?? args.Args.SpeechVerb;
- }
- #region User inputs from UI
- private void OnChangeVerb(Entity<VoiceMaskComponent> entity, ref VoiceMaskChangeVerbMessage msg)
- {
- if (msg.Verb is { } id && !_proto.HasIndex<SpeechVerbPrototype>(id))
- return;
- entity.Comp.VoiceMaskSpeechVerb = msg.Verb;
- // verb is only important to metagamers so no need to log as opposed to name
- _popupSystem.PopupEntity(Loc.GetString("voice-mask-popup-success"), entity, msg.Actor);
- UpdateUI(entity);
- }
- private void OnChangeName(Entity<VoiceMaskComponent> entity, ref VoiceMaskChangeNameMessage message)
- {
- if (message.Name.Length > HumanoidCharacterProfile.MaxNameLength || message.Name.Length <= 0)
- {
- _popupSystem.PopupEntity(Loc.GetString("voice-mask-popup-failure"), entity, message.Actor, PopupType.SmallCaution);
- return;
- }
- entity.Comp.VoiceMaskName = message.Name;
- _adminLogger.Add(LogType.Action, LogImpact.Medium, $"{ToPrettyString(message.Actor):player} set voice of {ToPrettyString(entity):mask}: {entity.Comp.VoiceMaskName}");
- _popupSystem.PopupEntity(Loc.GetString("voice-mask-popup-success"), entity, message.Actor);
- UpdateUI(entity);
- }
- #endregion
- #region UI
- private void OnEquip(EntityUid uid, VoiceMaskComponent component, ClothingGotEquippedEvent args)
- {
- _actions.AddAction(args.Wearer, ref component.ActionEntity, component.Action, uid);
- }
- private void OpenUI(VoiceMaskSetNameEvent ev)
- {
- var maskEntity = ev.Action.Comp.Container;
- if (!TryComp<VoiceMaskComponent>(maskEntity, out var voiceMaskComp))
- return;
- if (!_uiSystem.HasUi(maskEntity.Value, VoiceMaskUIKey.Key))
- return;
- _uiSystem.OpenUi(maskEntity.Value, VoiceMaskUIKey.Key, ev.Performer);
- UpdateUI((maskEntity.Value, voiceMaskComp));
- }
- private void UpdateUI(Entity<VoiceMaskComponent> entity)
- {
- if (_uiSystem.HasUi(entity, VoiceMaskUIKey.Key))
- _uiSystem.SetUiState(entity.Owner, VoiceMaskUIKey.Key, new VoiceMaskBuiState(GetCurrentVoiceName(entity), entity.Comp.VoiceMaskSpeechVerb));
- }
- #endregion
- #region Helper functions
- private string GetCurrentVoiceName(Entity<VoiceMaskComponent> entity)
- {
- return entity.Comp.VoiceMaskName ?? Loc.GetString("voice-mask-default-name-override");
- }
- #endregion
- }
|