2023-04-27 23:54:17 +06:00
using Content.Shared.Clothing.Components ;
using Content.Shared.Inventory.Events ;
using Robust.Shared.Serialization.Manager ;
using Content.Shared.Tag ;
2024-01-28 18:37:24 +07:00
using Content.Shared._White.ClothingGrant.Components ;
2024-07-29 06:04:21 +03:00
using Robust.Shared.Prototypes ;
2024-01-28 11:06:48 +03:00
using Robust.Shared.Timing ;
2023-04-27 23:54:17 +06:00
2024-01-28 18:37:24 +07:00
namespace Content.Shared._White.ClothingGrant.Systems ;
2023-04-27 23:54:17 +06:00
public sealed class ClothingGrantingSystem : EntitySystem
{
[Dependency] private readonly IComponentFactory _componentFactory = default ! ;
[Dependency] private readonly ISerializationManager _serializationManager = default ! ;
2024-01-28 11:06:48 +03:00
[Dependency] private readonly IGameTiming _timing = default ! ;
2023-04-27 23:54:17 +06:00
[Dependency] private readonly TagSystem _tagSystem = default ! ;
public override void Initialize ( )
{
base . Initialize ( ) ;
SubscribeLocalEvent < ClothingGrantComponentComponent , GotEquippedEvent > ( OnCompEquip ) ;
SubscribeLocalEvent < ClothingGrantComponentComponent , GotUnequippedEvent > ( OnCompUnequip ) ;
SubscribeLocalEvent < ClothingGrantTagComponent , GotEquippedEvent > ( OnTagEquip ) ;
SubscribeLocalEvent < ClothingGrantTagComponent , GotUnequippedEvent > ( OnTagUnequip ) ;
}
private void OnCompEquip ( EntityUid uid , ClothingGrantComponentComponent component , GotEquippedEvent args )
{
2024-01-28 11:06:48 +03:00
if ( _timing . ApplyingState )
return ;
2024-07-20 05:08:01 +03:00
if ( ! TryComp < ClothingComponent > ( uid , out var clothing ) )
return ;
2023-04-27 23:54:17 +06:00
2024-07-20 05:08:01 +03:00
if ( ! clothing . Slots . HasFlag ( args . SlotFlags ) )
return ;
2023-04-27 23:54:17 +06:00
2023-05-05 11:00:15 +03:00
if ( component . Components . Count > 8 )
2023-04-27 23:54:17 +06:00
{
2023-05-05 11:00:15 +03:00
Logger . Error ( "Although a component registry supports multiple components, we cannot bookkeep more than 8 component for ClothingGrantComponent at this time." ) ;
2023-04-27 23:54:17 +06:00
return ;
}
2024-07-29 06:04:21 +03:00
AddComponents ( args . Equipee , component . Components ) ;
component . IsActive = true ;
Dirty ( uid , component ) ;
}
public void AddComponents ( EntityUid uid , ComponentRegistry components )
{
foreach ( var ( name , data ) in components )
2023-04-27 23:54:17 +06:00
{
var newComp = ( Component ) _componentFactory . GetComponent ( name ) ;
2024-07-29 06:04:21 +03:00
if ( HasComp ( uid , newComp . GetType ( ) ) )
2023-04-27 23:54:17 +06:00
continue ;
2024-07-29 06:04:21 +03:00
newComp . Owner = uid ;
2023-04-27 23:54:17 +06:00
var temp = ( object ) newComp ;
_serializationManager . CopyTo ( data . Component , ref temp ) ;
2024-07-29 06:04:21 +03:00
EntityManager . AddComponent ( uid , ( Component ) temp ! ) ;
2023-04-27 23:54:17 +06:00
}
}
private void OnCompUnequip ( EntityUid uid , ClothingGrantComponentComponent component , GotUnequippedEvent args )
{
if ( ! component . IsActive ) return ;
foreach ( var ( name , data ) in component . Components )
{
var newComp = ( Component ) _componentFactory . GetComponent ( name ) ;
RemComp ( args . Equipee , newComp . GetType ( ) ) ;
}
component . IsActive = false ;
2024-07-20 05:08:01 +03:00
Dirty ( uid , component ) ;
2023-04-27 23:54:17 +06:00
}
private void OnTagEquip ( EntityUid uid , ClothingGrantTagComponent component , GotEquippedEvent args )
{
if ( ! TryComp < ClothingComponent > ( uid , out var clothing ) ) return ;
if ( ! clothing . Slots . HasFlag ( args . SlotFlags ) ) return ;
EnsureComp < TagComponent > ( args . Equipee ) ;
_tagSystem . AddTag ( args . Equipee , component . Tag ) ;
component . IsActive = true ;
2024-07-20 05:08:01 +03:00
Dirty ( uid , component ) ;
2023-04-27 23:54:17 +06:00
}
private void OnTagUnequip ( EntityUid uid , ClothingGrantTagComponent component , GotUnequippedEvent args )
{
if ( ! component . IsActive ) return ;
_tagSystem . RemoveTag ( args . Equipee , component . Tag ) ;
component . IsActive = false ;
2024-07-20 05:08:01 +03:00
Dirty ( uid , component ) ;
2023-04-27 23:54:17 +06:00
}
}