Files
tbd-station-14/Content.Shared/Containers/ContainerCompSystem.cs
metalgearsloth ae4c7ad0bc ContainerComp fix (#31434)
Forgot to push these ones on the branch.
2024-08-26 01:22:01 +10:00

44 lines
1.5 KiB
C#

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