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.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
@@ -39,25 +40,19 @@ namespace Content.Server.GameObjects.Components.Kitchen
|
||||
[ComponentReference(typeof(IActivate))]
|
||||
public class MicrowaveComponent : SharedMicrowaveComponent, IActivate, IInteractUsing, ISolutionChange, ISuicideAct
|
||||
{
|
||||
#pragma warning disable 649
|
||||
[Dependency] private readonly IEntityManager _entityManager;
|
||||
[Dependency] private readonly RecipeManager _recipeManager;
|
||||
[Dependency] private readonly IServerNotifyManager _notifyManager;
|
||||
#pragma warning restore 649
|
||||
[Dependency] private readonly IEntityManager _entityManager = default!;
|
||||
[Dependency] private readonly RecipeManager _recipeManager = default!;
|
||||
[Dependency] private readonly IServerNotifyManager _notifyManager = default!;
|
||||
|
||||
#region YAMLSERIALIZE
|
||||
private int _cookTimeDefault;
|
||||
private int _cookTimeMultiplier; //For upgrades and stuff I guess?
|
||||
private string _badRecipeName;
|
||||
private string _startCookingSound;
|
||||
private string _cookingCompleteSound;
|
||||
private string _badRecipeName = "";
|
||||
private string _startCookingSound = "";
|
||||
private string _cookingCompleteSound = "";
|
||||
#endregion
|
||||
|
||||
#region VIEWVARIABLES
|
||||
[ViewVariables]
|
||||
private SolutionComponent _solution;
|
||||
|
||||
[ViewVariables]
|
||||
[ViewVariables]
|
||||
private bool _busy = false;
|
||||
|
||||
/// <summary>
|
||||
@@ -67,20 +62,23 @@ namespace Content.Server.GameObjects.Components.Kitchen
|
||||
/// </summary>
|
||||
[ViewVariables]
|
||||
private uint _currentCookTimerTime = 1;
|
||||
#endregion
|
||||
|
||||
private bool _powered => _powerReceiver.Powered;
|
||||
private bool _hasContents => _solution.ReagentList.Count > 0 || _storage.ContainedEntities.Count > 0;
|
||||
private bool Powered => !Owner.TryGetComponent(out PowerReceiverComponent? receiver) || receiver.Powered;
|
||||
private bool _hasContents => Owner.TryGetComponent(out SolutionComponent? solution) && (solution.ReagentList.Count > 0 || _storage.ContainedEntities.Count > 0);
|
||||
private bool _uiDirty = true;
|
||||
private bool _lostPower = false;
|
||||
private int _currentCookTimeButtonIndex = 0;
|
||||
|
||||
void ISolutionChange.SolutionChanged(SolutionChangeEventArgs eventArgs) => _uiDirty = true;
|
||||
private AudioSystem _audioSystem;
|
||||
private AppearanceComponent _appearance;
|
||||
private PowerReceiverComponent _powerReceiver;
|
||||
private BoundUserInterface _userInterface;
|
||||
private Container _storage;
|
||||
private AudioSystem _audioSystem = default!;
|
||||
private Container _storage = default!;
|
||||
|
||||
[ViewVariables]
|
||||
private BoundUserInterface? UserInterface =>
|
||||
Owner.TryGetComponent(out ServerUserInterfaceComponent? ui) &&
|
||||
ui.TryGetBoundUserInterface(MicrowaveUiKey.Key, out var boundUi)
|
||||
? boundUi
|
||||
: null;
|
||||
|
||||
public override void ExposeData(ObjectSerializer serializer)
|
||||
{
|
||||
@@ -95,22 +93,21 @@ namespace Content.Server.GameObjects.Components.Kitchen
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
_solution ??= Owner.TryGetComponent(out SolutionComponent solutionComponent)
|
||||
? solutionComponent
|
||||
: Owner.AddComponent<SolutionComponent>();
|
||||
|
||||
Owner.EnsureComponent<SolutionComponent>();
|
||||
|
||||
_storage = ContainerManagerComponent.Ensure<Container>("microwave_entity_container", Owner, out var existed);
|
||||
_appearance = Owner.GetComponent<AppearanceComponent>();
|
||||
_powerReceiver = Owner.GetComponent<PowerReceiverComponent>();
|
||||
_audioSystem = EntitySystem.Get<AudioSystem>();
|
||||
_userInterface = Owner.GetComponent<ServerUserInterfaceComponent>()
|
||||
.GetBoundUserInterface(MicrowaveUiKey.Key);
|
||||
_userInterface.OnReceiveMessage += UserInterfaceOnReceiveMessage;
|
||||
|
||||
if (UserInterface != null)
|
||||
{
|
||||
UserInterface.OnReceiveMessage += UserInterfaceOnReceiveMessage;
|
||||
}
|
||||
}
|
||||
|
||||
private void UserInterfaceOnReceiveMessage(ServerBoundUserInterfaceMessage message)
|
||||
{
|
||||
if (!_powered || _busy)
|
||||
if (!Powered || _busy)
|
||||
{
|
||||
return;
|
||||
}
|
||||
@@ -158,13 +155,13 @@ namespace Content.Server.GameObjects.Components.Kitchen
|
||||
public void OnUpdate()
|
||||
{
|
||||
|
||||
if (!_powered)
|
||||
if (!Powered)
|
||||
{
|
||||
//TODO:If someone cuts power currently, microwave magically keeps going. FIX IT!
|
||||
SetAppearance(MicrowaveVisualState.Idle);
|
||||
}
|
||||
|
||||
if (_busy && !_powered)
|
||||
if (_busy && !Powered)
|
||||
{
|
||||
//we lost power while we were cooking/busy!
|
||||
_lostPower = true;
|
||||
@@ -174,11 +171,11 @@ namespace Content.Server.GameObjects.Components.Kitchen
|
||||
_uiDirty = true;
|
||||
}
|
||||
|
||||
if (_uiDirty)
|
||||
if (_uiDirty && Owner.TryGetComponent(out SolutionComponent? solution))
|
||||
{
|
||||
_userInterface.SetState(new MicrowaveUpdateUserInterfaceState
|
||||
UserInterface?.SetState(new MicrowaveUpdateUserInterfaceState
|
||||
(
|
||||
_solution.Solution.Contents.ToArray(),
|
||||
solution.Solution.Contents.ToArray(),
|
||||
_storage.ContainedEntities.Select(item => item.Uid).ToArray(),
|
||||
_busy,
|
||||
_currentCookTimeButtonIndex,
|
||||
@@ -190,26 +187,26 @@ namespace Content.Server.GameObjects.Components.Kitchen
|
||||
|
||||
private void SetAppearance(MicrowaveVisualState state)
|
||||
{
|
||||
if (_appearance != null || Owner.TryGetComponent(out _appearance))
|
||||
if (Owner.TryGetComponent(out AppearanceComponent? appearance))
|
||||
{
|
||||
_appearance.SetData(PowerDeviceVisuals.VisualState, state);
|
||||
appearance.SetData(PowerDeviceVisuals.VisualState, state);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void IActivate.Activate(ActivateEventArgs eventArgs)
|
||||
{
|
||||
if (!eventArgs.User.TryGetComponent(out IActorComponent actor) || !_powered)
|
||||
if (!eventArgs.User.TryGetComponent(out IActorComponent? actor) || !Powered)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_uiDirty = true;
|
||||
_userInterface.Open(actor.playerSession);
|
||||
UserInterface?.Open(actor.playerSession);
|
||||
}
|
||||
|
||||
public async Task<bool> InteractUsing(InteractUsingEventArgs eventArgs)
|
||||
{
|
||||
if (!_powered)
|
||||
if (!Powered)
|
||||
{
|
||||
_notifyManager.PopupMessage(Owner.Transform.GridPosition, eventArgs.User,
|
||||
Loc.GetString("It has no power!"));
|
||||
@@ -232,8 +229,13 @@ namespace Content.Server.GameObjects.Components.Kitchen
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!Owner.TryGetComponent(out SolutionComponent? solution))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
//Get transfer amount. May be smaller than _transferAmount if not enough room
|
||||
var realTransferAmount = ReagentUnit.Min(attackPourable.TransferAmount, _solution.EmptyVolume);
|
||||
var realTransferAmount = ReagentUnit.Min(attackPourable.TransferAmount, solution.EmptyVolume);
|
||||
if (realTransferAmount <= 0) //Special message if container is full
|
||||
{
|
||||
_notifyManager.PopupMessage(Owner.Transform.GridPosition, eventArgs.User,
|
||||
@@ -243,7 +245,7 @@ namespace Content.Server.GameObjects.Components.Kitchen
|
||||
|
||||
//Move units from attackSolution to targetSolution
|
||||
var removedSolution = attackSolution.SplitSolution(realTransferAmount);
|
||||
if (!_solution.TryAddSolution(removedSolution))
|
||||
if (!solution.TryAddSolution(removedSolution))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
@@ -280,6 +282,11 @@ namespace Content.Server.GameObjects.Components.Kitchen
|
||||
var solidsDict = new Dictionary<string, int>();
|
||||
foreach(var item in _storage.ContainedEntities)
|
||||
{
|
||||
if (item.Prototype == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if(solidsDict.ContainsKey(item.Prototype.ID))
|
||||
{
|
||||
solidsDict[item.Prototype.ID]++;
|
||||
@@ -303,7 +310,7 @@ namespace Content.Server.GameObjects.Components.Kitchen
|
||||
}
|
||||
|
||||
// Check recipes
|
||||
FoodRecipePrototype recipeToCook = null;
|
||||
FoodRecipePrototype? recipeToCook = null;
|
||||
foreach (var r in _recipeManager.Recipes.Where(r => CanSatisfyRecipe(r, solidsDict) == MicrowaveSuccessState.RecipePass))
|
||||
{
|
||||
recipeToCook = r;
|
||||
@@ -330,7 +337,7 @@ namespace Content.Server.GameObjects.Components.Kitchen
|
||||
{
|
||||
if (goodMeal)
|
||||
{
|
||||
SubtractContents(recipeToCook);
|
||||
SubtractContents(recipeToCook!);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -357,12 +364,18 @@ namespace Content.Server.GameObjects.Components.Kitchen
|
||||
|
||||
private void VaporizeReagents()
|
||||
{
|
||||
_solution.RemoveAllSolution();
|
||||
if (Owner.TryGetComponent(out SolutionComponent? solution))
|
||||
{
|
||||
solution.RemoveAllSolution();
|
||||
}
|
||||
}
|
||||
|
||||
private void VaporizeReagentQuantity(Solution.ReagentQuantity reagentQuantity)
|
||||
{
|
||||
_solution.TryRemoveReagent(reagentQuantity.ReagentId, reagentQuantity.Quantity);
|
||||
if (Owner.TryGetComponent(out SolutionComponent? solution))
|
||||
{
|
||||
solution?.TryRemoveReagent(reagentQuantity.ReagentId, reagentQuantity.Quantity);
|
||||
}
|
||||
}
|
||||
|
||||
private void VaporizeSolids()
|
||||
@@ -395,9 +408,14 @@ namespace Content.Server.GameObjects.Components.Kitchen
|
||||
|
||||
private void SubtractContents(FoodRecipePrototype recipe)
|
||||
{
|
||||
if (!Owner.TryGetComponent(out SolutionComponent? solution))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
foreach(var recipeReagent in recipe.IngredientsReagents)
|
||||
{
|
||||
_solution.TryRemoveReagent(recipeReagent.Key, ReagentUnit.New(recipeReagent.Value));
|
||||
solution?.TryRemoveReagent(recipeReagent.Key, ReagentUnit.New(recipeReagent.Value));
|
||||
}
|
||||
|
||||
foreach (var recipeSolid in recipe.IngredientsSolids)
|
||||
@@ -406,6 +424,11 @@ namespace Content.Server.GameObjects.Components.Kitchen
|
||||
{
|
||||
foreach (var item in _storage.ContainedEntities)
|
||||
{
|
||||
if (item.Prototype == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (item.Prototype.ID == recipeSolid.Key)
|
||||
{
|
||||
_storage.Remove(item);
|
||||
@@ -420,9 +443,14 @@ namespace Content.Server.GameObjects.Components.Kitchen
|
||||
|
||||
private MicrowaveSuccessState CanSatisfyRecipe(FoodRecipePrototype recipe, Dictionary<string,int> solids)
|
||||
{
|
||||
if (!Owner.TryGetComponent(out SolutionComponent? solution))
|
||||
{
|
||||
return MicrowaveSuccessState.RecipeFail;
|
||||
}
|
||||
|
||||
foreach (var reagent in recipe.IngredientsReagents)
|
||||
{
|
||||
if (!_solution.ContainsReagent(reagent.Key, out var amount))
|
||||
if (!solution.ContainsReagent(reagent.Key, out var amount))
|
||||
{
|
||||
return MicrowaveSuccessState.RecipeFail;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user