2020-12-03 22:49:00 +01:00
using System.Linq ;
using System.Threading.Tasks ;
2021-06-09 22:19:39 +02:00
using Content.Server.Construction.Components ;
2020-12-03 22:49:00 +01:00
using Content.Shared.Construction ;
using JetBrains.Annotations ;
using Robust.Shared.Containers ;
2021-02-11 01:13:03 -08:00
using Robust.Shared.GameObjects ;
2020-12-03 22:49:00 +01:00
using Robust.Shared.Log ;
2021-03-05 01:08:38 +01:00
using Robust.Shared.Serialization.Manager.Attributes ;
2020-12-03 22:49:00 +01:00
namespace Content.Server.Construction.Completions
{
[UsedImplicitly]
2021-03-05 01:08:38 +01:00
[DataDefinition]
2020-12-03 22:49:00 +01:00
public class BuildMachine : IGraphAction
{
public async Task PerformAction ( IEntity entity , IEntity ? user )
{
if ( ! entity . TryGetComponent ( out ContainerManagerComponent ? containerManager ) )
{
Logger . Warning ( $"Machine frame entity {entity} did not have a container manager! Aborting build machine action." ) ;
return ;
}
if ( ! entity . TryGetComponent ( out MachineFrameComponent ? machineFrame ) )
{
Logger . Warning ( $"Machine frame entity {entity} did not have a machine frame component! Aborting build machine action." ) ;
return ;
}
if ( ! machineFrame . IsComplete )
{
Logger . Warning ( $"Machine frame entity {entity} doesn't have all required parts to be built! Aborting build machine action." ) ;
return ;
}
if ( ! containerManager . TryGetContainer ( MachineFrameComponent . BoardContainer , out var entBoardContainer ) )
{
Logger . Warning ( $"Machine frame entity {entity} did not have the '{MachineFrameComponent.BoardContainer}' container! Aborting build machine action." ) ;
return ;
}
if ( ! containerManager . TryGetContainer ( MachineFrameComponent . PartContainer , out var entPartContainer ) )
{
Logger . Warning ( $"Machine frame entity {entity} did not have the '{MachineFrameComponent.PartContainer}' container! Aborting build machine action." ) ;
return ;
}
if ( entBoardContainer . ContainedEntities . Count ! = 1 )
{
Logger . Warning ( $"Machine frame entity {entity} did not have exactly one item in the '{MachineFrameComponent.BoardContainer}' container! Aborting build machine action." ) ;
}
var board = entBoardContainer . ContainedEntities [ 0 ] ;
if ( ! board . TryGetComponent ( out MachineBoardComponent ? boardComponent ) )
{
Logger . Warning ( $"Machine frame entity {entity} had an invalid entity in container \" { MachineFrameComponent . BoardContainer } \ "! Aborting build machine action." ) ;
return ;
}
var entityManager = entity . EntityManager ;
entBoardContainer . Remove ( board ) ;
var machine = entityManager . SpawnEntity ( boardComponent . Prototype , entity . Transform . Coordinates ) ;
machine . Transform . LocalRotation = entity . Transform . LocalRotation ;
2021-03-01 15:24:46 -08:00
var boardContainer = ContainerHelpers . EnsureContainer < Container > ( machine , MachineFrameComponent . BoardContainer , out var existed ) ;
2020-12-03 22:49:00 +01:00
if ( existed )
{
// Clean that up...
boardContainer . CleanContainer ( ) ;
}
2021-03-01 15:24:46 -08:00
var partContainer = ContainerHelpers . EnsureContainer < Container > ( machine , MachineFrameComponent . PartContainer , out existed ) ;
2020-12-03 22:49:00 +01:00
if ( existed )
{
// Clean that up, too...
partContainer . CleanContainer ( ) ;
}
boardContainer . Insert ( board ) ;
// Now we insert all parts.
foreach ( var part in entPartContainer . ContainedEntities . ToArray ( ) )
{
entPartContainer . ForceRemove ( part ) ;
partContainer . Insert ( part ) ;
}
if ( machine . TryGetComponent ( out ConstructionComponent ? construction ) )
{
// We only add these two container. If some construction needs to take other containers into account, fix this.
construction . AddContainer ( MachineFrameComponent . BoardContainer ) ;
construction . AddContainer ( MachineFrameComponent . PartContainer ) ;
}
if ( machine . TryGetComponent ( out MachineComponent ? machineComp ) )
{
machineComp . RefreshParts ( ) ;
}
entity . Delete ( ) ;
}
}
}