CatStation/Content.Server/Administration/Commands/AddReagent.cs
Pieter-Jan Briers 5168b5f3d4
IoC source gen compatibility (#43863)
* IoC source gen compatibility

Can be merged before or after https://github.com/space-wizards/RobustToolbox/pull/6549 doesn't really matter.

* Missed a spot
2026-05-09 03:29:58 +00:00

73 lines
2.8 KiB
C#

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
{
/// <summary>
/// Command that allows you to edit an existing solution by adding (or removing) reagents.
/// </summary>
[AdminCommand(AdminFlags.Admin)]
public sealed partial class AddReagent : IConsoleCommand
{
[Dependency] private IEntityManager _entManager = default!;
[Dependency] private 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} <target> <solution> <reagent> <quantity>";
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;
}
var solutionContainerSystem = _entManager.System<SharedSolutionContainerSystem>();
if (!solutionContainerSystem.TryGetSolution(uid.Value, args[1], out var solution))
{
var solutions = solutionContainerSystem.EnumerateSolutions(uid.Value).ToArray();
if (!solutions.Any())
{
shell.WriteLine("Entity does not have any solutions!");
return;
}
var validSolutions = string.Join(", ", solutions.Select(s => s.Name));
shell.WriteLine($"Entity does not have a \"{args[1]}\" solution. Valid solutions are:\n{validSolutions}");
return;
}
if (!_protomanager.HasIndex<ReagentPrototype>(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);
}
}
}