]> git.smokeofanarchy.ru Git - space-station-14.git/commitdiff
Migrate Lathe Material Ejection Code to MaterialStorage (#23199)
authorHannah Giovanna Dawson <karakkaraz@gmail.com>
Sun, 31 Dec 2023 01:08:33 +0000 (01:08 +0000)
committerGitHub <noreply@github.com>
Sun, 31 Dec 2023 01:08:33 +0000 (20:08 -0500)
* SS14-23184 Migrate Lathe Material Ejection Code to MaterialStorage

The lathe material ejection code acts as a do-nothing
man-in-the-middle system that does work that would be
reasonable for any MaterialStorage-using machine to
use. This has been fixed by migrating the ejection
code to MaterialStorage, allowing anything that uses
the system to eject mats it is storing.

* Fix some YAML references. Science!!

Content.Client/Lathe/UI/LatheBoundUserInterface.cs
Content.Server/Lathe/LatheSystem.cs
Content.Server/Materials/MaterialStorageSystem.cs
Content.Shared/Lathe/LatheComponent.cs
Content.Shared/Lathe/LatheEjectMaterialMessage.cs [deleted file]
Content.Shared/Materials/MaterialStorageComponent.cs
Resources/Prototypes/Entities/Structures/Machines/lathe.yml

index 69e41d1885535866d736b8897cf759428ae6990f..3e04b184b4e8b996952b2fb41662cce18569ffb1 100644 (file)
@@ -1,4 +1,5 @@
 using Content.Shared.Lathe;
+using Content.Shared.Materials;
 using Content.Shared.Research.Components;
 using JetBrains.Annotations;
 using Robust.Client.GameObjects;
@@ -34,7 +35,7 @@ namespace Content.Client.Lathe.UI
 
             _menu.OnEjectPressed += (material, sheetsToExtract) =>
             {
-                SendMessage(new LatheEjectMaterialMessage(material, sheetsToExtract));
+                SendMessage(new EjectMaterialMessage(material, sheetsToExtract));
             };
 
             _menu.OpenCentered();
index a262c1a69f7730458d7bfb9e06ebe05a1e004ff5..9e06dca52b8047ac48f0acdef9d668ff336b159f 100644 (file)
@@ -59,42 +59,6 @@ namespace Content.Server.Lathe
             SubscribeLocalEvent<TechnologyDatabaseComponent, LatheGetRecipesEvent>(OnGetRecipes);
             SubscribeLocalEvent<EmagLatheRecipesComponent, LatheGetRecipesEvent>(GetEmagLatheRecipes);
             SubscribeLocalEvent<LatheHeatProducingComponent, LatheStartPrintingEvent>(OnHeatStartPrinting);
-
-            SubscribeLocalEvent<LatheComponent, LatheEjectMaterialMessage>(OnLatheEjectMessage);
-        }
-
-        private void OnLatheEjectMessage(EntityUid uid, LatheComponent lathe, LatheEjectMaterialMessage message)
-        {
-            if (!lathe.CanEjectStoredMaterials)
-                return;
-
-            if (!_proto.TryIndex<MaterialPrototype>(message.Material, out var material))
-                return;
-
-            var volume = 0;
-
-            if (material.StackEntity != null)
-            {
-                var entProto = _proto.Index<EntityPrototype>(material.StackEntity);
-                if (!entProto.TryGetComponent<PhysicalCompositionComponent>(out var composition))
-                    return;
-
-                var volumePerSheet = composition.MaterialComposition.FirstOrDefault(kvp => kvp.Key == message.Material).Value;
-                var sheetsToExtract = Math.Min(message.SheetsToExtract, _stack.GetMaxCount(material.StackEntity));
-
-                volume = sheetsToExtract * volumePerSheet;
-            }
-
-            if (volume > 0 && _materialStorage.TryChangeMaterialAmount(uid, message.Material, -volume))
-            {
-                var mats = _materialStorage.SpawnMultipleFromMaterial(volume, material, Transform(uid).Coordinates, out _);
-                foreach (var mat in mats)
-                {
-                    if (TerminatingOrDeleted(mat))
-                        continue;
-                    _stack.TryMergeToContacts(mat);
-                }
-            }
         }
 
         public override void Update(float frameTime)
@@ -116,7 +80,7 @@ namespace Content.Server.Lathe
                     continue;
                 heatComp.NextSecond += TimeSpan.FromSeconds(1);
 
-                var position = _transform.GetGridTilePositionOrDefault((uid,xform));
+                var position = _transform.GetGridTilePositionOrDefault((uid, xform));
                 _environments.Clear();
 
                 if (_atmosphere.GetTileMixture(xform.GridUid, xform.MapUid, position, true) is { } tileMix)
index 5d581220e2aa561ad0bf519aa901eef7a0fa71f5..336a987505f8671c65394b8536d0cd7bbae57652 100644 (file)
@@ -30,6 +30,8 @@ public sealed class MaterialStorageSystem : SharedMaterialStorageSystem
     {
         base.Initialize();
         SubscribeLocalEvent<MaterialStorageComponent, MachineDeconstructedEvent>(OnDeconstructed);
+
+        SubscribeLocalEvent<MaterialStorageComponent, EjectMaterialMessage>(OnEjectMessage);
     }
 
     private void OnDeconstructed(EntityUid uid, MaterialStorageComponent component, MachineDeconstructedEvent args)
@@ -43,6 +45,34 @@ public sealed class MaterialStorageSystem : SharedMaterialStorageSystem
         }
     }
 
+    private void OnEjectMessage(EntityUid uid, MaterialStorageComponent component, EjectMaterialMessage message)
+    {
+        if (!component.CanEjectStoredMaterials || !_prototypeManager.TryIndex<MaterialPrototype>(message.Material, out var material))
+            return;
+
+        var volume = 0;
+
+        if (material.StackEntity != null)
+        {
+            if (!_prototypeManager.Index<EntityPrototype>(material.StackEntity).TryGetComponent<PhysicalCompositionComponent>(out var composition))
+                return;
+
+            var volumePerSheet = composition.MaterialComposition.FirstOrDefault(kvp => kvp.Key == message.Material).Value;
+            var sheetsToExtract = Math.Min(message.SheetsToExtract, _stackSystem.GetMaxCount(material.StackEntity));
+
+            volume = sheetsToExtract * volumePerSheet;
+        }
+
+        if (volume <= 0 || !TryChangeMaterialAmount(uid, message.Material, -volume))
+            return;
+
+        var mats = SpawnMultipleFromMaterial(volume, material, Transform(uid).Coordinates, out _);
+        foreach (var mat in mats.Where(mat => !TerminatingOrDeleted(mat)))
+        {
+            _stackSystem.TryMergeToContacts(mat);
+        }
+    }
+
     public override bool TryInsertMaterialEntity(EntityUid user,
         EntityUid toInsert,
         EntityUid receiver,
index 8607cf48e2a626999c9a10a2fa3bb4fc3ba15e34..6bbdb61a426f9484e398344b9d5077ec914d020d 100644 (file)
@@ -47,12 +47,6 @@ namespace Content.Shared.Lathe
         [ViewVariables]
         public LatheRecipePrototype? CurrentRecipe;
 
-        /// <summary>
-        /// Whether the lathe can eject the materials stored within it
-        /// </summary>
-        [DataField]
-        public bool CanEjectStoredMaterials = true;
-
         #region MachineUpgrading
         /// <summary>
         /// A modifier that changes how long it takes to print a recipe
diff --git a/Content.Shared/Lathe/LatheEjectMaterialMessage.cs b/Content.Shared/Lathe/LatheEjectMaterialMessage.cs
deleted file mode 100644 (file)
index 36e3005..0000000
+++ /dev/null
@@ -1,16 +0,0 @@
-using Robust.Shared.Serialization;
-
-namespace Content.Shared.Lathe;
-
-[Serializable, NetSerializable]
-public sealed class LatheEjectMaterialMessage : BoundUserInterfaceMessage
-{
-    public string Material;
-    public int SheetsToExtract;
-
-    public LatheEjectMaterialMessage(string material, int sheetsToExtract)
-    {
-        Material = material;
-        SheetsToExtract = sheetsToExtract;
-    }
-}
index c905478bca3ad7d76d169facdbe448b45b9507f8..6762dfeadc414ba87e02c5a998c78832510d8f1c 100644 (file)
@@ -61,6 +61,12 @@ public sealed partial class MaterialStorageComponent : Component
     /// </summary>
     [DataField]
     public TimeSpan InsertionTime = TimeSpan.FromSeconds(0.79f); // 0.01 off for animation timing
+
+    /// <summary>
+    /// Whether the storage can eject the materials stored within it
+    /// </summary>
+    [DataField]
+    public bool CanEjectStoredMaterials = true;
 }
 
 [Serializable, NetSerializable]
@@ -94,3 +100,20 @@ public record struct GetMaterialWhitelistEvent(EntityUid Storage)
 
     public List<ProtoId<MaterialPrototype>> Whitelist = new();
 }
+
+/// <summary>
+/// Message sent to try and eject a material from a storage
+/// </summary>
+[Serializable, NetSerializable]
+public sealed class EjectMaterialMessage : BoundUserInterfaceMessage
+{
+    public string Material;
+    public int SheetsToExtract;
+
+    public EjectMaterialMessage(string material, int sheetsToExtract)
+    {
+        Material = material;
+        SheetsToExtract = sheetsToExtract;
+    }
+}
+
index 7472258f75f610224b877b5b68eac0ceb3265a01..7811b137950ca26d6f5fc9e5b24b71aecc8a95d3 100644 (file)
     - type: MaterialStorage
       dropOnDeconstruct: false #should drop ores instead of ingots/sheets
       ignoreColor: true
+      canEjectStoredMaterials: false
       whitelist:
         tags:
           - Ore
     - type: Lathe
       idleState: icon
       runningState: building
-      canEjectStoredMaterials: false
       staticRecipes:
         - SheetSteel30
         - SheetGlass30
   - type: MaterialStorage
     dropOnDeconstruct: false #should drop ores instead of ingots/sheets
     ignoreColor: true
+    canEjectStoredMaterials: false
     whitelist:
       tags:
       - Raw
   - type: Lathe
     idleState: base_machine
     runningState: base_machine_processing
-    canEjectStoredMaterials: false
     staticRecipes:
     - MaterialSheetMeat
     - SheetPaper