ContainerFillSystem.cs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using System.Numerics;
  2. using Content.Shared.EntityTable;
  3. using Robust.Shared.Containers;
  4. using Robust.Shared.Map;
  5. namespace Content.Shared.Containers;
  6. public sealed class ContainerFillSystem : EntitySystem
  7. {
  8. [Dependency] private readonly SharedContainerSystem _containerSystem = default!;
  9. [Dependency] private readonly EntityTableSystem _entityTable = default!;
  10. [Dependency] private readonly SharedTransformSystem _transform = default!;
  11. public override void Initialize()
  12. {
  13. base.Initialize();
  14. SubscribeLocalEvent<ContainerFillComponent, MapInitEvent>(OnMapInit);
  15. SubscribeLocalEvent<EntityTableContainerFillComponent, MapInitEvent>(OnTableMapInit);
  16. }
  17. private void OnMapInit(EntityUid uid, ContainerFillComponent component, MapInitEvent args)
  18. {
  19. if (!TryComp(uid, out ContainerManagerComponent? containerComp))
  20. return;
  21. var xform = Transform(uid);
  22. var coords = new EntityCoordinates(uid, Vector2.Zero);
  23. foreach (var (contaienrId, prototypes) in component.Containers)
  24. {
  25. if (!_containerSystem.TryGetContainer(uid, contaienrId, out var container, containerComp))
  26. {
  27. Log.Error($"Entity {ToPrettyString(uid)} with a {nameof(ContainerFillComponent)} is missing a container ({contaienrId}).");
  28. continue;
  29. }
  30. foreach (var proto in prototypes)
  31. {
  32. var ent = Spawn(proto, coords);
  33. if (!_containerSystem.Insert(ent, container, containerXform: xform))
  34. {
  35. Log.Error($"Entity {ToPrettyString(uid)} with a {nameof(ContainerFillComponent)} failed to insert an entity: {ToPrettyString(ent)}.");
  36. _transform.AttachToGridOrMap(ent);
  37. break;
  38. }
  39. }
  40. }
  41. }
  42. private void OnTableMapInit(Entity<EntityTableContainerFillComponent> ent, ref MapInitEvent args)
  43. {
  44. if (!TryComp(ent, out ContainerManagerComponent? containerComp))
  45. return;
  46. if (TerminatingOrDeleted(ent) || !Exists(ent))
  47. return;
  48. var xform = Transform(ent);
  49. var coords = new EntityCoordinates(ent, Vector2.Zero);
  50. foreach (var (containerId, table) in ent.Comp.Containers)
  51. {
  52. if (!_containerSystem.TryGetContainer(ent, containerId, out var container, containerComp))
  53. {
  54. Log.Error($"Entity {ToPrettyString(ent)} with a {nameof(EntityTableContainerFillComponent)} is missing a container ({containerId}).");
  55. continue;
  56. }
  57. var spawns = _entityTable.GetSpawns(table);
  58. foreach (var proto in spawns)
  59. {
  60. var spawn = Spawn(proto, coords);
  61. if (!_containerSystem.Insert(spawn, container, containerXform: xform))
  62. {
  63. Log.Error($"Entity {ToPrettyString(ent)} with a {nameof(EntityTableContainerFillComponent)} failed to insert an entity: {ToPrettyString(spawn)}.");
  64. _transform.AttachToGridOrMap(spawn);
  65. break;
  66. }
  67. }
  68. }
  69. }
  70. }