Skip to content

Commit

Permalink
Make APC UI work correctly with multiple users (space-wizards#32465)
Browse files Browse the repository at this point in the history
* 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 <helloworld@eoinrul.es>
  • Loading branch information
eoineoineoin and eoineoineoin authored Oct 12, 2024
1 parent a9ecf80 commit 70b7747
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 29 deletions.
13 changes: 11 additions & 2 deletions Content.Client/Power/APC/ApcBoundUserInterface.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -22,6 +23,14 @@ protected override void Open()
_menu = this.CreateWindow<ApcMenu>();
_menu.SetEntity(Owner);
_menu.OnBreaker += BreakerPressed;

var hasAccess = false;
if (PlayerManager.LocalEntity != null)
{
var accessReader = EntMan.System<AccessReaderSystem>();
hasAccess = accessReader.IsAllowed((EntityUid)PlayerManager.LocalEntity, Owner);
}
_menu?.SetAccessEnabled(hasAccess);
}

protected override void UpdateState(BoundUserInterfaceState state)
Expand Down
30 changes: 17 additions & 13 deletions Content.Client/Power/APC/UI/ApcMenu.xaml.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 0 additions & 3 deletions Content.Server/Power/Components/ApcComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/// <summary>
/// APC state needs to always be updated after first processing tick.
Expand Down
6 changes: 1 addition & 5 deletions Content.Server/Power/EntitySystems/ApcSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);

Expand Down
7 changes: 2 additions & 5 deletions Content.Shared/APC/SharedApc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,15 +178,13 @@ public enum ApcChargeState : sbyte
public sealed class ApcBoundInterfaceState : BoundUserInterfaceState, IEquatable<ApcBoundInterfaceState>
{
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;
Expand All @@ -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);
Expand All @@ -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);
}
}

Expand Down
1 change: 0 additions & 1 deletion Resources/Prototypes/Entities/Structures/Power/apc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@
type: ApcBoundUserInterface
- type: ActivatableUI
inHandsOnly: false
singleUser: true
key: enum.ApcUiKey.Key
- type: Construction
graph: APC
Expand Down

0 comments on commit 70b7747

Please sign in to comment.