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:
DrSmugleaf
2020-08-22 22:29:20 +02:00
committed by GitHub
parent c8178550b8
commit b9196d0a10
84 changed files with 1790 additions and 1123 deletions

View File

@@ -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();
}

View File

@@ -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();
}

View File

@@ -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()

View File

@@ -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);
}
}
}