Add a test that puts all components on an entity and checks for no exceptions (#1815)
* Add test that puts all components on an entity and checks for no exceptions Also fix all the exceptions that happened because of this * Add comments to the test * Fix nullable errors * Fix more nullable errors * More nullable error fixes * Unignore basic actor component * Fix more nullable errors * NULLABLE ERROR * Add string interpolation * Merge if checks * Remove redundant pragma warning disable 649 * Address reviews * Remove null wrappers around TryGetComponent * Merge conflict fixes * APC battery component error fix * Fix power test * Fix atmos mapgrid usages
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
#nullable enable
|
||||
using System;
|
||||
using Content.Server.GameObjects.Components.NodeContainer.NodeGroups;
|
||||
using Content.Server.GameObjects.Components.Power.PowerNetComponents;
|
||||
using Content.Shared.GameObjects.Components.Power;
|
||||
@@ -20,17 +21,12 @@ namespace Content.Server.GameObjects.Components.Power.ApcNetComponents
|
||||
[ComponentReference(typeof(IActivate))]
|
||||
public class ApcComponent : BaseApcNetComponent, IActivate
|
||||
{
|
||||
[Dependency] private readonly IGameTiming _gameTiming = default!;
|
||||
|
||||
public override string Name => "Apc";
|
||||
|
||||
[ViewVariables]
|
||||
public BatteryComponent Battery { get; private set; }
|
||||
|
||||
public bool MainBreakerEnabled { get; private set; } = true;
|
||||
|
||||
private BoundUserInterface _userInterface;
|
||||
|
||||
private AppearanceComponent _appearance;
|
||||
|
||||
private ApcChargeState _lastChargeState;
|
||||
|
||||
private TimeSpan _lastChargeStateChange;
|
||||
@@ -39,7 +35,7 @@ namespace Content.Server.GameObjects.Components.Power.ApcNetComponents
|
||||
|
||||
private TimeSpan _lastExternalPowerStateChange;
|
||||
|
||||
private float _lastCharge = 0f;
|
||||
private float _lastCharge;
|
||||
|
||||
private TimeSpan _lastChargeChange;
|
||||
|
||||
@@ -49,17 +45,27 @@ namespace Content.Server.GameObjects.Components.Power.ApcNetComponents
|
||||
|
||||
private const int VisualsChangeDelay = 1;
|
||||
|
||||
#pragma warning disable 649
|
||||
[Dependency] private readonly IGameTiming _gameTiming;
|
||||
#pragma warning restore 649
|
||||
[ViewVariables]
|
||||
private BoundUserInterface? UserInterface =>
|
||||
Owner.TryGetComponent(out ServerUserInterfaceComponent? ui) &&
|
||||
ui.TryGetBoundUserInterface(ApcUiKey.Key, out var boundUi)
|
||||
? boundUi
|
||||
: null;
|
||||
|
||||
public BatteryComponent? Battery => Owner.TryGetComponent(out BatteryComponent? batteryComponent) ? batteryComponent : null;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
Battery = Owner.GetComponent<BatteryComponent>();
|
||||
_appearance = Owner.GetComponent<AppearanceComponent>();
|
||||
_userInterface = Owner.GetComponent<ServerUserInterfaceComponent>().GetBoundUserInterface(ApcUiKey.Key);
|
||||
_userInterface.OnReceiveMessage += UserInterfaceOnReceiveMessage;
|
||||
|
||||
Owner.EnsureComponent<BatteryComponent>();
|
||||
Owner.EnsureComponent<PowerConsumerComponent>();
|
||||
|
||||
if (UserInterface != null)
|
||||
{
|
||||
UserInterface.OnReceiveMessage += UserInterfaceOnReceiveMessage;
|
||||
}
|
||||
|
||||
Update();
|
||||
}
|
||||
|
||||
@@ -90,15 +96,23 @@ namespace Content.Server.GameObjects.Components.Power.ApcNetComponents
|
||||
{
|
||||
_lastChargeState = newState;
|
||||
_lastChargeStateChange = _gameTiming.CurTime;
|
||||
_appearance.SetData(ApcVisuals.ChargeState, newState);
|
||||
|
||||
if (Owner.TryGetComponent(out AppearanceComponent? appearance))
|
||||
{
|
||||
appearance.SetData(ApcVisuals.ChargeState, newState);
|
||||
}
|
||||
}
|
||||
var newCharge = Battery.CurrentCharge;
|
||||
if (newCharge != _lastCharge && _lastChargeChange + TimeSpan.FromSeconds(VisualsChangeDelay) < _gameTiming.CurTime)
|
||||
|
||||
Owner.TryGetComponent(out BatteryComponent? battery);
|
||||
|
||||
var newCharge = battery?.CurrentCharge;
|
||||
if (newCharge != null && newCharge != _lastCharge && _lastChargeChange + TimeSpan.FromSeconds(VisualsChangeDelay) < _gameTiming.CurTime)
|
||||
{
|
||||
_lastCharge = newCharge;
|
||||
_lastCharge = newCharge.Value;
|
||||
_lastChargeChange = _gameTiming.CurTime;
|
||||
_uiDirty = true;
|
||||
}
|
||||
|
||||
var extPowerState = CalcExtPowerState();
|
||||
if (extPowerState != _lastExternalPowerState && _lastExternalPowerStateChange + TimeSpan.FromSeconds(VisualsChangeDelay) < _gameTiming.CurTime)
|
||||
{
|
||||
@@ -106,21 +120,33 @@ namespace Content.Server.GameObjects.Components.Power.ApcNetComponents
|
||||
_lastExternalPowerStateChange = _gameTiming.CurTime;
|
||||
_uiDirty = true;
|
||||
}
|
||||
if (_uiDirty)
|
||||
|
||||
if (_uiDirty && battery != null && newCharge != null)
|
||||
{
|
||||
_userInterface.SetState(new ApcBoundInterfaceState(MainBreakerEnabled, extPowerState, newCharge / Battery.MaxCharge));
|
||||
UserInterface?.SetState(new ApcBoundInterfaceState(MainBreakerEnabled, extPowerState, newCharge.Value / battery.MaxCharge));
|
||||
_uiDirty = false;
|
||||
}
|
||||
}
|
||||
|
||||
private ApcChargeState CalcChargeState()
|
||||
{
|
||||
var chargeFraction = Battery.CurrentCharge / Battery.MaxCharge;
|
||||
if (!Owner.TryGetComponent(out BatteryComponent? battery))
|
||||
{
|
||||
return ApcChargeState.Lack;
|
||||
}
|
||||
|
||||
var chargeFraction = battery.CurrentCharge / battery.MaxCharge;
|
||||
|
||||
if (chargeFraction > HighPowerThreshold)
|
||||
{
|
||||
return ApcChargeState.Full;
|
||||
}
|
||||
var consumer = Owner.GetComponent<PowerConsumerComponent>();
|
||||
|
||||
if (!Owner.TryGetComponent(out PowerConsumerComponent? consumer))
|
||||
{
|
||||
return ApcChargeState.Full;
|
||||
}
|
||||
|
||||
if (consumer.DrawRate == consumer.ReceivedPower)
|
||||
{
|
||||
return ApcChargeState.Charging;
|
||||
@@ -133,7 +159,7 @@ namespace Content.Server.GameObjects.Components.Power.ApcNetComponents
|
||||
|
||||
private ApcExternalPowerState CalcExtPowerState()
|
||||
{
|
||||
if (!Owner.TryGetComponent(out BatteryStorageComponent batteryStorage))
|
||||
if (!Owner.TryGetComponent(out BatteryStorageComponent? batteryStorage))
|
||||
{
|
||||
return ApcExternalPowerState.None;
|
||||
}
|
||||
@@ -154,11 +180,12 @@ namespace Content.Server.GameObjects.Components.Power.ApcNetComponents
|
||||
|
||||
void IActivate.Activate(ActivateEventArgs eventArgs)
|
||||
{
|
||||
if (!eventArgs.User.TryGetComponent(out IActorComponent actor))
|
||||
if (!eventArgs.User.TryGetComponent(out IActorComponent? actor))
|
||||
{
|
||||
return;
|
||||
}
|
||||
_userInterface.Open(actor.playerSession);
|
||||
|
||||
UserInterface?.Open(actor.playerSession);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
#nullable enable
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Content.Server.GameObjects.Components.GUI;
|
||||
using Content.Server.GameObjects.Components.Items.Storage;
|
||||
@@ -24,19 +25,14 @@ namespace Content.Server.GameObjects.Components.Power.ApcNetComponents.PowerRece
|
||||
public abstract class BaseCharger : Component, IActivate, IInteractUsing
|
||||
{
|
||||
[ViewVariables]
|
||||
private BatteryComponent _heldBattery;
|
||||
private BatteryComponent? _heldBattery;
|
||||
|
||||
[ViewVariables]
|
||||
private ContainerSlot _container;
|
||||
|
||||
[ViewVariables]
|
||||
private PowerReceiverComponent _powerReceiver;
|
||||
private ContainerSlot _container = default!;
|
||||
|
||||
[ViewVariables]
|
||||
private CellChargerStatus _status;
|
||||
|
||||
private AppearanceComponent _appearanceComponent;
|
||||
|
||||
[ViewVariables]
|
||||
private int _chargeRate;
|
||||
|
||||
@@ -53,16 +49,25 @@ namespace Content.Server.GameObjects.Components.Power.ApcNetComponents.PowerRece
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
_powerReceiver = Owner.GetComponent<PowerReceiverComponent>();
|
||||
|
||||
Owner.EnsureComponent<PowerReceiverComponent>();
|
||||
_container = ContainerManagerComponent.Ensure<ContainerSlot>($"{Name}-powerCellContainer", Owner);
|
||||
_appearanceComponent = Owner.GetComponent<AppearanceComponent>();
|
||||
// Default state in the visualizer is OFF, so when this gets powered on during initialization it will generally show empty
|
||||
_powerReceiver.OnPowerStateChanged += PowerUpdate;
|
||||
if (Owner.TryGetComponent(out PowerReceiverComponent? receiver))
|
||||
{
|
||||
receiver.OnPowerStateChanged += PowerUpdate;
|
||||
}
|
||||
}
|
||||
|
||||
public override void OnRemove()
|
||||
{
|
||||
_powerReceiver.OnPowerStateChanged -= PowerUpdate;
|
||||
if (Owner.TryGetComponent(out PowerReceiverComponent? receiver))
|
||||
{
|
||||
receiver.OnPowerStateChanged -= PowerUpdate;
|
||||
}
|
||||
|
||||
_heldBattery = null;
|
||||
|
||||
base.OnRemove();
|
||||
}
|
||||
|
||||
@@ -97,12 +102,12 @@ namespace Content.Server.GameObjects.Components.Power.ApcNetComponents.PowerRece
|
||||
|
||||
_container.Remove(heldItem);
|
||||
_heldBattery = null;
|
||||
if (user.TryGetComponent(out HandsComponent handsComponent))
|
||||
if (user.TryGetComponent(out HandsComponent? handsComponent))
|
||||
{
|
||||
handsComponent.PutInHandOrDrop(heldItem.GetComponent<ItemComponent>());
|
||||
}
|
||||
|
||||
if (heldItem.TryGetComponent(out ServerBatteryBarrelComponent batteryBarrelComponent))
|
||||
if (heldItem.TryGetComponent(out ServerBatteryBarrelComponent? batteryBarrelComponent))
|
||||
{
|
||||
batteryBarrelComponent.UpdateAppearance();
|
||||
}
|
||||
@@ -110,7 +115,7 @@ namespace Content.Server.GameObjects.Components.Power.ApcNetComponents.PowerRece
|
||||
UpdateStatus();
|
||||
}
|
||||
|
||||
private void PowerUpdate(object sender, PowerStateEventArgs eventArgs)
|
||||
private void PowerUpdate(object? sender, PowerStateEventArgs eventArgs)
|
||||
{
|
||||
UpdateStatus();
|
||||
}
|
||||
@@ -125,7 +130,7 @@ namespace Content.Server.GameObjects.Components.Power.ApcNetComponents.PowerRece
|
||||
data.Visibility = VerbVisibility.Invisible;
|
||||
return;
|
||||
}
|
||||
if (!user.TryGetComponent(out HandsComponent handsComponent))
|
||||
if (!user.TryGetComponent(out HandsComponent? handsComponent))
|
||||
{
|
||||
data.Visibility = VerbVisibility.Invisible;
|
||||
return;
|
||||
@@ -143,7 +148,7 @@ namespace Content.Server.GameObjects.Components.Power.ApcNetComponents.PowerRece
|
||||
|
||||
protected override void Activate(IEntity user, BaseCharger component)
|
||||
{
|
||||
if (!user.TryGetComponent(out HandsComponent handsComponent))
|
||||
if (!user.TryGetComponent(out HandsComponent? handsComponent))
|
||||
{
|
||||
return;
|
||||
}
|
||||
@@ -186,7 +191,8 @@ namespace Content.Server.GameObjects.Components.Power.ApcNetComponents.PowerRece
|
||||
|
||||
private CellChargerStatus GetStatus()
|
||||
{
|
||||
if (!_powerReceiver.Powered)
|
||||
if (Owner.TryGetComponent(out PowerReceiverComponent? receiver) &&
|
||||
!receiver.Powered)
|
||||
{
|
||||
return CellChargerStatus.Off;
|
||||
}
|
||||
@@ -227,34 +233,39 @@ namespace Content.Server.GameObjects.Components.Power.ApcNetComponents.PowerRece
|
||||
{
|
||||
// Not called UpdateAppearance just because it messes with the load
|
||||
var status = GetStatus();
|
||||
if (_status == status)
|
||||
if (_status == status ||
|
||||
!Owner.TryGetComponent(out PowerReceiverComponent? receiver))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_status = status;
|
||||
Owner.TryGetComponent(out AppearanceComponent? appearance);
|
||||
|
||||
switch (_status)
|
||||
{
|
||||
// Update load just in case
|
||||
case CellChargerStatus.Off:
|
||||
_powerReceiver.Load = 0;
|
||||
_appearanceComponent?.SetData(CellVisual.Light, CellChargerStatus.Off);
|
||||
receiver.Load = 0;
|
||||
appearance?.SetData(CellVisual.Light, CellChargerStatus.Off);
|
||||
break;
|
||||
case CellChargerStatus.Empty:
|
||||
_powerReceiver.Load = 0;
|
||||
_appearanceComponent?.SetData(CellVisual.Light, CellChargerStatus.Empty); ;
|
||||
receiver.Load = 0;
|
||||
appearance?.SetData(CellVisual.Light, CellChargerStatus.Empty);
|
||||
break;
|
||||
case CellChargerStatus.Charging:
|
||||
_powerReceiver.Load = (int) (_chargeRate / _transferEfficiency);
|
||||
_appearanceComponent?.SetData(CellVisual.Light, CellChargerStatus.Charging);
|
||||
receiver.Load = (int) (_chargeRate / _transferEfficiency);
|
||||
appearance?.SetData(CellVisual.Light, CellChargerStatus.Charging);
|
||||
break;
|
||||
case CellChargerStatus.Charged:
|
||||
_powerReceiver.Load = 0;
|
||||
_appearanceComponent?.SetData(CellVisual.Light, CellChargerStatus.Charged);
|
||||
receiver.Load = 0;
|
||||
appearance?.SetData(CellVisual.Light, CellChargerStatus.Charged);
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
_appearanceComponent?.SetData(CellVisual.Occupied, _container.ContainedEntity != null);
|
||||
|
||||
appearance?.SetData(CellVisual.Occupied, _container.ContainedEntity != null);
|
||||
}
|
||||
|
||||
public void OnUpdate(float frameTime) //todo: make single system for this
|
||||
@@ -268,10 +279,17 @@ namespace Content.Server.GameObjects.Components.Power.ApcNetComponents.PowerRece
|
||||
|
||||
private void TransferPower(float frameTime)
|
||||
{
|
||||
if (!_powerReceiver.Powered)
|
||||
if (Owner.TryGetComponent(out PowerReceiverComponent? receiver) &&
|
||||
!receiver.Powered)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (_heldBattery == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_heldBattery.CurrentCharge += _chargeRate * frameTime;
|
||||
// Just so the sprite won't be set to 99.99999% visibility
|
||||
if (_heldBattery.MaxCharge - _heldBattery.CurrentCharge < 0.01)
|
||||
|
||||
@@ -108,7 +108,11 @@ namespace Content.Server.GameObjects.Components.Power.ApcNetComponents.PowerRece
|
||||
|
||||
public void UpdateColor()
|
||||
{
|
||||
var sprite = Owner.GetComponent<SpriteComponent>();
|
||||
if (!Owner.TryGetComponent(out SpriteComponent sprite))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
sprite.Color = Color;
|
||||
}
|
||||
|
||||
|
||||
@@ -225,14 +225,18 @@ namespace Content.Server.GameObjects.Components.Power.ApcNetComponents.PowerRece
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
Owner.GetComponent<PowerReceiverComponent>().OnPowerStateChanged += UpdateLight;
|
||||
Owner.EnsureComponent<PowerReceiverComponent>().OnPowerStateChanged += UpdateLight;
|
||||
|
||||
_lightBulbContainer = ContainerManagerComponent.Ensure<ContainerSlot>("light_bulb", Owner);
|
||||
}
|
||||
|
||||
public override void OnRemove()
|
||||
{
|
||||
Owner.GetComponent<PowerReceiverComponent>().OnPowerStateChanged -= UpdateLight;
|
||||
if (Owner.TryGetComponent(out PowerReceiverComponent receiver))
|
||||
{
|
||||
receiver.OnPowerStateChanged -= UpdateLight;
|
||||
}
|
||||
|
||||
base.OnRemove();
|
||||
}
|
||||
|
||||
|
||||
@@ -13,12 +13,9 @@ namespace Content.Server.GameObjects.Components.Power
|
||||
{
|
||||
public override string Name => "PowerCell";
|
||||
|
||||
private AppearanceComponent _appearance;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
_appearance = Owner.GetComponent<AppearanceComponent>();
|
||||
CurrentCharge = MaxCharge;
|
||||
UpdateVisuals();
|
||||
}
|
||||
@@ -31,7 +28,10 @@ namespace Content.Server.GameObjects.Components.Power
|
||||
|
||||
private void UpdateVisuals()
|
||||
{
|
||||
_appearance?.SetData(PowerCellVisuals.ChargeLevel, CurrentCharge / MaxCharge);
|
||||
if (Owner.TryGetComponent(out AppearanceComponent appearance))
|
||||
{
|
||||
appearance.SetData(PowerCellVisuals.ChargeLevel, CurrentCharge / MaxCharge);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,8 +31,9 @@ namespace Content.Server.GameObjects.Components.Power.PowerNetComponents
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
_battery = Owner.GetComponent<BatteryComponent>();
|
||||
_supplier = Owner.GetComponent<PowerSupplierComponent>();
|
||||
|
||||
_battery = Owner.EnsureComponent<BatteryComponent>();
|
||||
_supplier = Owner.EnsureComponent<PowerSupplierComponent>();
|
||||
UpdateSupplyRate();
|
||||
}
|
||||
|
||||
|
||||
@@ -31,8 +31,9 @@ namespace Content.Server.GameObjects.Components.Power.PowerNetComponents
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
_battery = Owner.GetComponent<BatteryComponent>();
|
||||
Consumer = Owner.GetComponent<PowerConsumerComponent>();
|
||||
|
||||
_battery = Owner.EnsureComponent<BatteryComponent>();
|
||||
Consumer = Owner.EnsureComponent<PowerConsumerComponent>();
|
||||
UpdateDrawRate();
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
#nullable enable
|
||||
using System;
|
||||
using Content.Shared.GameObjects.Components.Power;
|
||||
using Content.Shared.Utility;
|
||||
using Robust.Server.GameObjects;
|
||||
@@ -16,13 +17,11 @@ namespace Content.Server.GameObjects.Components.Power.PowerNetComponents
|
||||
[RegisterComponent]
|
||||
public class SmesComponent : Component
|
||||
{
|
||||
[Dependency] private readonly IGameTiming _gameTiming = default!;
|
||||
|
||||
public override string Name => "Smes";
|
||||
|
||||
private BatteryComponent _battery;
|
||||
|
||||
private AppearanceComponent _appearance;
|
||||
|
||||
private int _lastChargeLevel = 0;
|
||||
private int _lastChargeLevel;
|
||||
|
||||
private TimeSpan _lastChargeLevelChange;
|
||||
|
||||
@@ -32,15 +31,12 @@ namespace Content.Server.GameObjects.Components.Power.PowerNetComponents
|
||||
|
||||
private const int VisualsChangeDelay = 1;
|
||||
|
||||
#pragma warning disable 649
|
||||
[Dependency] private readonly IGameTiming _gameTiming;
|
||||
#pragma warning restore 649
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
_battery = Owner.GetComponent<BatteryComponent>();
|
||||
_appearance = Owner.GetComponent<AppearanceComponent>();
|
||||
|
||||
Owner.EnsureComponent<BatteryComponent>();
|
||||
Owner.EnsureComponent<AppearanceComponent>();
|
||||
}
|
||||
|
||||
public void OnUpdate()
|
||||
@@ -50,7 +46,11 @@ namespace Content.Server.GameObjects.Components.Power.PowerNetComponents
|
||||
{
|
||||
_lastChargeLevel = newLevel;
|
||||
_lastChargeLevelChange = _gameTiming.CurTime;
|
||||
_appearance.SetData(SmesVisuals.LastChargeLevel, newLevel);
|
||||
|
||||
if (Owner.TryGetComponent(out AppearanceComponent? appearance))
|
||||
{
|
||||
appearance.SetData(SmesVisuals.LastChargeLevel, newLevel);
|
||||
}
|
||||
}
|
||||
|
||||
var newChargeState = GetNewChargeState();
|
||||
@@ -58,13 +58,22 @@ namespace Content.Server.GameObjects.Components.Power.PowerNetComponents
|
||||
{
|
||||
_lastChargeState = newChargeState;
|
||||
_lastChargeStateChange = _gameTiming.CurTime;
|
||||
_appearance.SetData(SmesVisuals.LastChargeState, newChargeState);
|
||||
|
||||
if (Owner.TryGetComponent(out AppearanceComponent? appearance))
|
||||
{
|
||||
appearance.SetData(SmesVisuals.LastChargeState, newChargeState);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private int GetNewChargeLevel()
|
||||
{
|
||||
return ContentHelpers.RoundToLevels(_battery.CurrentCharge, _battery.MaxCharge, 6);
|
||||
if (!Owner.TryGetComponent(out BatteryComponent? battery))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
return ContentHelpers.RoundToLevels(battery.CurrentCharge, battery.MaxCharge, 6);
|
||||
}
|
||||
|
||||
private ChargeState GetNewChargeState()
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using Content.Server.GameObjects.Components.Power.ApcNetComponents;
|
||||
#nullable enable
|
||||
using Content.Server.GameObjects.Components.Power.ApcNetComponents;
|
||||
using Content.Server.GameObjects.EntitySystems;
|
||||
using Content.Shared.GameObjects.Components.Power;
|
||||
using Content.Shared.Interfaces.GameObjects.Components;
|
||||
@@ -7,6 +8,7 @@ using Robust.Server.Interfaces.GameObjects;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Interfaces.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.ViewVariables;
|
||||
|
||||
namespace Content.Server.GameObjects.Components.Power.PowerNetComponents
|
||||
{
|
||||
@@ -14,28 +16,34 @@ namespace Content.Server.GameObjects.Components.Power.PowerNetComponents
|
||||
[ComponentReference(typeof(IActivate))]
|
||||
public class SolarControlConsoleComponent : SharedSolarControlConsoleComponent, IActivate
|
||||
{
|
||||
#pragma warning disable 649
|
||||
[Dependency] private IEntitySystemManager _entitySystemManager;
|
||||
#pragma warning restore 649
|
||||
[Dependency] private readonly IEntitySystemManager _entitySystemManager = default!;
|
||||
|
||||
private BoundUserInterface _userInterface;
|
||||
private PowerReceiverComponent _powerReceiver;
|
||||
private PowerSolarSystem _powerSolarSystem;
|
||||
private bool Powered => _powerReceiver.Powered;
|
||||
private PowerSolarSystem _powerSolarSystem = default!;
|
||||
private bool Powered => !Owner.TryGetComponent(out PowerReceiverComponent? receiver) || receiver.Powered;
|
||||
|
||||
[ViewVariables]
|
||||
private BoundUserInterface? UserInterface =>
|
||||
Owner.TryGetComponent(out ServerUserInterfaceComponent? ui) &&
|
||||
ui.TryGetBoundUserInterface(SolarControlConsoleUiKey.Key, out var boundUi)
|
||||
? boundUi
|
||||
: null;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
_userInterface = Owner.GetComponent<ServerUserInterfaceComponent>().GetBoundUserInterface(SolarControlConsoleUiKey.Key);
|
||||
_userInterface.OnReceiveMessage += UserInterfaceOnReceiveMessage;
|
||||
_powerReceiver = Owner.GetComponent<PowerReceiverComponent>();
|
||||
if (UserInterface != null)
|
||||
{
|
||||
UserInterface.OnReceiveMessage += UserInterfaceOnReceiveMessage;
|
||||
}
|
||||
|
||||
Owner.EnsureComponent<PowerReceiverComponent>();
|
||||
_powerSolarSystem = _entitySystemManager.GetEntitySystem<PowerSolarSystem>();
|
||||
}
|
||||
|
||||
public void UpdateUIState()
|
||||
{
|
||||
_userInterface.SetState(new SolarControlConsoleBoundInterfaceState(_powerSolarSystem.TargetPanelRotation, _powerSolarSystem.TargetPanelVelocity, _powerSolarSystem.TotalPanelPower, _powerSolarSystem.TowardsSun));
|
||||
UserInterface?.SetState(new SolarControlConsoleBoundInterfaceState(_powerSolarSystem.TargetPanelRotation, _powerSolarSystem.TargetPanelVelocity, _powerSolarSystem.TotalPanelPower, _powerSolarSystem.TowardsSun));
|
||||
}
|
||||
|
||||
private void UserInterfaceOnReceiveMessage(ServerBoundUserInterfaceMessage obj)
|
||||
@@ -57,7 +65,7 @@ namespace Content.Server.GameObjects.Components.Power.PowerNetComponents
|
||||
|
||||
void IActivate.Activate(ActivateEventArgs eventArgs)
|
||||
{
|
||||
if (!eventArgs.User.TryGetComponent(out IActorComponent actor))
|
||||
if (!eventArgs.User.TryGetComponent(out IActorComponent? actor))
|
||||
{
|
||||
return;
|
||||
}
|
||||
@@ -69,7 +77,7 @@ namespace Content.Server.GameObjects.Components.Power.PowerNetComponents
|
||||
|
||||
// always update the UI immediately before opening, just in case
|
||||
UpdateUIState();
|
||||
_userInterface.Open(actor.playerSession);
|
||||
UserInterface?.Open(actor.playerSession);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user