diff --git a/Content.Server/Light/EntitySystems/HandheldLightSystem.cs b/Content.Server/Light/EntitySystems/HandheldLightSystem.cs index 303e2a2fd7..0902602953 100644 --- a/Content.Server/Light/EntitySystems/HandheldLightSystem.cs +++ b/Content.Server/Light/EntitySystems/HandheldLightSystem.cs @@ -1,5 +1,8 @@ +using Content.Server.Actions; using Content.Server.Popups; using Content.Server.PowerCell; +using Content.Shared.Actions; +using Content.Shared.Actions.ActionTypes; using Content.Shared.Examine; using Content.Shared.Interaction; using Content.Shared.Light; @@ -8,7 +11,10 @@ using Content.Shared.Toggleable; using Content.Shared.Verbs; using JetBrains.Annotations; using Robust.Server.GameObjects; +using Robust.Shared.Audio; +using Robust.Shared.Containers; using Robust.Shared.GameStates; +using Robust.Shared.Player; using Robust.Shared.Prototypes; using Robust.Shared.Utility; @@ -31,6 +37,7 @@ namespace Content.Server.Light.EntitySystems { base.Initialize(); + SubscribeLocalEvent(OnRemove); SubscribeLocalEvent(OnGetState); SubscribeLocalEvent(OnExamine); @@ -38,7 +45,41 @@ namespace Content.Server.Light.EntitySystems SubscribeLocalEvent(OnActivate); + SubscribeLocalEvent(OnGetActions); SubscribeLocalEvent(OnToggleAction); + + SubscribeLocalEvent(OnEntInserted); + SubscribeLocalEvent(OnEntRemoved); + } + + private void OnEntInserted( + EntityUid uid, + HandheldLightComponent component, + EntInsertedIntoContainerMessage args) + { + // Not guaranteed to be the correct container for our slot, I don't care. + UpdateLevel(uid, component); + } + + private void OnEntRemoved( + EntityUid uid, + HandheldLightComponent component, + EntRemovedFromContainerMessage args) + { + // Ditto above + UpdateLevel(uid, component); + } + + private void OnGetActions(EntityUid uid, HandheldLightComponent component, GetItemActionsEvent args) + { + if (component.ToggleAction == null + && _proto.TryIndex(component.ToggleActionId, out InstantActionPrototype? act)) + { + component.ToggleAction = new(act); + } + + if (component.ToggleAction != null) + args.Actions.Add(component.ToggleAction); } private void OnToggleAction(EntityUid uid, HandheldLightComponent component, ToggleActionEvent args) @@ -73,6 +114,11 @@ namespace Content.Server.Light.EntitySystems return (byte?) ContentHelpers.RoundToNearestLevels(battery.CurrentCharge / battery.MaxCharge * 255, 255, HandheldLightComponent.StatusLevels); } + private void OnRemove(EntityUid uid, HandheldLightComponent component, ComponentRemove args) + { + _activeLights.Remove(component); + } + private void OnActivate(EntityUid uid, HandheldLightComponent component, ActivateInWorldEvent args) { if (args.Handled) @@ -98,6 +144,36 @@ namespace Content.Server.Light.EntitySystems : Loc.GetString("handheld-light-component-on-examine-is-off-message")); } + public override void Shutdown() + { + base.Shutdown(); + _activeLights.Clear(); + } + + public override void Update(float frameTime) + { + var toRemove = new RemQueue(); + + foreach (var handheld in _activeLights) + { + var uid = handheld.Owner; + + if (handheld.Deleted) + { + toRemove.Add(handheld); + continue; + } + + if (Paused(uid)) continue; + TryUpdate(uid, handheld, frameTime); + } + + foreach (var light in toRemove) + { + _activeLights.Remove(light); + } + } + private void AddToggleLightVerb(EntityUid uid, HandheldLightComponent component, GetVerbsEvent args) { if (!args.CanAccess || !args.CanInteract)