2020-10-30 16:06:48 +01:00
using Content.Server.Administration ;
2022-10-23 00:46:28 +02:00
using Content.Server.Body.Systems ;
2020-10-30 16:06:48 +01:00
using Content.Shared.Administration ;
2021-06-09 22:19:39 +02:00
using Content.Shared.Body.Components ;
2022-10-23 00:46:28 +02:00
using Content.Shared.Body.Part ;
2021-02-01 16:49:43 -08:00
using Robust.Shared.Console ;
2020-10-25 23:16:57 +01:00
2021-06-09 22:19:39 +02:00
namespace Content.Server.Body.Commands
2020-10-25 23:16:57 +01:00
{
2020-10-30 16:06:48 +01:00
[AdminCommand(AdminFlags.Fun)]
2022-02-16 00:23:23 -07:00
public sealed class AttachBodyPartCommand : IConsoleCommand
2020-10-25 23:16:57 +01:00
{
2023-09-11 09:42:41 +10:00
[Dependency] private readonly IEntityManager _entManager = default ! ;
2020-10-25 23:16:57 +01:00
public string Command = > "attachbodypart" ;
public string Description = > "Attaches a body part to you or someone else." ;
public string Help = > $"{Command} <partEntityUid> / {Command} <entityUid> <partEntityUid>" ;
2021-02-01 16:49:43 -08:00
public void Execute ( IConsoleShell shell , string argStr , string [ ] args )
2020-10-25 23:16:57 +01:00
{
2023-10-28 09:59:53 +11:00
var player = shell . Player ;
2020-10-25 23:16:57 +01:00
2022-10-23 00:46:28 +02:00
EntityUid bodyId ;
2023-09-11 09:42:41 +10:00
EntityUid ? partUid ;
2020-10-25 23:16:57 +01:00
switch ( args . Length )
{
case 1 :
if ( player = = null )
{
2021-02-01 16:49:43 -08:00
shell . WriteLine ( $"You need to specify an entity to attach the part to if you aren't a player.\n{Help}" ) ;
2020-10-25 23:16:57 +01:00
return ;
}
if ( player . AttachedEntity = = null )
{
2021-02-01 16:49:43 -08:00
shell . WriteLine ( $"You need to specify an entity to attach the part to if you aren't attached to an entity.\n{Help}" ) ;
2020-10-25 23:16:57 +01:00
return ;
}
2023-09-11 09:42:41 +10:00
if ( ! NetEntity . TryParse ( args [ 0 ] , out var partNet ) | | ! _entManager . TryGetEntity ( partNet , out partUid ) )
2020-10-25 23:16:57 +01:00
{
2021-02-01 16:49:43 -08:00
shell . WriteLine ( $"{args[0]} is not a valid entity uid." ) ;
2020-10-25 23:16:57 +01:00
return ;
}
2022-10-23 00:46:28 +02:00
bodyId = player . AttachedEntity . Value ;
2020-10-25 23:16:57 +01:00
break ;
case 2 :
2023-09-11 09:42:41 +10:00
if ( ! NetEntity . TryParse ( args [ 0 ] , out var entityNet ) | | ! _entManager . TryGetEntity ( entityNet , out var entityUid ) )
2020-10-25 23:16:57 +01:00
{
2021-02-01 16:49:43 -08:00
shell . WriteLine ( $"{args[0]} is not a valid entity uid." ) ;
2020-10-25 23:16:57 +01:00
return ;
}
2023-09-11 09:42:41 +10:00
if ( ! NetEntity . TryParse ( args [ 1 ] , out partNet ) | | ! _entManager . TryGetEntity ( partNet , out partUid ) )
2020-10-25 23:16:57 +01:00
{
2021-02-01 16:49:43 -08:00
shell . WriteLine ( $"{args[1]} is not a valid entity uid." ) ;
2020-10-25 23:16:57 +01:00
return ;
}
2023-09-11 09:42:41 +10:00
if ( ! _entManager . EntityExists ( entityUid ) )
2020-10-25 23:16:57 +01:00
{
2021-02-01 16:49:43 -08:00
shell . WriteLine ( $"{entityUid} is not a valid entity." ) ;
2020-10-25 23:16:57 +01:00
return ;
}
2023-09-11 09:42:41 +10:00
bodyId = entityUid . Value ;
2020-10-25 23:16:57 +01:00
break ;
default :
2021-02-01 16:49:43 -08:00
shell . WriteLine ( Help ) ;
2020-10-25 23:16:57 +01:00
return ;
}
2023-09-11 09:42:41 +10:00
if ( ! _entManager . TryGetComponent ( bodyId , out BodyComponent ? body ) )
2020-10-25 23:16:57 +01:00
{
2023-09-11 09:42:41 +10:00
shell . WriteLine ( $"Entity {_entManager.GetComponent<MetaDataComponent>(bodyId).EntityName} with uid {bodyId} does not have a {nameof(BodyComponent)}." ) ;
2020-10-25 23:16:57 +01:00
return ;
}
2023-09-11 09:42:41 +10:00
if ( ! _entManager . EntityExists ( partUid ) )
2020-10-25 23:16:57 +01:00
{
2021-02-01 16:49:43 -08:00
shell . WriteLine ( $"{partUid} is not a valid entity." ) ;
2020-10-25 23:16:57 +01:00
return ;
}
2023-09-11 09:42:41 +10:00
if ( ! _entManager . TryGetComponent ( partUid , out BodyPartComponent ? part ) )
2020-10-25 23:16:57 +01:00
{
2023-09-11 09:42:41 +10:00
shell . WriteLine ( $"Entity {_entManager.GetComponent<MetaDataComponent>(partUid.Value).EntityName} with uid {args[0]} does not have a {nameof(BodyPartComponent)}." ) ;
2020-10-25 23:16:57 +01:00
return ;
}
2023-09-11 09:42:41 +10:00
var bodySystem = _entManager . System < BodySystem > ( ) ;
2023-09-21 00:23:02 -07:00
if ( bodySystem . BodyHasChild ( bodyId , partUid . Value , body , part ) )
2020-10-25 23:16:57 +01:00
{
2023-09-11 09:42:41 +10:00
shell . WriteLine ( $"Body part {_entManager.GetComponent<MetaDataComponent>(partUid.Value).EntityName} with uid {partUid} is already attached to entity {_entManager.GetComponent<MetaDataComponent>(bodyId).EntityName} with uid {bodyId}" ) ;
2020-10-25 23:16:57 +01:00
return ;
}
2022-10-23 00:46:28 +02:00
var slotId = $"AttachBodyPartVerb-{partUid}" ;
// ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
2023-09-21 00:23:02 -07:00
if ( body . RootContainer . ContainedEntity ! = null )
2022-10-23 00:46:28 +02:00
{
2023-09-21 00:23:02 -07:00
bodySystem . AttachPartToRoot ( bodyId , partUid . Value , body , part ) ;
2022-10-23 00:46:28 +02:00
}
else
{
2023-09-21 00:23:02 -07:00
var ( rootPartId , rootPart ) = bodySystem . GetRootPartOrNull ( bodyId , body ) ! . Value ;
if ( ! bodySystem . TryCreatePartSlotAndAttach ( rootPartId , slotId , partUid . Value , part . PartType , rootPart , part ) )
2022-10-23 00:46:28 +02:00
{
2023-09-11 09:42:41 +10:00
shell . WriteError ( $"Could not create slot {slotId} on entity {_entManager.ToPrettyString(bodyId)}" ) ;
2022-10-23 00:46:28 +02:00
return ;
}
}
2023-09-11 09:42:41 +10:00
shell . WriteLine ( $"Attached part {_entManager.ToPrettyString(partUid.Value)} to {_entManager.ToPrettyString(bodyId)}" ) ;
2020-10-25 23:16:57 +01:00
}
}
}