using System.Linq; using Content.Server.Administration; using Content.Shared.Administration; using Content.Shared.Body; using Robust.Shared.Containers; using Robust.Shared.Prototypes; using Robust.Shared.Toolshed; namespace Content.Server.Body; [ToolshedCommand, AdminCommand(AdminFlags.Debug)] public sealed class OrganCommand : ToolshedCommand { private DetachableOrganSystem? _detachableOrgan; private SharedContainerSystem? _container; private OrganRelationSystem? _organRelation; [CommandImplementation("parent")] public EntityUid? Parent([PipedArgument] EntityUid child) { if (!TryComp(child, out var childOrgan)) return null; return childOrgan.Parent; } [CommandImplementation("parent")] public IEnumerable Parent([PipedArgument] IEnumerable children) { return children.Select(Parent); } [CommandImplementation("children")] public IEnumerable Children([PipedArgument] EntityUid parent) { if (!TryComp(parent, out var parentOrgan)) yield break; foreach (var child in parentOrgan.Children) { yield return child; } } [CommandImplementation("children")] public IEnumerable Children([PipedArgument] IEnumerable parents) { return parents.SelectMany(Children); } [CommandImplementation("detach")] public EntityUid? Detach([PipedArgument] EntityUid organ) { _detachableOrgan ??= GetSys(); return _detachableOrgan.Detach(organ); } [CommandImplementation("detach")] public IEnumerable Detach([PipedArgument] IEnumerable organs) { return organs.Select(Detach); } [CommandImplementation("attach")] public void Attach([PipedArgument] EntityUid parent, EntityUid child) { _container ??= GetSys(); _organRelation ??= GetSys(); if (!TryComp(parent, out var parentOrgan) || !TryComp(child, out var childOrgan) || !TryComp(parent, out var parentOrganComponent) || parentOrganComponent.Body is not { } body) return; if (!_container.TryGetContainer(body, BodyComponent.ContainerID, out var container)) return; _container.Insert(child, container, force: true); _organRelation.Relate((parent, parentOrgan), (child, childOrgan)); } [CommandImplementation("attach")] public void Attach([PipedArgument] IEnumerable parents, EntityUid child) { foreach (var parent in parents) { Attach(parent, child); } } [CommandImplementation("is")] public bool Is([PipedArgument] EntityUid organ, ProtoId category) { if (!TryComp(organ, out var organComp)) return false; return organComp.Category == category; } [CommandImplementation("is")] public IEnumerable Is([PipedArgument] IEnumerable organs, ProtoId category) { return organs.Select(it => Is(it, category)); } [CommandImplementation("of_type")] public IEnumerable Typed([PipedArgument] IEnumerable organs, ProtoId category) { return organs.Where(it => Is(it, category)); } }