Skip to content

Commit 70b7747

Browse files
authored
Make APC UI work correctly with multiple users (space-wizards#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 <[email protected]>
1 parent a9ecf80 commit 70b7747

File tree

6 files changed

+31
-29
lines changed

6 files changed

+31
-29
lines changed

Content.Client/Power/APC/ApcBoundUserInterface.cs

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
using Content.Client.Power.APC.UI;
1+
using Content.Client.Power.APC.UI;
2+
using Content.Shared.Access.Systems;
23
using Content.Shared.APC;
34
using JetBrains.Annotations;
4-
using Robust.Client.GameObjects;
55
using Robust.Client.UserInterface;
6+
using Robust.Shared.Player;
67

78
namespace Content.Client.Power.APC
89
{
@@ -22,6 +23,14 @@ protected override void Open()
2223
_menu = this.CreateWindow<ApcMenu>();
2324
_menu.SetEntity(Owner);
2425
_menu.OnBreaker += BreakerPressed;
26+
27+
var hasAccess = false;
28+
if (PlayerManager.LocalEntity != null)
29+
{
30+
var accessReader = EntMan.System<AccessReaderSystem>();
31+
hasAccess = accessReader.IsAllowed((EntityUid)PlayerManager.LocalEntity, Owner);
32+
}
33+
_menu?.SetAccessEnabled(hasAccess);
2534
}
2635

2736
protected override void UpdateState(BoundUserInterfaceState state)

Content.Client/Power/APC/UI/ApcMenu.xaml.cs

+17-13
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Robust.Client.AutoGenerated;
1+
using Robust.Client.AutoGenerated;
22
using Robust.Client.UserInterface.XAML;
33
using Robust.Client.GameObjects;
44
using Robust.Shared.IoC;
@@ -36,19 +36,9 @@ public void UpdateState(BoundUserInterfaceState state)
3636
{
3737
var castState = (ApcBoundInterfaceState) state;
3838

39-
if (BreakerButton != null)
39+
if (!BreakerButton.Disabled)
4040
{
41-
if(castState.HasAccess == false)
42-
{
43-
BreakerButton.Disabled = true;
44-
BreakerButton.ToolTip = Loc.GetString("apc-component-insufficient-access");
45-
}
46-
else
47-
{
48-
BreakerButton.Disabled = false;
49-
BreakerButton.ToolTip = null;
50-
BreakerButton.Pressed = castState.MainBreaker;
51-
}
41+
BreakerButton.Pressed = castState.MainBreaker;
5242
}
5343

5444
if (PowerLabel != null)
@@ -86,6 +76,20 @@ public void UpdateState(BoundUserInterfaceState state)
8676
}
8777
}
8878

79+
public void SetAccessEnabled(bool hasAccess)
80+
{
81+
if(hasAccess)
82+
{
83+
BreakerButton.Disabled = false;
84+
BreakerButton.ToolTip = null;
85+
}
86+
else
87+
{
88+
BreakerButton.Disabled = true;
89+
BreakerButton.ToolTip = Loc.GetString("apc-component-insufficient-access");
90+
}
91+
}
92+
8993
private void UpdateChargeBarColor(float charge)
9094
{
9195
if (ChargeBar == null)

Content.Server/Power/Components/ApcComponent.cs

-3
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,6 @@ public sealed partial class ApcComponent : BaseApcNetComponent
2525

2626
[DataField("enabled")]
2727
public bool MainBreakerEnabled = true;
28-
// TODO: remove this since it probably breaks when 2 people use it
29-
[DataField("hasAccess")]
30-
public bool HasAccess = false;
3128

3229
/// <summary>
3330
/// APC state needs to always be updated after first processing tick.

Content.Server/Power/EntitySystems/ApcSystem.cs

+1-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using Content.Server.Popups;
33
using Content.Server.Power.Components;
44
using Content.Server.Power.Pow3r;
5-
using Content.Shared.Access.Components;
65
using Content.Shared.Access.Systems;
76
using Content.Shared.APC;
87
using Content.Shared.Emag.Components;
@@ -71,11 +70,8 @@ private static void OnApcStartup(EntityUid uid, ApcComponent component, Componen
7170
component.NeedStateUpdate = true;
7271
}
7372

74-
//Update the HasAccess var for UI to read
7573
private void OnBoundUiOpen(EntityUid uid, ApcComponent component, BoundUIOpenedEvent args)
7674
{
77-
// TODO: this should be per-player not stored on the apc
78-
component.HasAccess = _accessReader.IsAllowed(args.Actor, uid);
7975
UpdateApcState(uid, component);
8076
}
8177

@@ -165,7 +161,7 @@ public void UpdateUIState(EntityUid uid,
165161
// TODO: Fix ContentHelpers or make a new one coz this is cooked.
166162
var charge = ContentHelpers.RoundToNearestLevels(battery.CurrentStorage / battery.Capacity, 1.0, 100 / ChargeAccuracy) / 100f * ChargeAccuracy;
167163

168-
var state = new ApcBoundInterfaceState(apc.MainBreakerEnabled, apc.HasAccess,
164+
var state = new ApcBoundInterfaceState(apc.MainBreakerEnabled,
169165
(int) MathF.Ceiling(battery.CurrentSupply), apc.LastExternalState,
170166
charge);
171167

Content.Shared/APC/SharedApc.cs

+2-5
Original file line numberDiff line numberDiff line change
@@ -178,15 +178,13 @@ public enum ApcChargeState : sbyte
178178
public sealed class ApcBoundInterfaceState : BoundUserInterfaceState, IEquatable<ApcBoundInterfaceState>
179179
{
180180
public readonly bool MainBreaker;
181-
public readonly bool HasAccess;
182181
public readonly int Power;
183182
public readonly ApcExternalPowerState ApcExternalPower;
184183
public readonly float Charge;
185184

186-
public ApcBoundInterfaceState(bool mainBreaker, bool hasAccess, int power, ApcExternalPowerState apcExternalPower, float charge)
185+
public ApcBoundInterfaceState(bool mainBreaker, int power, ApcExternalPowerState apcExternalPower, float charge)
187186
{
188187
MainBreaker = mainBreaker;
189-
HasAccess = hasAccess;
190188
Power = power;
191189
ApcExternalPower = apcExternalPower;
192190
Charge = charge;
@@ -197,7 +195,6 @@ public bool Equals(ApcBoundInterfaceState? other)
197195
if (ReferenceEquals(null, other)) return false;
198196
if (ReferenceEquals(this, other)) return true;
199197
return MainBreaker == other.MainBreaker &&
200-
HasAccess == other.HasAccess &&
201198
Power == other.Power &&
202199
ApcExternalPower == other.ApcExternalPower &&
203200
MathHelper.CloseTo(Charge, other.Charge);
@@ -210,7 +207,7 @@ public override bool Equals(object? obj)
210207

211208
public override int GetHashCode()
212209
{
213-
return HashCode.Combine(MainBreaker, HasAccess, Power, (int) ApcExternalPower, Charge);
210+
return HashCode.Combine(MainBreaker, Power, (int) ApcExternalPower, Charge);
214211
}
215212
}
216213

Resources/Prototypes/Entities/Structures/Power/apc.yml

-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@
9191
type: ApcBoundUserInterface
9292
- type: ActivatableUI
9393
inHandsOnly: false
94-
singleUser: true
9594
key: enum.ApcUiKey.Key
9695
- type: Construction
9796
graph: APC

0 commit comments

Comments
 (0)