Fix and refactor SolutionContainerSystem.RemoveEachReagent (#7245)

This commit is contained in:
Vera Aguilera Puerto
2022-03-23 14:05:10 +01:00
committed by GitHub
parent be6431f7aa
commit 46ac70a734
3 changed files with 39 additions and 34 deletions

View File

@@ -167,34 +167,41 @@ namespace Content.Shared.Chemistry.Components
return FixedPoint2.New(0);
}
public void RemoveReagent(string reagentId, FixedPoint2 quantity)
/// <summary>
/// Attempts to remove an amount of reagent from the solution.
/// </summary>
/// <param name="reagentId">The reagent to be removed.</param>
/// <param name="quantity">The amount of reagent to remove.</param>
/// <returns>How much reagent was actually removed. Zero if the reagent is not present on the solution.</returns>
public FixedPoint2 RemoveReagent(string reagentId, FixedPoint2 quantity)
{
if(quantity <= 0)
return;
return FixedPoint2.Zero;
for (var i = 0; i < Contents.Count; i++)
{
var reagent = Contents[i];
if(reagent.ReagentId != reagentId)
continue;
if (!IoCManager.Resolve<IPrototypeManager>().TryIndex(reagentId, out ReagentPrototype? proto))
proto = new ReagentPrototype();
var curQuantity = reagent.Quantity;
var newQuantity = curQuantity - quantity;
if (newQuantity <= 0)
{
Contents.RemoveSwap(i);
TotalVolume -= curQuantity;
}
else
{
Contents[i] = new ReagentQuantity(reagentId, newQuantity);
TotalVolume -= quantity;
return curQuantity;
}
return;
Contents[i] = new ReagentQuantity(reagentId, newQuantity);
TotalVolume -= quantity;
return quantity;
}
// Reagent is not on the solution...
return FixedPoint2.Zero;
}
/// <summary>