using Content.Shared.Chemistry.EntitySystems;
using Content.Shared.Administration;
using Content.Shared.Chemistry.Components.SolutionManager;
using Content.Shared.Chemistry.Reagent;
using Content.Shared.FixedPoint;
using Robust.Shared.Console;
using Robust.Shared.Prototypes;
using System.Linq;
namespace Content.Server.Administration.Commands
{
///
/// Command that allows you to edit an existing solution by adding (or removing) reagents.
///
[AdminCommand(AdminFlags.Admin)]
public sealed class AddReagent : IConsoleCommand
{
[Dependency] private readonly IEntityManager _entManager = default!;
[Dependency] private readonly IPrototypeManager _protomanager = default!;
public string Command => "addreagent";
public string Description => "Add (or remove) some amount of reagent from some solution.";
public string Help => $"Usage: {Command} ";
public void Execute(IConsoleShell shell, string argStr, string[] args)
{
if (args.Length < 4)
{
shell.WriteLine($"Not enough arguments.\n{Help}");
return;
}
if (!NetEntity.TryParse(args[0], out var uidNet) || !_entManager.TryGetEntity(uidNet, out var uid))
{
shell.WriteLine($"Invalid entity id.");
return;
}
if (!_entManager.TryGetComponent(uid, out SolutionContainerManagerComponent? man))
{
shell.WriteLine($"Entity does not have any solutions.");
return;
}
var solutionContainerSystem = _entManager.System();
if (!solutionContainerSystem.TryGetSolution((uid.Value, man), args[1], out var solution))
{
var validSolutions = string.Join(", ", solutionContainerSystem.EnumerateSolutions((uid.Value, man)).Select(s => s.Name));
shell.WriteLine($"Entity does not have a \"{args[1]}\" solution. Valid solutions are:\n{validSolutions}");
return;
}
if (!_protomanager.HasIndex(args[2]))
{
shell.WriteLine($"Unknown reagent prototype");
return;
}
if (!float.TryParse(args[3], out var quantityFloat))
{
shell.WriteLine($"Failed to parse quantity");
return;
}
var quantity = FixedPoint2.New(MathF.Abs(quantityFloat));
if (quantityFloat > 0)
solutionContainerSystem.TryAddReagent(solution.Value, args[2], quantity, out _);
else
solutionContainerSystem.RemoveReagent(solution.Value, args[2], quantity);
}
}
}