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 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)
|
||||
|
||||
Reference in New Issue
Block a user