Files
crystall-punk-14/Content.Shared/_CP14/Workbench/Requirements/StackGroupResource.cs
Ed 0decd375e4 Birch tree (#1154)
* birch

* wooden birch log and planks

* birch tile crafting

* add birch to worldgen

* Update grasslands.yml

* wooden planks (any)

* universal wood crafting

* birch wall
2025-04-09 15:54:38 +03:00

98 lines
2.8 KiB
C#

/*
* This file is sublicensed under MIT License
* https://github.com/space-wizards/space-station-14/blob/master/LICENSE.TXT
*/
using Content.Shared._CP14.Workbench.Prototypes;
using Content.Shared.Stacks;
using Robust.Shared.Prototypes;
using Robust.Shared.Utility;
namespace Content.Shared._CP14.Workbench.Requirements;
public sealed partial class StackGroupResource : CP14WorkbenchCraftRequirement
{
public override bool HideRecipe { get; set; } = false;
[DataField(required: true)]
public ProtoId<CP14StackGroupPrototype> Group;
[DataField]
public int Count = 1;
public override bool CheckRequirement(EntityManager entManager,
IPrototypeManager protoManager,
HashSet<EntityUid> placedEntities,
EntityUid user,
CP14WorkbenchRecipePrototype recipe)
{
if (!protoManager.TryIndex(Group, out var indexedGroup))
return false;
var count = 0;
foreach (var ent in placedEntities)
{
if (!entManager.TryGetComponent<StackComponent>(ent, out var stack))
continue;
if (!indexedGroup.Stacks.Contains(stack.StackTypeId))
continue;
count += stack.Count;
}
if (count < Count)
return false;
return true;
}
public override void PostCraft(EntityManager entManager, IPrototypeManager protoManager,
HashSet<EntityUid> placedEntities,
EntityUid user)
{
var stackSystem = entManager.System<SharedStackSystem>();
if (!protoManager.TryIndex(Group, out var indexedGroup))
return;
var requiredCount = Count;
foreach (var placedEntity in placedEntities)
{
if (!entManager.TryGetComponent<StackComponent>(placedEntity, out var stack))
continue;
if (!indexedGroup.Stacks.Contains(stack.StackTypeId))
continue;
var count = (int)MathF.Min(requiredCount, stack.Count);
if (stack.Count - count <= 0)
entManager.DeleteEntity(placedEntity);
else
stackSystem.SetCount(placedEntity, stack.Count - count, stack);
requiredCount -= count;
}
}
public override string GetRequirementTitle(IPrototypeManager protoManager)
{
var indexedGroup = protoManager.Index(Group);
return $"{Loc.GetString(indexedGroup.Name)} x{Count}";
}
public override EntityPrototype? GetRequirementEntityView(IPrototypeManager protoManager)
{
return null;
}
public override SpriteSpecifier? GetRequirementTexture(IPrototypeManager protoManager)
{
var indexedGroup = protoManager.Index(Group);
return !protoManager.TryIndex(indexedGroup.Stacks.FirstOrNull(), out var indexedStack) ? null : indexedStack.Icon;
}
}