2023-01-02 22:39:11 -06:00
using Content.Server.Administration.Logs ;
2022-04-16 05:31:12 +00:00
using Content.Server.Popups ;
using Content.Server.UserInterface ;
using Content.Shared.AirlockPainter ;
using Content.Shared.AirlockPainter.Prototypes ;
2023-02-24 19:01:25 -05:00
using Content.Shared.DoAfter ;
2023-01-02 22:39:11 -06:00
using Content.Shared.Database ;
2022-04-16 05:31:12 +00:00
using Content.Shared.Doors.Components ;
using Content.Shared.Interaction ;
using JetBrains.Annotations ;
using Robust.Server.GameObjects ;
namespace Content.Server.AirlockPainter
{
/// <summary>
/// A system for painting airlocks using airlock painter
/// </summary>
[UsedImplicitly]
public sealed class AirlockPainterSystem : SharedAirlockPainterSystem
{
2023-01-02 22:39:11 -06:00
[Dependency] private readonly IAdminLogManager _adminLogger = default ! ;
2022-04-16 05:31:12 +00:00
[Dependency] private readonly UserInterfaceSystem _userInterfaceSystem = default ! ;
2023-04-03 13:13:48 +12:00
[Dependency] private readonly SharedDoAfterSystem _doAfterSystem = default ! ;
2022-04-16 05:31:12 +00:00
[Dependency] private readonly PopupSystem _popupSystem = default ! ;
2023-02-24 19:01:25 -05:00
[Dependency] private readonly SharedAudioSystem _audio = default ! ;
2023-02-02 17:34:53 +01:00
[Dependency] private readonly SharedAppearanceSystem _appearance = default ! ;
2022-04-16 05:31:12 +00:00
public override void Initialize ( )
{
base . Initialize ( ) ;
SubscribeLocalEvent < AirlockPainterComponent , AfterInteractEvent > ( AfterInteractOn ) ;
SubscribeLocalEvent < AirlockPainterComponent , ActivateInWorldEvent > ( OnActivate ) ;
SubscribeLocalEvent < AirlockPainterComponent , AirlockPainterSpritePickedMessage > ( OnSpritePicked ) ;
2023-04-03 13:13:48 +12:00
SubscribeLocalEvent < AirlockPainterComponent , AirlockPainterDoAfterEvent > ( OnDoAfter ) ;
2022-04-16 05:31:12 +00:00
}
2023-04-03 13:13:48 +12:00
private void OnDoAfter ( EntityUid uid , AirlockPainterComponent component , AirlockPainterDoAfterEvent args )
2022-04-16 05:31:12 +00:00
{
2023-04-03 13:13:48 +12:00
component . IsSpraying = false ;
2023-02-24 19:01:25 -05:00
if ( args . Handled | | args . Cancelled )
return ;
2023-01-02 22:39:11 -06:00
2023-02-24 19:01:25 -05:00
if ( args . Args . Target ! = null )
{
2023-04-03 13:13:48 +12:00
_audio . PlayPvs ( component . SpraySound , uid ) ;
_appearance . SetData ( args . Args . Target . Value , DoorVisuals . BaseRSI , args . Sprite ) ;
2023-02-24 19:01:25 -05:00
_adminLogger . Add ( LogType . Action , LogImpact . Low , $"{ToPrettyString(args.Args.User):user} painted {ToPrettyString(args.Args.Target.Value):target}" ) ;
2022-04-16 05:31:12 +00:00
}
2023-02-24 19:01:25 -05:00
args . Handled = true ;
2022-04-16 05:31:12 +00:00
}
private void OnActivate ( EntityUid uid , AirlockPainterComponent component , ActivateInWorldEvent args )
{
if ( ! EntityManager . TryGetComponent ( args . User , out ActorComponent ? actor ) )
return ;
DirtyUI ( uid , component ) ;
2023-07-08 09:02:17 -07:00
if ( _userInterfaceSystem . TryGetUi ( uid , AirlockPainterUiKey . Key , out var bui ) )
_userInterfaceSystem . OpenUi ( bui , actor . PlayerSession ) ;
2022-04-16 05:31:12 +00:00
args . Handled = true ;
}
private void AfterInteractOn ( EntityUid uid , AirlockPainterComponent component , AfterInteractEvent args )
{
if ( component . IsSpraying | | args . Target is not { Valid : true } target | | ! args . CanReach )
return ;
if ( ! EntityManager . TryGetComponent < PaintableAirlockComponent > ( target , out var airlock ) )
return ;
if ( ! _prototypeManager . TryIndex < AirlockGroupPrototype > ( airlock . Group , out var grp ) )
{
2023-06-27 23:56:52 +10:00
Log . Error ( "Group not defined: %s" , airlock . Group ) ;
2022-04-16 05:31:12 +00:00
return ;
}
string style = Styles [ component . Index ] ;
if ( ! grp . StylePaths . TryGetValue ( style , out var sprite ) )
{
string msg = Loc . GetString ( "airlock-painter-style-not-available" ) ;
2022-12-19 10:41:47 +13:00
_popupSystem . PopupEntity ( msg , args . User , args . User ) ;
2022-04-16 05:31:12 +00:00
return ;
}
component . IsSpraying = true ;
2023-02-24 19:01:25 -05:00
2023-04-03 13:13:48 +12:00
var doAfterEventArgs = new DoAfterArgs ( args . User , component . SprayTime , new AirlockPainterDoAfterEvent ( sprite ) , uid , target : target , used : uid )
2022-04-16 05:31:12 +00:00
{
BreakOnTargetMove = true ,
BreakOnUserMove = true ,
BreakOnDamage = true ,
NeedHand = true ,
} ;
2023-04-03 13:13:48 +12:00
_doAfterSystem . TryStartDoAfter ( doAfterEventArgs ) ;
2023-01-02 22:39:11 -06:00
// Log attempt
_adminLogger . Add ( LogType . Action , LogImpact . Low , $"{ToPrettyString(args.User):user} is painting {ToPrettyString(uid):target} to '{style}' at {Transform(uid).Coordinates:targetlocation}" ) ;
2022-04-16 05:31:12 +00:00
}
private void OnSpritePicked ( EntityUid uid , AirlockPainterComponent component , AirlockPainterSpritePickedMessage args )
{
component . Index = args . Index ;
DirtyUI ( uid , component ) ;
}
private void DirtyUI ( EntityUid uid ,
AirlockPainterComponent ? component = null )
{
if ( ! Resolve ( uid , ref component ) )
return ;
_userInterfaceSystem . TrySetUiState ( uid , AirlockPainterUiKey . Key ,
new AirlockPainterBoundUserInterfaceState ( component . Index ) ) ;
}
}
}