using Content.Server.Popups; using Content.Server.Power.EntitySystems; using Content.Server.Shuttles.Components; using Content.Shared.Construction.Components; using Content.Shared.Popups; namespace Content.Server.Shuttles.Systems; public sealed class StationAnchorSystem : EntitySystem { [Dependency] private readonly ShuttleSystem _shuttleSystem = default!; [Dependency] private readonly PopupSystem _popupSystem = default!; public override void Initialize() { base.Initialize(); SubscribeLocalEvent(OnUnanchorAttempt); SubscribeLocalEvent(OnAnchorStationChange); SubscribeLocalEvent(OnActivated); SubscribeLocalEvent(OnDeactivated); SubscribeLocalEvent(OnMapInit); } private void OnMapInit(Entity ent, ref MapInitEvent args) { if (!ent.Comp.SwitchedOn) return; SetStatus(ent, true); } private void OnActivated(Entity ent, ref ChargedMachineActivatedEvent args) { SetStatus(ent, true); } private void OnDeactivated(Entity ent, ref ChargedMachineDeactivatedEvent args) { SetStatus(ent, false); } /// /// Prevent unanchoring when anchor is active /// private void OnUnanchorAttempt(Entity ent, ref UnanchorAttemptEvent args) { if (!ent.Comp.SwitchedOn) return; _popupSystem.PopupEntity( Loc.GetString("station-anchor-unanchoring-failed"), ent, args.User, PopupType.Medium); args.Cancel(); } private void OnAnchorStationChange(Entity ent, ref AnchorStateChangedEvent args) { if (!args.Anchored) SetStatus(ent, false); } private void SetStatus(Entity ent, bool enabled, ShuttleComponent? shuttleComponent = default) { var transform = Transform(ent); var grid = transform.GridUid; if (!grid.HasValue || !transform.Anchored && enabled || !Resolve(grid.Value, ref shuttleComponent)) return; if (enabled) { _shuttleSystem.Disable(grid.Value); } else { _shuttleSystem.Enable(grid.Value); } shuttleComponent.Enabled = !enabled; ent.Comp.SwitchedOn = enabled; } }