From ab96ec8b2027b35d0bd5ce99ef193483ef48d9d8 Mon Sep 17 00:00:00 2001
From: Samuka <47865393+Samuka-C@users.noreply.github.com>
Date: Sat, 25 Apr 2026 19:32:31 -0300
Subject: [PATCH] SpawnOnBatteryFull component (#43727)
* code stuff
* fix error
* now its fixed
* use the charge from the arg
* only on battery full
* remove whitespace
Co-authored-by: slarticodefast <161409025+slarticodefast@users.noreply.github.com>
* use default! instead of null!
Co-authored-by: slarticodefast <161409025+slarticodefast@users.noreply.github.com>
* use set charge
---------
Co-authored-by: slarticodefast <161409025+slarticodefast@users.noreply.github.com>
---
.../Components/SpawnOnBatteryFullComponent.cs | 25 ++++++++++
.../EntitySystems/SpawnOnBatteryFullSystem.cs | 48 +++++++++++++++++++
2 files changed, 73 insertions(+)
create mode 100644 Content.Server/Power/Components/SpawnOnBatteryFullComponent.cs
create mode 100644 Content.Server/Power/EntitySystems/SpawnOnBatteryFullSystem.cs
diff --git a/Content.Server/Power/Components/SpawnOnBatteryFullComponent.cs b/Content.Server/Power/Components/SpawnOnBatteryFullComponent.cs
new file mode 100644
index 0000000000..f21eaec82e
--- /dev/null
+++ b/Content.Server/Power/Components/SpawnOnBatteryFullComponent.cs
@@ -0,0 +1,25 @@
+using Content.Shared.EntityTable.EntitySelectors;
+using Robust.Shared.Prototypes;
+
+namespace Content.Server.Power.Components;
+
+///
+/// Spawns a entity when the battery reaches max charge.
+/// It also consumes all power when spawning the entity.
+///
+[RegisterComponent]
+public sealed partial class SpawnOnBatteryFullComponent : Component
+{
+ ///
+ /// A entity proto to spawn.
+ ///
+ /// Takes priority over
+ [DataField]
+ public EntProtoId? Proto;
+
+ ///
+ /// A entity table to spawn stuff from.
+ ///
+ [DataField]
+ public EntityTableSelector? Table;
+}
diff --git a/Content.Server/Power/EntitySystems/SpawnOnBatteryFullSystem.cs b/Content.Server/Power/EntitySystems/SpawnOnBatteryFullSystem.cs
new file mode 100644
index 0000000000..365e25459c
--- /dev/null
+++ b/Content.Server/Power/EntitySystems/SpawnOnBatteryFullSystem.cs
@@ -0,0 +1,48 @@
+using Content.Server.Power.Components;
+using Content.Shared.EntityTable;
+using Content.Shared.EntityTable.EntitySelectors;
+using Content.Shared.Power;
+using Content.Shared.Power.Components;
+using Robust.Shared.Prototypes;
+
+namespace Content.Server.Power.EntitySystems;
+
+public sealed class SpawnOnBatteryFullSystem : EntitySystem
+{
+ [Dependency] private readonly BatterySystem _battery = default!;
+ [Dependency] private readonly EntityTableSystem _entityTable = default!;
+
+ public override void Initialize()
+ {
+ base.Initialize();
+
+ SubscribeLocalEvent(OnBatteryStateChange);
+ }
+
+ private void OnBatteryStateChange(Entity entity, ref BatteryStateChangedEvent args)
+ {
+ if (args.NewState != BatteryState.Full)
+ return;
+
+ if (entity.Comp.Proto == null)
+ SpawnFromEntityTable(entity, entity.Comp.Table);
+ else
+ SpawnFromProto(entity, entity.Comp.Proto.Value);
+
+ _battery.SetCharge(entity.Owner, 0);
+ }
+
+ private void SpawnFromEntityTable(EntityUid entity, EntityTableSelector? table)
+ {
+ var spawns = _entityTable.GetSpawns(table);
+ foreach (var spawn in spawns)
+ {
+ Spawn(spawn, Transform(entity).Coordinates);
+ }
+ }
+
+ private void SpawnFromProto(EntityUid entity, EntProtoId proto)
+ {
+ Spawn(proto, Transform(entity).Coordinates);
+ }
+}