Files

105 lines
3.4 KiB
C#
Raw Permalink Normal View History

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;
using Content.Shared._White.ClothingGrant.Components;
using Robust.Shared.Prototypes;
2024-01-28 11:06:48 +03:00
using Robust.Shared.Timing;
2023-04-27 23:54:17 +06: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;
if (!TryComp<ClothingComponent>(uid, out var clothing))
return;
2023-04-27 23:54:17 +06: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;
}
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);
if (HasComp(uid, newComp.GetType()))
2023-04-27 23:54:17 +06:00
continue;
newComp.Owner = uid;
2023-04-27 23:54:17 +06:00
var temp = (object) newComp;
_serializationManager.CopyTo(data.Component, ref temp);
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;
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;
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;
Dirty(uid, component);
2023-04-27 23:54:17 +06:00
}
}