From 70b7747fddd58d556c1408464dcf969cdeb4a71a Mon Sep 17 00:00:00 2001 From: eoineoineoin Date: Sat, 12 Oct 2024 09:21:43 +0100 Subject: [PATCH] Make APC UI work correctly with multiple users (#32465) * Make APC UI work correctly with multiple users * Check access only on client, when constructing UI * Do TODO (Thanks, Robust 236.1) --------- Co-authored-by: Eoin Mcloughlin --- .../Power/APC/ApcBoundUserInterface.cs | 13 ++++++-- Content.Client/Power/APC/UI/ApcMenu.xaml.cs | 30 +++++++++++-------- .../Power/Components/ApcComponent.cs | 3 -- .../Power/EntitySystems/ApcSystem.cs | 6 +--- Content.Shared/APC/SharedApc.cs | 7 ++--- .../Entities/Structures/Power/apc.yml | 1 - 6 files changed, 31 insertions(+), 29 deletions(-) diff --git a/Content.Client/Power/APC/ApcBoundUserInterface.cs b/Content.Client/Power/APC/ApcBoundUserInterface.cs index a790c5d984ad1..5c4036a9159e6 100644 --- a/Content.Client/Power/APC/ApcBoundUserInterface.cs +++ b/Content.Client/Power/APC/ApcBoundUserInterface.cs @@ -1,8 +1,9 @@ -using Content.Client.Power.APC.UI; +using Content.Client.Power.APC.UI; +using Content.Shared.Access.Systems; using Content.Shared.APC; using JetBrains.Annotations; -using Robust.Client.GameObjects; using Robust.Client.UserInterface; +using Robust.Shared.Player; namespace Content.Client.Power.APC { @@ -22,6 +23,14 @@ protected override void Open() _menu = this.CreateWindow(); _menu.SetEntity(Owner); _menu.OnBreaker += BreakerPressed; + + var hasAccess = false; + if (PlayerManager.LocalEntity != null) + { + var accessReader = EntMan.System(); + hasAccess = accessReader.IsAllowed((EntityUid)PlayerManager.LocalEntity, Owner); + } + _menu?.SetAccessEnabled(hasAccess); } protected override void UpdateState(BoundUserInterfaceState state) diff --git a/Content.Client/Power/APC/UI/ApcMenu.xaml.cs b/Content.Client/Power/APC/UI/ApcMenu.xaml.cs index 2f61ea63a8640..25e885b3c7a18 100644 --- a/Content.Client/Power/APC/UI/ApcMenu.xaml.cs +++ b/Content.Client/Power/APC/UI/ApcMenu.xaml.cs @@ -1,4 +1,4 @@ -using Robust.Client.AutoGenerated; +using Robust.Client.AutoGenerated; using Robust.Client.UserInterface.XAML; using Robust.Client.GameObjects; using Robust.Shared.IoC; @@ -36,19 +36,9 @@ public void UpdateState(BoundUserInterfaceState state) { var castState = (ApcBoundInterfaceState) state; - if (BreakerButton != null) + if (!BreakerButton.Disabled) { - if(castState.HasAccess == false) - { - BreakerButton.Disabled = true; - BreakerButton.ToolTip = Loc.GetString("apc-component-insufficient-access"); - } - else - { - BreakerButton.Disabled = false; - BreakerButton.ToolTip = null; - BreakerButton.Pressed = castState.MainBreaker; - } + BreakerButton.Pressed = castState.MainBreaker; } if (PowerLabel != null) @@ -86,6 +76,20 @@ public void UpdateState(BoundUserInterfaceState state) } } + public void SetAccessEnabled(bool hasAccess) + { + if(hasAccess) + { + BreakerButton.Disabled = false; + BreakerButton.ToolTip = null; + } + else + { + BreakerButton.Disabled = true; + BreakerButton.ToolTip = Loc.GetString("apc-component-insufficient-access"); + } + } + private void UpdateChargeBarColor(float charge) { if (ChargeBar == null) diff --git a/Content.Server/Power/Components/ApcComponent.cs b/Content.Server/Power/Components/ApcComponent.cs index bee8defc43e46..0bf9bc1872163 100644 --- a/Content.Server/Power/Components/ApcComponent.cs +++ b/Content.Server/Power/Components/ApcComponent.cs @@ -25,9 +25,6 @@ public sealed partial class ApcComponent : BaseApcNetComponent [DataField("enabled")] public bool MainBreakerEnabled = true; - // TODO: remove this since it probably breaks when 2 people use it - [DataField("hasAccess")] - public bool HasAccess = false; /// /// APC state needs to always be updated after first processing tick. diff --git a/Content.Server/Power/EntitySystems/ApcSystem.cs b/Content.Server/Power/EntitySystems/ApcSystem.cs index 52c19c302cee9..14dddbb43e329 100644 --- a/Content.Server/Power/EntitySystems/ApcSystem.cs +++ b/Content.Server/Power/EntitySystems/ApcSystem.cs @@ -2,7 +2,6 @@ using Content.Server.Popups; using Content.Server.Power.Components; using Content.Server.Power.Pow3r; -using Content.Shared.Access.Components; using Content.Shared.Access.Systems; using Content.Shared.APC; using Content.Shared.Emag.Components; @@ -71,11 +70,8 @@ private static void OnApcStartup(EntityUid uid, ApcComponent component, Componen component.NeedStateUpdate = true; } - //Update the HasAccess var for UI to read private void OnBoundUiOpen(EntityUid uid, ApcComponent component, BoundUIOpenedEvent args) { - // TODO: this should be per-player not stored on the apc - component.HasAccess = _accessReader.IsAllowed(args.Actor, uid); UpdateApcState(uid, component); } @@ -165,7 +161,7 @@ public void UpdateUIState(EntityUid uid, // TODO: Fix ContentHelpers or make a new one coz this is cooked. var charge = ContentHelpers.RoundToNearestLevels(battery.CurrentStorage / battery.Capacity, 1.0, 100 / ChargeAccuracy) / 100f * ChargeAccuracy; - var state = new ApcBoundInterfaceState(apc.MainBreakerEnabled, apc.HasAccess, + var state = new ApcBoundInterfaceState(apc.MainBreakerEnabled, (int) MathF.Ceiling(battery.CurrentSupply), apc.LastExternalState, charge); diff --git a/Content.Shared/APC/SharedApc.cs b/Content.Shared/APC/SharedApc.cs index bf9fdc9444ba5..802c06a6ab6f8 100644 --- a/Content.Shared/APC/SharedApc.cs +++ b/Content.Shared/APC/SharedApc.cs @@ -178,15 +178,13 @@ public enum ApcChargeState : sbyte public sealed class ApcBoundInterfaceState : BoundUserInterfaceState, IEquatable { public readonly bool MainBreaker; - public readonly bool HasAccess; public readonly int Power; public readonly ApcExternalPowerState ApcExternalPower; public readonly float Charge; - public ApcBoundInterfaceState(bool mainBreaker, bool hasAccess, int power, ApcExternalPowerState apcExternalPower, float charge) + public ApcBoundInterfaceState(bool mainBreaker, int power, ApcExternalPowerState apcExternalPower, float charge) { MainBreaker = mainBreaker; - HasAccess = hasAccess; Power = power; ApcExternalPower = apcExternalPower; Charge = charge; @@ -197,7 +195,6 @@ public bool Equals(ApcBoundInterfaceState? other) if (ReferenceEquals(null, other)) return false; if (ReferenceEquals(this, other)) return true; return MainBreaker == other.MainBreaker && - HasAccess == other.HasAccess && Power == other.Power && ApcExternalPower == other.ApcExternalPower && MathHelper.CloseTo(Charge, other.Charge); @@ -210,7 +207,7 @@ public override bool Equals(object? obj) public override int GetHashCode() { - return HashCode.Combine(MainBreaker, HasAccess, Power, (int) ApcExternalPower, Charge); + return HashCode.Combine(MainBreaker, Power, (int) ApcExternalPower, Charge); } } diff --git a/Resources/Prototypes/Entities/Structures/Power/apc.yml b/Resources/Prototypes/Entities/Structures/Power/apc.yml index 7c0537dec6e76..abf6931c5ee13 100644 --- a/Resources/Prototypes/Entities/Structures/Power/apc.yml +++ b/Resources/Prototypes/Entities/Structures/Power/apc.yml @@ -91,7 +91,6 @@ type: ApcBoundUserInterface - type: ActivatableUI inHandsOnly: false - singleUser: true key: enum.ApcUiKey.Key - type: Construction graph: APC