- Powerful - Data-oriented - Approved by PJB - Powered by node graphs and AI pathfinding - Coded by the same nerd who brought you atmos Co-authored-by: Exp <theexp111@gmail.com>
69 lines
2.6 KiB
C#
69 lines
2.6 KiB
C#
#nullable enable
|
|
using System.Threading.Tasks;
|
|
using Content.Server.GameObjects.Components.Construction;
|
|
using Content.Shared.Construction;
|
|
using JetBrains.Annotations;
|
|
using Robust.Server.GameObjects.Components.Container;
|
|
using Robust.Shared.Interfaces.GameObjects;
|
|
using Robust.Shared.Log;
|
|
using Robust.Shared.Serialization;
|
|
|
|
namespace Content.Server.Construction.Completions
|
|
{
|
|
[UsedImplicitly]
|
|
public class BuildComputer : IGraphAction
|
|
{
|
|
public string Container { get; private set; } = string.Empty;
|
|
|
|
public void ExposeData(ObjectSerializer serializer)
|
|
{
|
|
serializer.DataField(this, x => x.Container, "container", string.Empty);
|
|
}
|
|
|
|
public async Task PerformAction(IEntity entity, IEntity? user)
|
|
{
|
|
if (!entity.TryGetComponent(out ContainerManagerComponent? containerManager))
|
|
{
|
|
Logger.Warning($"Computer entity {entity} did not have a container manager! Aborting build computer action.");
|
|
return;
|
|
}
|
|
|
|
if (!containerManager.TryGetContainer(Container, out var container))
|
|
{
|
|
Logger.Warning($"Computer entity {entity} did not have the specified '{Container}' container! Aborting build computer action.");
|
|
return;
|
|
}
|
|
|
|
if (container.ContainedEntities.Count != 1)
|
|
{
|
|
Logger.Warning($"Computer entity {entity} did not have exactly one item in the specified '{Container}' container! Aborting build computer action.");
|
|
}
|
|
|
|
var board = container.ContainedEntities[0];
|
|
|
|
if (!board.TryGetComponent(out ComputerBoardComponent? boardComponent))
|
|
{
|
|
Logger.Warning($"Computer entity {entity} had an invalid entity in container \"{Container}\"! Aborting build computer action.");
|
|
return;
|
|
}
|
|
|
|
var entityManager = entity.EntityManager;
|
|
container.Remove(board);
|
|
|
|
var computer = entityManager.SpawnEntity(boardComponent.Prototype, entity.Transform.Coordinates);
|
|
computer.Transform.LocalRotation = entity.Transform.LocalRotation;
|
|
|
|
var computerContainer = ContainerManagerComponent.Ensure<Container>(Container, computer);
|
|
computerContainer.Insert(board);
|
|
|
|
if (computer.TryGetComponent(out ConstructionComponent? construction))
|
|
{
|
|
// We only add this container. If some construction needs to take other containers into account, fix this.
|
|
construction.AddContainer(Container);
|
|
}
|
|
|
|
entity.Delete();
|
|
}
|
|
}
|
|
}
|