From f03fc585baa5d22e3b962321b5082a8e7e4f7726 Mon Sep 17 00:00:00 2001
From: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
Date: Sun, 25 Aug 2024 22:06:06 +1000
Subject: [PATCH] Add ContainerComp (#31311)
Applies EntProtoId changes upon insertion / removal from container. Can also be useful for borgs / mechs / vehicles in future but atm I just used it for AI.
---
.../Containers/ContainerCompComponent.cs | 17 +++++++
.../Containers/ContainerCompSystem.cs | 44 +++++++++++++++++++
2 files changed, 61 insertions(+)
create mode 100644 Content.Shared/Containers/ContainerCompComponent.cs
create mode 100644 Content.Shared/Containers/ContainerCompSystem.cs
diff --git a/Content.Shared/Containers/ContainerCompComponent.cs b/Content.Shared/Containers/ContainerCompComponent.cs
new file mode 100644
index 0000000000..b1415e0d8b
--- /dev/null
+++ b/Content.Shared/Containers/ContainerCompComponent.cs
@@ -0,0 +1,17 @@
+using Robust.Shared.GameStates;
+using Robust.Shared.Prototypes;
+
+namespace Content.Shared.Containers;
+
+///
+/// Applies container changes whenever an entity is inserted into the specified container on this entity.
+///
+[RegisterComponent, NetworkedComponent]
+public sealed partial class ContainerCompComponent : Component
+{
+ [DataField(required: true)]
+ public EntProtoId Proto;
+
+ [DataField(required: true)]
+ public string Container = string.Empty;
+}
diff --git a/Content.Shared/Containers/ContainerCompSystem.cs b/Content.Shared/Containers/ContainerCompSystem.cs
new file mode 100644
index 0000000000..1e1983a331
--- /dev/null
+++ b/Content.Shared/Containers/ContainerCompSystem.cs
@@ -0,0 +1,44 @@
+using Robust.Shared.Containers;
+using Robust.Shared.Prototypes;
+
+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 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)
+ return;
+
+ if (_proto.TryIndex(ent.Comp.Container, out var entProto))
+ {
+ foreach (var entry in entProto.Components.Values)
+ {
+ RemComp(args.Entity, entry.Component);
+ }
+ }
+ }
+
+ private void OnConInsert(Entity ent, ref EntInsertedIntoContainerMessage args)
+ {
+ if (args.Container.ID != ent.Comp.Container)
+ return;
+
+ if (_proto.TryIndex(ent.Comp.Proto, out var entProto))
+ {
+ EntityManager.AddComponents(args.Entity, entProto.Components);
+ }
+ }
+}
--
2.51.2