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,5 @@
using System;
#nullable enable
using System;
using System.Collections.Generic;
using Content.Server.GameObjects.Components.Body.Digestive;
using Content.Server.GameObjects.Components.Chemistry;
@@ -25,24 +26,30 @@ namespace Content.Server.GameObjects.Components.Nutrition
[ComponentReference(typeof(IAfterInteract))]
public class FoodComponent : Component, IUse, IAfterInteract
{
#pragma warning disable 649
[Dependency] private readonly IEntitySystemManager _entitySystem;
#pragma warning restore 649
[Dependency] private readonly IEntitySystemManager _entitySystem = default!;
public override string Name => "Food";
[ViewVariables]
private string _useSound;
[ViewVariables]
private string _trashPrototype;
[ViewVariables]
private SolutionComponent _contents;
[ViewVariables]
private ReagentUnit _transferAmount;
[ViewVariables] private string _useSound = "";
[ViewVariables] private string? _trashPrototype;
[ViewVariables] private ReagentUnit _transferAmount;
private UtensilType _utensilsNeeded;
public int UsesRemaining => _contents.CurrentVolume == 0
?
0 : Math.Max(1, (int)Math.Ceiling((_contents.CurrentVolume / _transferAmount).Float()));
[ViewVariables]
public int UsesRemaining
{
get
{
if (!Owner.TryGetComponent(out SolutionComponent? solution))
{
return 0;
}
return solution.CurrentVolume == 0
? 0
: Math.Max(1, (int)Math.Ceiling((solution.CurrentVolume / _transferAmount).Float()));
}
}
public override void ExposeData(ObjectSerializer serializer)
{
@@ -60,7 +67,7 @@ namespace Content.Server.GameObjects.Components.Nutrition
{
var types = new List<UtensilType>();
foreach (UtensilType type in Enum.GetValues(typeof(UtensilType)))
foreach (var type in (UtensilType[]) Enum.GetValues(typeof(UtensilType)))
{
if ((_utensilsNeeded & type) != 0)
{
@@ -75,8 +82,7 @@ namespace Content.Server.GameObjects.Components.Nutrition
public override void Initialize()
{
base.Initialize();
_contents = Owner.GetComponent<SolutionComponent>();
Owner.EnsureComponent<SolutionComponent>();
}
bool IUse.UseEntity(UseEntityEventArgs eventArgs)
@@ -101,8 +107,13 @@ namespace Content.Server.GameObjects.Components.Nutrition
TryUseFood(eventArgs.User, eventArgs.Target);
}
public virtual bool TryUseFood(IEntity user, IEntity target, UtensilComponent utensilUsed = null)
public virtual bool TryUseFood(IEntity? user, IEntity? target, UtensilComponent? utensilUsed = null)
{
if (!Owner.TryGetComponent(out SolutionComponent? solution))
{
return false;
}
if (user == null)
{
return false;
@@ -116,7 +127,7 @@ namespace Content.Server.GameObjects.Components.Nutrition
var trueTarget = target ?? user;
if (!trueTarget.TryGetComponent(out StomachComponent stomach))
if (!trueTarget.TryGetComponent(out StomachComponent? stomach))
{
return false;
}
@@ -130,11 +141,11 @@ namespace Content.Server.GameObjects.Components.Nutrition
utensils = new List<UtensilComponent>();
var types = UtensilType.None;
if (user.TryGetComponent(out HandsComponent hands))
if (user.TryGetComponent(out HandsComponent? hands))
{
foreach (var item in hands.GetAllHeldItems())
{
if (!item.Owner.TryGetComponent(out UtensilComponent utensil))
if (!item.Owner.TryGetComponent(out UtensilComponent? utensil))
{
continue;
}
@@ -156,11 +167,11 @@ namespace Content.Server.GameObjects.Components.Nutrition
return false;
}
var transferAmount = ReagentUnit.Min(_transferAmount, _contents.CurrentVolume);
var split = _contents.SplitSolution(transferAmount);
var transferAmount = ReagentUnit.Min(_transferAmount, solution.CurrentVolume);
var split = solution.SplitSolution(transferAmount);
if (!stomach.TryTransferSolution(split))
{
_contents.TryAddSolution(split);
solution.TryAddSolution(split);
trueTarget.PopupMessage(user, Loc.GetString("You can't eat any more!"));
return false;
}
@@ -194,13 +205,13 @@ namespace Content.Server.GameObjects.Components.Nutrition
var finisher = Owner.EntityManager.SpawnEntity(_trashPrototype, position);
// If the user is holding the item
if (user.TryGetComponent(out HandsComponent handsComponent) &&
if (user.TryGetComponent(out HandsComponent? handsComponent) &&
handsComponent.IsHolding(Owner))
{
Owner.Delete();
// Put the trash in the user's hand
if (finisher.TryGetComponent(out ItemComponent item) &&
if (finisher.TryGetComponent(out ItemComponent? item) &&
handsComponent.CanPutInHand(item))
{
handsComponent.PutInHand(item);