Give prototype refactor (#29697)
* Update GivePrototype * File scoped namespace * Change to EntProtoId instead of ProtoId<> for better validation
This commit is contained in:
@@ -1,44 +1,50 @@
|
||||
using Content.Server.Stack;
|
||||
using Content.Shared.Construction;
|
||||
using Content.Shared.Hands.Components;
|
||||
using Content.Shared.Hands.EntitySystems;
|
||||
using Content.Shared.Prototypes;
|
||||
using Content.Shared.Stacks;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Shared.Prototypes;
|
||||
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
|
||||
|
||||
namespace Content.Server.Construction.Completions
|
||||
namespace Content.Server.Construction.Completions;
|
||||
|
||||
[UsedImplicitly]
|
||||
[DataDefinition]
|
||||
public sealed partial class GivePrototype : IGraphAction
|
||||
{
|
||||
[UsedImplicitly]
|
||||
[DataDefinition]
|
||||
public sealed partial class GivePrototype : IGraphAction
|
||||
{
|
||||
[DataField("prototype", customTypeSerializer:typeof(PrototypeIdSerializer<EntityPrototype>))]
|
||||
public string Prototype { get; private set; } = string.Empty;
|
||||
[DataField("amount")]
|
||||
public int Amount { get; private set; } = 1;
|
||||
[DataField]
|
||||
public EntProtoId Prototype { get; private set; } = string.Empty;
|
||||
|
||||
public void PerformAction(EntityUid uid, EntityUid? userUid, IEntityManager entityManager)
|
||||
[DataField]
|
||||
public int Amount { get; private set; } = 1;
|
||||
|
||||
public void PerformAction(EntityUid uid, EntityUid? userUid, IEntityManager entityManager)
|
||||
{
|
||||
if (string.IsNullOrEmpty(Prototype))
|
||||
return;
|
||||
|
||||
if (EntityPrototypeHelpers.HasComponent<StackComponent>(Prototype))
|
||||
{
|
||||
if (string.IsNullOrEmpty(Prototype))
|
||||
var stackSystem = entityManager.EntitySysManager.GetEntitySystem<StackSystem>();
|
||||
var stacks = stackSystem.SpawnMultiple(Prototype, Amount, userUid ?? uid);
|
||||
|
||||
if (userUid is null || !entityManager.TryGetComponent(userUid, out HandsComponent? handsComp))
|
||||
return;
|
||||
|
||||
var coordinates = entityManager.GetComponent<TransformComponent>(userUid ?? uid).Coordinates;
|
||||
|
||||
if (EntityPrototypeHelpers.HasComponent<StackComponent>(Prototype))
|
||||
foreach (var item in stacks)
|
||||
{
|
||||
var stackEnt = entityManager.SpawnEntity(Prototype, coordinates);
|
||||
var stack = entityManager.GetComponent<StackComponent>(stackEnt);
|
||||
entityManager.EntitySysManager.GetEntitySystem<StackSystem>().SetCount(stackEnt, Amount, stack);
|
||||
entityManager.EntitySysManager.GetEntitySystem<SharedHandsSystem>().PickupOrDrop(userUid, stackEnt);
|
||||
stackSystem.TryMergeToHands(item, userUid.Value, hands: handsComp);
|
||||
}
|
||||
else
|
||||
}
|
||||
else
|
||||
{
|
||||
var handsSystem = entityManager.EntitySysManager.GetEntitySystem<SharedHandsSystem>();
|
||||
var handsComp = userUid is not null ? entityManager.GetComponent<HandsComponent>(userUid.Value) : null;
|
||||
for (var i = 0; i < Amount; i++)
|
||||
{
|
||||
for (var i = 0; i < Amount; i++)
|
||||
{
|
||||
var item = entityManager.SpawnEntity(Prototype, coordinates);
|
||||
entityManager.EntitySysManager.GetEntitySystem<SharedHandsSystem>().PickupOrDrop(userUid, item);
|
||||
}
|
||||
var item = entityManager.SpawnNextToOrDrop(Prototype, userUid ?? uid);
|
||||
handsSystem.PickupOrDrop(userUid, item, handsComp: handsComp);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user