using Robust.Shared.Containers;
using Robust.Shared.Prototypes;
using Robust.Shared.Timing;
namespace Content.Shared.Containers;
///
/// Applies / removes an entity prototype from a child entity when it's inserted into a container.
///
public sealed class ContainerCompSystem : EntitySystem
{
[Dependency] private readonly IGameTiming _timing = default!;
[Dependency] private readonly IPrototypeManager _proto = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent(OnConInsert);
SubscribeLocalEvent(OnConRemove);
}
private void OnConRemove(Entity ent, ref EntRemovedFromContainerMessage args)
{
if (args.Container.ID != ent.Comp.Container || _timing.ApplyingState)
return;
if (_proto.Resolve(ent.Comp.Proto, out var entProto))
{
EntityManager.RemoveComponents(args.Entity, entProto.Components);
}
}
private void OnConInsert(Entity ent, ref EntInsertedIntoContainerMessage args)
{
if (args.Container.ID != ent.Comp.Container || _timing.ApplyingState)
return;
if (_proto.Resolve(ent.Comp.Proto, out var entProto))
{
EntityManager.AddComponents(args.Entity, entProto.Components);
}
}
}