]> git.smokeofanarchy.ru Git - space-station-14.git/commitdiff
add SpawnTableOnUse (#32620)
authordeltanedas <39013340+deltanedas@users.noreply.github.com>
Mon, 16 Dec 2024 12:23:14 +0000 (12:23 +0000)
committerGitHub <noreply@github.com>
Mon, 16 Dec 2024 12:23:14 +0000 (15:23 +0300)
* add SpawnTableOnUse

* make BaseEmitSound more flexible and remove sound from spawntable

* add log

* :trollface:

---------

Co-authored-by: deltanedas <@deltanedas:kde.org>
Content.Server/Storage/Components/SpawnTableOnUseComponent.cs [new file with mode: 0644]
Content.Server/Storage/EntitySystems/SpawnTableOnUseSystem.cs [new file with mode: 0644]
Content.Shared/Sound/Components/BaseEmitSoundComponent.cs
Content.Shared/Sound/SharedEmitSoundSystem.cs

diff --git a/Content.Server/Storage/Components/SpawnTableOnUseComponent.cs b/Content.Server/Storage/Components/SpawnTableOnUseComponent.cs
new file mode 100644 (file)
index 0000000..a7cbac7
--- /dev/null
@@ -0,0 +1,17 @@
+using Content.Server.Storage.EntitySystems;
+using Content.Shared.EntityTable.EntitySelectors;
+
+namespace Content.Server.Storage.Components;
+
+/// <summary>
+/// Spawns items from an entity table when used in hand.
+/// </summary>
+[RegisterComponent, Access(typeof(SpawnTableOnUseSystem))]
+public sealed partial class SpawnTableOnUseComponent : Component
+{
+    /// <summary>
+    /// The entity table to select entities from.
+    /// </summary>
+    [DataField(required: true)]
+    public EntityTableSelector Table = default!;
+}
diff --git a/Content.Server/Storage/EntitySystems/SpawnTableOnUseSystem.cs b/Content.Server/Storage/EntitySystems/SpawnTableOnUseSystem.cs
new file mode 100644 (file)
index 0000000..96556ed
--- /dev/null
@@ -0,0 +1,41 @@
+using Content.Server.Administration.Logs;
+using Content.Server.Storage.Components;
+using Content.Shared.Database;
+using Content.Shared.EntityTable;
+using Content.Shared.Hands.EntitySystems;
+using Content.Shared.Interaction.Events;
+
+namespace Content.Server.Storage.EntitySystems;
+
+public sealed class SpawnTableOnUseSystem : EntitySystem
+{
+    [Dependency] private readonly EntityTableSystem _entityTable = default!;
+    [Dependency] private readonly IAdminLogManager _adminLogger = default!;
+    [Dependency] private readonly SharedHandsSystem _hands = default!;
+
+    public override void Initialize()
+    {
+        base.Initialize();
+
+        SubscribeLocalEvent<SpawnTableOnUseComponent, UseInHandEvent>(OnUseInHand);
+    }
+
+    private void OnUseInHand(Entity<SpawnTableOnUseComponent> ent, ref UseInHandEvent args)
+    {
+        if (args.Handled)
+            return;
+
+        args.Handled = true;
+
+        var coords = Transform(ent).Coordinates;
+        var spawns = _entityTable.GetSpawns(ent.Comp.Table);
+        foreach (var id in spawns)
+        {
+            var spawned = Spawn(id, coords);
+            _adminLogger.Add(LogType.EntitySpawn, LogImpact.Low, $"{ToPrettyString(args.User):user} used {ToPrettyString(ent):spawner} which spawned {ToPrettyString(spawned)}");
+            _hands.TryPickupAnyHand(args.User, spawned);
+        }
+
+        Del(ent);
+    }
+}
index 4e6ebb23d21644d39a3c59e92d13911850920031..870d20457ef761e6f360b90c4e1f6bc2c591a705 100644 (file)
@@ -14,4 +14,11 @@ public abstract partial class BaseEmitSoundComponent : Component
     [ViewVariables(VVAccess.ReadWrite)]
     [DataField(required: true)]
     public SoundSpecifier? Sound;
+
+    /// <summary>
+    /// Play the sound at the position instead of parented to the source entity.
+    /// Useful if the entity is deleted after.
+    /// </summary>
+    [DataField]
+    public bool Positional;
 }
index 3e051fff3177702c211056573f197a3e89122895..30744b68644cbc978811973728336000710175dd 100644 (file)
@@ -145,14 +145,22 @@ public abstract class SharedEmitSoundSystem : EntitySystem
         if (component.Sound == null)
             return;
 
-        if (predict)
+        if (component.Positional)
         {
-            _audioSystem.PlayPredicted(component.Sound, uid, user);
+            var coords = Transform(uid).Coordinates;
+            if (predict)
+                _audioSystem.PlayPredicted(component.Sound, coords, user);
+            else if (_netMan.IsServer)
+                // don't predict sounds that client couldn't have played already
+                _audioSystem.PlayPvs(component.Sound, coords);
         }
-        else if (_netMan.IsServer)
+        else
         {
-            // don't predict sounds that client couldn't have played already
-            _audioSystem.PlayPvs(component.Sound, uid);
+            if (predict)
+                _audioSystem.PlayPredicted(component.Sound, uid, user);
+            else if (_netMan.IsServer)
+                // don't predict sounds that client couldn't have played already
+                _audioSystem.PlayPvs(component.Sound, uid);
         }
     }