Files
tbd-station-14/Content.Server/Construction/StackHelpers.cs
DrSmugleaf fdcbece63d Refactor stacks to use prototypes (#3387)
* Refactor stacks to use prototypes

* Fix not assigned warning

* Add names to stacks

* Make machine baords and material constructions use the name as well

* Remove defaulting stacks to prototype id

* Fix tests

Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
2021-02-25 16:18:29 +11:00

34 lines
1.1 KiB
C#

#nullable enable
using System;
using Content.Server.GameObjects.Components.Stack;
using Content.Shared.GameObjects.Components;
using Content.Shared.Stacks;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Map;
namespace Content.Server.Construction
{
public static class StackHelpers
{
/// <summary>
/// Spawns a stack of a specified type given an amount.
/// </summary>
public static IEntity SpawnStack(StackPrototype stack, int amount, EntityCoordinates coordinates, IEntityManager? entityManager = null)
{
entityManager ??= IoCManager.Resolve<IEntityManager>();
// TODO: Add more.
string prototype = stack.Spawn ?? throw new ArgumentOutOfRangeException(nameof(stack),
"Stack type doesn't have a prototype specified yet!");
var ent = entityManager.SpawnEntity(prototype, coordinates);
var stackComponent = ent.GetComponent<StackComponent>();
stackComponent.Count = Math.Min(amount, stackComponent.MaxCount);
return ent;
}
}
}