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