diff --git a/Content.Server/PowerCell/PowerCellSystem.cs b/Content.Server/PowerCell/PowerCellSystem.cs index 2bd81447d3..ecfcb2070b 100644 --- a/Content.Server/PowerCell/PowerCellSystem.cs +++ b/Content.Server/PowerCell/PowerCellSystem.cs @@ -7,6 +7,7 @@ using Content.Shared.Examine; using Content.Shared.PowerCell; using Content.Shared.PowerCell.Components; using Content.Shared.Rounding; +using Robust.Shared.Containers; using Robust.Shared.GameObjects; using Robust.Shared.IoC; using Robust.Shared.Localization; @@ -20,6 +21,7 @@ public class PowerCellSystem : SharedPowerCellSystem [Dependency] private readonly SolutionContainerSystem _solutionsSystem = default!; [Dependency] private readonly ExplosionSystem _explosionSystem = default!; [Dependency] private readonly AdminLogSystem _logSystem = default!; + [Dependency] private readonly SharedContainerSystem _containerSystem = default!; public override void Initialize() { @@ -48,6 +50,14 @@ public class PowerCellSystem : SharedPowerCellSystem var frac = battery.CurrentCharge / battery.MaxCharge; var level = (byte) ContentHelpers.RoundToNearestLevels(frac, 1, PowerCellComponent.PowerCellVisualsLevels); appearance.SetData(PowerCellVisuals.ChargeLevel, level); + + // If this power cell is inside a cell-slot, inform that entity that the power has changed (for updating visuals n such). + if (_containerSystem.TryGetContainingContainer(uid, out var container) + && TryComp(container.Owner, out PowerCellSlotComponent? slot) + && slot.CellSlot.Item == uid) + { + RaiseLocalEvent(container.Owner, new PowerCellChangedEvent(false), false); + } } private void Explode(EntityUid uid, BatteryComponent? battery = null) diff --git a/Content.Server/Stunnable/StunbatonSystem.cs b/Content.Server/Stunnable/StunbatonSystem.cs index 7c5d728fb1..caeffc02af 100644 --- a/Content.Server/Stunnable/StunbatonSystem.cs +++ b/Content.Server/Stunnable/StunbatonSystem.cs @@ -99,7 +99,12 @@ namespace Content.Server.Stunnable private void OnPowerCellChanged(EntityUid uid, StunbatonComponent comp, PowerCellChangedEvent args) { - if (args.Ejected) + if (!comp.Activated) + return; + + if (args.Ejected + || !_cellSystem.TryGetBatteryFromSlot(comp.Owner, out var battery) + || battery.CurrentCharge < comp.EnergyPerUse) { TurnOff(comp); } @@ -175,9 +180,6 @@ namespace Content.Server.Stunnable return; var playerFilter = Filter.Pvs(comp.Owner); - if (!EntityManager.TryGetComponent(comp.Owner, out var slot)) - return; - if (!_cellSystem.TryGetBatteryFromSlot(comp.Owner, out var battery)) { SoundSystem.Play(playerFilter, comp.TurnOnFailSound.GetSound(), comp.Owner, AudioHelpers.WithVariation(0.25f)); diff --git a/Content.Server/Weapon/Ranged/Barrels/Components/ServerBatteryBarrelComponent.cs b/Content.Server/Weapon/Ranged/Barrels/Components/ServerBatteryBarrelComponent.cs index 295ba5af4e..d8cb5ee49b 100644 --- a/Content.Server/Weapon/Ranged/Barrels/Components/ServerBatteryBarrelComponent.cs +++ b/Content.Server/Weapon/Ranged/Barrels/Components/ServerBatteryBarrelComponent.cs @@ -163,8 +163,8 @@ namespace Content.Server.Weapon.Ranged.Barrels.Components throw new InvalidOperationException("Ammo doesn't have hitscan or projectile?"); } - Dirty(); - UpdateAppearance(); + // capacitor.UseCharge() triggers a PowerCellChangedEvent which will cause appearance to be updated. + // So let's not double-call UpdateAppearance() here. return entity.Value; } } diff --git a/Content.Shared/PowerCell/Components/PowerCellSlotComponent.cs b/Content.Shared/PowerCell/Components/PowerCellSlotComponent.cs index 6bb4742ca0..f8585bcd93 100644 --- a/Content.Shared/PowerCell/Components/PowerCellSlotComponent.cs +++ b/Content.Shared/PowerCell/Components/PowerCellSlotComponent.cs @@ -64,6 +64,9 @@ public sealed class PowerCellSlotComponent : Component public bool FitsInCharger = true; } +/// +/// Raised directed at an entity with a power cell slot when the power cell inside has its charge updated or is ejected/inserted. +/// public class PowerCellChangedEvent : EntityEventArgs { public readonly bool Ejected;