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

@@ -1,4 +1,6 @@
using System;
#nullable enable
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Content.Server.GameObjects.Components.GUI;
@@ -40,24 +42,26 @@ namespace Content.Server.GameObjects.Components.Chemistry
[ComponentReference(typeof(IInteractUsing))]
public class ChemMasterComponent : SharedChemMasterComponent, IActivate, IInteractUsing, ISolutionChange
{
#pragma warning disable 649
[Dependency] private readonly IServerNotifyManager _notifyManager;
[Dependency] private readonly ILocalizationManager _localizationManager;
#pragma warning restore 649
[Dependency] private readonly IServerNotifyManager _notifyManager = default!;
[Dependency] private readonly ILocalizationManager _localizationManager = default!;
[ViewVariables] private BoundUserInterface _userInterface;
[ViewVariables] private ContainerSlot _beakerContainer;
[ViewVariables] private string _packPrototypeId;
[ViewVariables] private ContainerSlot _beakerContainer = default!;
[ViewVariables] private string _packPrototypeId = "";
[ViewVariables] private bool HasBeaker => _beakerContainer.ContainedEntity != null;
[ViewVariables] private bool BufferModeTransfer = true;
[ViewVariables] private bool _bufferModeTransfer = true;
private PowerReceiverComponent _powerReceiver;
private bool Powered => _powerReceiver.Powered;
private bool Powered => !Owner.TryGetComponent(out PowerReceiverComponent? receiver) || receiver.Powered;
private readonly SolutionComponent BufferSolution = new SolutionComponent();
[ViewVariables]
private BoundUserInterface? UserInterface =>
Owner.TryGetComponent(out ServerUserInterfaceComponent? ui) &&
ui.TryGetBoundUserInterface(ChemMasterUiKey.Key, out var boundUi)
? boundUi
: null;
/// <summary>
/// Shows the serializer how to save/load this components yaml prototype.
@@ -77,14 +81,19 @@ namespace Content.Server.GameObjects.Components.Chemistry
public override void Initialize()
{
base.Initialize();
_userInterface = Owner.GetComponent<ServerUserInterfaceComponent>()
.GetBoundUserInterface(ChemMasterUiKey.Key);
_userInterface.OnReceiveMessage += OnUiReceiveMessage;
if (UserInterface != null)
{
UserInterface.OnReceiveMessage += OnUiReceiveMessage;
}
_beakerContainer =
ContainerManagerComponent.Ensure<ContainerSlot>($"{Name}-reagentContainerContainer", Owner);
_powerReceiver = Owner.GetComponent<PowerReceiverComponent>();
_powerReceiver.OnPowerStateChanged += OnPowerChanged;
if (Owner.TryGetComponent(out PowerReceiverComponent? receiver))
{
receiver.OnPowerStateChanged += OnPowerChanged;
}
//BufferSolution = Owner.BufferSolution
BufferSolution.Solution = new Solution();
@@ -93,7 +102,7 @@ namespace Content.Server.GameObjects.Components.Chemistry
UpdateUserInterface();
}
private void OnPowerChanged(object sender, PowerStateEventArgs e)
private void OnPowerChanged(object? sender, PowerStateEventArgs e)
{
UpdateUserInterface();
}
@@ -105,6 +114,11 @@ namespace Content.Server.GameObjects.Components.Chemistry
/// <param name="obj">A user interface message from the client.</param>
private void OnUiReceiveMessage(ServerBoundUserInterfaceMessage obj)
{
if (obj.Session.AttachedEntity == null)
{
return;
}
var msg = (UiActionMessage) obj.Message;
var needsPower = msg.action switch
{
@@ -124,11 +138,11 @@ namespace Content.Server.GameObjects.Components.Chemistry
TransferReagent(msg.id, msg.amount, msg.isBuffer);
break;
case UiAction.Transfer:
BufferModeTransfer = true;
_bufferModeTransfer = true;
UpdateUserInterface();
break;
case UiAction.Discard:
BufferModeTransfer = false;
_bufferModeTransfer = false;
UpdateUserInterface();
break;
case UiAction.CreatePills:
@@ -147,7 +161,7 @@ namespace Content.Server.GameObjects.Components.Chemistry
/// </summary>
/// <param name="playerEntity">The player entity.</param>
/// <returns>Returns true if the entity can use the chem master, and false if it cannot.</returns>
private bool PlayerCanUseChemMaster(IEntity playerEntity, bool needsPower = true)
private bool PlayerCanUseChemMaster(IEntity? playerEntity, bool needsPower = true)
{
//Need player entity to check if they are still able to use the chem master
if (playerEntity == null)
@@ -172,18 +186,18 @@ namespace Content.Server.GameObjects.Components.Chemistry
if (beaker == null)
{
return new ChemMasterBoundUserInterfaceState(Powered, false, ReagentUnit.New(0), ReagentUnit.New(0),
"", Owner.Name, null, BufferSolution.ReagentList.ToList(), BufferModeTransfer, BufferSolution.CurrentVolume, BufferSolution.MaxVolume);
"", Owner.Name, new List<Solution.ReagentQuantity>(), BufferSolution.ReagentList.ToList(), _bufferModeTransfer, BufferSolution.CurrentVolume, BufferSolution.MaxVolume);
}
var solution = beaker.GetComponent<SolutionComponent>();
return new ChemMasterBoundUserInterfaceState(Powered, true, solution.CurrentVolume, solution.MaxVolume,
beaker.Name, Owner.Name, solution.ReagentList.ToList(), BufferSolution.ReagentList.ToList(), BufferModeTransfer, BufferSolution.CurrentVolume, BufferSolution.MaxVolume);
beaker.Name, Owner.Name, solution.ReagentList.ToList(), BufferSolution.ReagentList.ToList(), _bufferModeTransfer, BufferSolution.CurrentVolume, BufferSolution.MaxVolume);
}
private void UpdateUserInterface()
{
var state = GetUserInterfaceState();
_userInterface.SetState(state);
UserInterface?.SetState(state);
}
/// <summary>
@@ -207,7 +221,7 @@ namespace Content.Server.GameObjects.Components.Chemistry
private void TransferReagent(string id, ReagentUnit amount, bool isBuffer)
{
if (!HasBeaker && BufferModeTransfer) return;
if (!HasBeaker && _bufferModeTransfer) return;
var beaker = _beakerContainer.ContainedEntity;
var beakerSolution = beaker.GetComponent<SolutionComponent>();
if (isBuffer)
@@ -227,7 +241,7 @@ namespace Content.Server.GameObjects.Components.Chemistry
}
BufferSolution.Solution.RemoveReagent(id, actualAmount);
if (BufferModeTransfer)
if (_bufferModeTransfer)
{
beakerSolution.Solution.AddReagent(id, actualAmount);
}
@@ -351,12 +365,12 @@ namespace Content.Server.GameObjects.Components.Chemistry
/// <param name="args">Data relevant to the event such as the actor which triggered it.</param>
void IActivate.Activate(ActivateEventArgs args)
{
if (!args.User.TryGetComponent(out IActorComponent actor))
if (!args.User.TryGetComponent(out IActorComponent? actor))
{
return;
}
if (!args.User.TryGetComponent(out IHandsComponent hands))
if (!args.User.TryGetComponent(out IHandsComponent? hands))
{
_notifyManager.PopupMessage(Owner.Transform.GridPosition, args.User,
_localizationManager.GetString("You have no hands."));
@@ -366,7 +380,7 @@ namespace Content.Server.GameObjects.Components.Chemistry
var activeHandEntity = hands.GetActiveHand?.Owner;
if (activeHandEntity == null)
{
_userInterface.Open(actor.playerSession);
UserInterface?.Open(actor.playerSession);
}
}
@@ -379,13 +393,20 @@ namespace Content.Server.GameObjects.Components.Chemistry
/// <returns></returns>
async Task<bool> IInteractUsing.InteractUsing(InteractUsingEventArgs args)
{
if (!args.User.TryGetComponent(out IHandsComponent hands))
if (!args.User.TryGetComponent(out IHandsComponent? hands))
{
_notifyManager.PopupMessage(Owner.Transform.GridPosition, args.User,
_localizationManager.GetString("You have no hands."));
return true;
}
if (hands.GetActiveHand == null)
{
_notifyManager.PopupMessage(Owner.Transform.GridPosition, args.User,
Loc.GetString("You have nothing on your hand."));
return false;
}
var activeHandEntity = hands.GetActiveHand.Owner;
if (activeHandEntity.TryGetComponent<SolutionComponent>(out var solution))
{