]> git.smokeofanarchy.ru Git - space-station-14.git/commitdiff
Add container-related triggers (#39647)
authorArtxmisery <78118840+Artxmisery@users.noreply.github.com>
Thu, 14 Aug 2025 23:53:48 +0000 (23:53 +0000)
committerGitHub <noreply@github.com>
Thu, 14 Aug 2025 23:53:48 +0000 (01:53 +0200)
* added triggers for when an entity is inserted or removed from a container, for both the entity and the container

* removed unnecessary comments

* consolidated into one shared system for all four triggers

* consolidated into one shared system for all four triggers

* named it right this time

* made container owner user of got triggers and added guard statements

* container id

* Update Content.Shared/Trigger/Components/Triggers/TriggerOnRemovedFromContainerComponent.cs

---------

Co-authored-by: slarticodefast <161409025+slarticodefast@users.noreply.github.com>
Content.Shared/Trigger/Components/Triggers/TriggerOnGotInsertedIntoContainerComponent.cs [new file with mode: 0644]
Content.Shared/Trigger/Components/Triggers/TriggerOnGotRemovedFromContainerComponent.cs [new file with mode: 0644]
Content.Shared/Trigger/Components/Triggers/TriggerOnInsertedIntoContainerComponent.cs [new file with mode: 0644]
Content.Shared/Trigger/Components/Triggers/TriggerOnRemovedFromContainerComponent.cs [new file with mode: 0644]
Content.Shared/Trigger/Systems/TriggerOnContainerInteractionSystem.cs [new file with mode: 0644]

diff --git a/Content.Shared/Trigger/Components/Triggers/TriggerOnGotInsertedIntoContainerComponent.cs b/Content.Shared/Trigger/Components/Triggers/TriggerOnGotInsertedIntoContainerComponent.cs
new file mode 100644 (file)
index 0000000..132167a
--- /dev/null
@@ -0,0 +1,18 @@
+using Robust.Shared.GameStates;
+
+namespace Content.Shared.Trigger.Components.Triggers;
+
+/// <summary>
+/// Triggers an entity when it gets inserted into a container.
+/// The user is the owner of the container the entity is being inserted into.
+/// </summary>
+[RegisterComponent, NetworkedComponent, AutoGenerateComponentState]
+public sealed partial class TriggerOnGotInsertedIntoContainerComponent : BaseTriggerOnXComponent
+{
+    /// <summary>
+    /// The container to the entity has to be inserted into.
+    /// Null will allow all containers.
+    /// </summary>
+    [DataField, AutoNetworkedField]
+    public string? ContainerId;
+}
diff --git a/Content.Shared/Trigger/Components/Triggers/TriggerOnGotRemovedFromContainerComponent.cs b/Content.Shared/Trigger/Components/Triggers/TriggerOnGotRemovedFromContainerComponent.cs
new file mode 100644 (file)
index 0000000..8c66bf4
--- /dev/null
@@ -0,0 +1,18 @@
+using Robust.Shared.GameStates;
+
+namespace Content.Shared.Trigger.Components.Triggers;
+
+/// <summary>
+/// Triggers an entity when it gets removed from a container.
+/// The user is the owner of the container the entity is being removed from.
+/// </summary>
+[RegisterComponent, NetworkedComponent, AutoGenerateComponentState]
+public sealed partial class TriggerOnGotRemovedFromContainerComponent : BaseTriggerOnXComponent
+{
+    /// <summary>
+    /// The container to the entity has to be removed from.
+    /// Null will allow all containers.
+    /// </summary>
+    [DataField, AutoNetworkedField]
+    public string? ContainerId;
+}
diff --git a/Content.Shared/Trigger/Components/Triggers/TriggerOnInsertedIntoContainerComponent.cs b/Content.Shared/Trigger/Components/Triggers/TriggerOnInsertedIntoContainerComponent.cs
new file mode 100644 (file)
index 0000000..d5616df
--- /dev/null
@@ -0,0 +1,18 @@
+using Robust.Shared.GameStates;
+
+namespace Content.Shared.Trigger.Components.Triggers;
+
+/// <summary>
+/// Triggers an entity when something is inserted into it.
+/// The user is the entity being inserted into the container.
+/// </summary>
+[RegisterComponent, NetworkedComponent, AutoGenerateComponentState]
+public sealed partial class TriggerOnInsertedIntoContainerComponent : BaseTriggerOnXComponent
+{
+    /// <summary>
+    /// The container to the entity has to be inserted into.
+    /// Null will allow all containers.
+    /// </summary>
+    [DataField, AutoNetworkedField]
+    public string? ContainerId;
+}
diff --git a/Content.Shared/Trigger/Components/Triggers/TriggerOnRemovedFromContainerComponent.cs b/Content.Shared/Trigger/Components/Triggers/TriggerOnRemovedFromContainerComponent.cs
new file mode 100644 (file)
index 0000000..095b040
--- /dev/null
@@ -0,0 +1,18 @@
+using Robust.Shared.GameStates;
+
+namespace Content.Shared.Trigger.Components.Triggers;
+
+/// <summary>
+/// Triggers an entity when something is removed from it.
+/// The user is the entity being removed from the container.
+/// </summary>
+[RegisterComponent, NetworkedComponent, AutoGenerateComponentState]
+public sealed partial class TriggerOnRemovedFromContainerComponent : BaseTriggerOnXComponent
+{
+    /// <summary>
+    /// The container to the entity has to be removed from.
+    /// Null will allow all containers.
+    /// </summary>
+    [DataField, AutoNetworkedField]
+    public string? ContainerId;
+}
diff --git a/Content.Shared/Trigger/Systems/TriggerOnContainerInteractionSystem.cs b/Content.Shared/Trigger/Systems/TriggerOnContainerInteractionSystem.cs
new file mode 100644 (file)
index 0000000..8fa9308
--- /dev/null
@@ -0,0 +1,70 @@
+using Content.Shared.Trigger.Components.Triggers;
+using Robust.Shared.Containers;
+using Robust.Shared.Timing;
+
+namespace Content.Shared.Trigger.Systems;
+
+/// <summary>
+/// System for creating triggers when entities are inserted into or removed from containers.
+/// </summary>
+public sealed class TriggerOnContainerInteractionSystem : EntitySystem
+{
+    [Dependency] private readonly TriggerSystem _trigger = default!;
+    [Dependency] private readonly IGameTiming _timing = default!;
+
+    public override void Initialize()
+    {
+        base.Initialize();
+
+        SubscribeLocalEvent<TriggerOnInsertedIntoContainerComponent, EntInsertedIntoContainerMessage>(OnInsertedIntoContainer);
+        SubscribeLocalEvent<TriggerOnRemovedFromContainerComponent, EntRemovedFromContainerMessage>(OnRemovedFromContainer);
+        SubscribeLocalEvent<TriggerOnGotInsertedIntoContainerComponent, EntGotInsertedIntoContainerMessage>(OnGotInsertedIntoContainer);
+        SubscribeLocalEvent<TriggerOnGotRemovedFromContainerComponent, EntGotRemovedFromContainerMessage>(OnGotRemovedFromContainer);
+    }
+
+    // Used by containers to trigger when entities are inserted into or removed from them
+    private void OnInsertedIntoContainer(Entity<TriggerOnInsertedIntoContainerComponent> ent, ref EntInsertedIntoContainerMessage args)
+    {
+        if (_timing.ApplyingState)
+            return;
+
+        if (ent.Comp.ContainerId != null && ent.Comp.ContainerId != args.Container.ID)
+            return;
+
+        _trigger.Trigger(ent.Owner, args.Entity, ent.Comp.KeyOut);
+    }
+
+    private void OnRemovedFromContainer(Entity<TriggerOnRemovedFromContainerComponent> ent, ref EntRemovedFromContainerMessage args)
+    {
+        if (_timing.ApplyingState)
+            return;
+
+        if (ent.Comp.ContainerId != null && ent.Comp.ContainerId != args.Container.ID)
+            return;
+
+        _trigger.Trigger(ent.Owner, args.Entity, ent.Comp.KeyOut);
+    }
+
+    // Used by entities to trigger when they are inserted into or removed from a container
+    private void OnGotInsertedIntoContainer(Entity<TriggerOnGotInsertedIntoContainerComponent> ent, ref EntGotInsertedIntoContainerMessage args)
+    {
+        if (_timing.ApplyingState)
+            return;
+
+        if (ent.Comp.ContainerId != null && ent.Comp.ContainerId != args.Container.ID)
+            return;
+
+        _trigger.Trigger(ent.Owner, args.Container.Owner, ent.Comp.KeyOut);
+    }
+
+    private void OnGotRemovedFromContainer(Entity<TriggerOnGotRemovedFromContainerComponent> ent, ref EntGotRemovedFromContainerMessage args)
+    {
+        if (_timing.ApplyingState)
+            return;
+
+        if (ent.Comp.ContainerId != null && ent.Comp.ContainerId != args.Container.ID)
+            return;
+
+        _trigger.Trigger(ent.Owner, args.Container.Owner, ent.Comp.KeyOut);
+    }
+}