1
0

ContainerCompSystem.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using Robust.Shared.Containers;
  2. using Robust.Shared.Prototypes;
  3. using Robust.Shared.Timing;
  4. namespace Content.Shared.Containers;
  5. /// <summary>
  6. /// Applies / removes an entity prototype from a child entity when it's inserted into a container.
  7. /// </summary>
  8. public sealed class ContainerCompSystem : EntitySystem
  9. {
  10. [Dependency] private readonly IGameTiming _timing = default!;
  11. [Dependency] private readonly IPrototypeManager _proto = default!;
  12. public override void Initialize()
  13. {
  14. base.Initialize();
  15. SubscribeLocalEvent<ContainerCompComponent, EntInsertedIntoContainerMessage>(OnConInsert);
  16. SubscribeLocalEvent<ContainerCompComponent, EntRemovedFromContainerMessage>(OnConRemove);
  17. }
  18. private void OnConRemove(Entity<ContainerCompComponent> ent, ref EntRemovedFromContainerMessage args)
  19. {
  20. if (args.Container.ID != ent.Comp.Container || _timing.ApplyingState)
  21. return;
  22. if (_proto.TryIndex(ent.Comp.Proto, out var entProto))
  23. {
  24. EntityManager.RemoveComponents(args.Entity, entProto.Components);
  25. }
  26. }
  27. private void OnConInsert(Entity<ContainerCompComponent> ent, ref EntInsertedIntoContainerMessage args)
  28. {
  29. if (args.Container.ID != ent.Comp.Container || _timing.ApplyingState)
  30. return;
  31. if (_proto.TryIndex(ent.Comp.Proto, out var entProto))
  32. {
  33. EntityManager.AddComponents(args.Entity, entProto.Components);
  34. }
  35. }
  36. }