using Content.Server.Mech.Systems; using Content.Server.Power.Components; using Content.Shared.Construction; using Content.Shared.Mech.Components; using JetBrains.Annotations; using Robust.Server.Containers; using Robust.Shared.Containers; using Robust.Shared.Prototypes; using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype; namespace Content.Server.Construction.Completions; /// /// Creates the mech entity while transferring all relevant parts inside of it, /// for right now, the cell that was used in construction. /// [UsedImplicitly, DataDefinition] public sealed partial class BuildMech : IGraphAction { [DataField("mechPrototype", required: true, customTypeSerializer: typeof(PrototypeIdSerializer))] public string MechPrototype = string.Empty; [DataField("container")] public string Container = "battery-container"; // TODO use or generalize ConstructionSystem.ChangeEntity(); public void PerformAction(EntityUid uid, EntityUid? userUid, IEntityManager entityManager) { if (!entityManager.TryGetComponent(uid, out ContainerManagerComponent? containerManager)) { Logger.Warning($"Mech construct entity {uid} did not have a container manager! Aborting build mech action."); return; } var containerSystem = entityManager.EntitySysManager.GetEntitySystem(); var mechSys = entityManager.System(); if (!containerSystem.TryGetContainer(uid, Container, out var container, containerManager)) { Logger.Warning($"Mech construct entity {uid} did not have the specified '{Container}' container! Aborting build mech action."); return; } if (container.ContainedEntities.Count != 1) { Logger.Warning($"Mech construct entity {uid} did not have exactly one item in the specified '{Container}' container! Aborting build mech action."); } var cell = container.ContainedEntities[0]; if (!entityManager.TryGetComponent(cell, out var batteryComponent)) { Logger.Warning($"Mech construct entity {uid} had an invalid entity in container \"{Container}\"! Aborting build mech action."); return; } containerSystem.Remove(cell, container); var transform = entityManager.GetComponent(uid); var mech = entityManager.SpawnEntity(MechPrototype, transform.Coordinates); if (entityManager.TryGetComponent(mech, out var mechComp) && mechComp.BatterySlot.ContainedEntity == null) { mechSys.InsertBattery(mech, cell, mechComp, batteryComponent); containerSystem.Insert(cell, mechComp.BatterySlot); } var entChangeEv = new ConstructionChangeEntityEvent(mech, uid); entityManager.EventBus.RaiseLocalEvent(uid, entChangeEv); entityManager.EventBus.RaiseLocalEvent(mech, entChangeEv, broadcast: true); entityManager.QueueDeleteEntity(uid); } }