Files
tbd-station-14/Content.Server/Storage/Components/StorageCounterComponent.cs
Ygg01 3fd28c2565 Added mapped storage for things like crayon belts and tools (#4201)
* Added mapped storage for things like crayon belts and tools

* Attempt to get StorageFillEvent to work

* Managed to get it working with Visualizer logi

* Improved PR and did some light refactoring of components

* Update Content.Client/Storage/Visualizers/MappedItemVisualizer.cs

Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>

* Removed event, went with stateful ApperanceData

* Removed ids in favor of whitelist

* Refactor YAML, Moved functionality to Shared and renamed it.

* Changed so insert/remove always send full state.

* Move logic to component

* Fix some issues on MappedVisualizer and few nitpicks

- Fix mapped visualizer only doing init or update layers
- Fixed naming of systems
- Fixed sort of crayons

* Forgot to apply Vera's suggestion

* Fix the data to be more strict and to avoid unnecessary clearing

Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
2021-07-22 02:56:55 -07:00

83 lines
2.5 KiB
C#

using System;
using System.Collections.Generic;
using Content.Shared.Stacks;
using Content.Shared.Tag;
using Robust.Server.GameObjects;
using Robust.Shared.Containers;
using Robust.Shared.GameObjects;
using Robust.Shared.Log;
using Robust.Shared.Serialization;
using Robust.Shared.Serialization.Manager.Attributes;
namespace Content.Server.Storage.Components
{
/// <summary>
/// Storage that spawns and counts a single item.
/// Usually used for things like matchboxes, cigarette packs,
/// cigar cases etc.
/// </summary>
/// <code>
/// - type: StorageCounter
/// amount: 6 # Note: this field can be omitted
/// countTag: Cigarette # Note: field doesn't point to entity Id, but its tag
/// </code>
[Obsolete("Should be deprecated in favor of SharedItemCounterSystem")]
[RegisterComponent]
public class StorageCounterComponent : Component, ISerializationHooks
{
// TODO Convert to EntityWhitelist
[DataField("countTag")]
private string? _countTag;
[DataField("amount")]
private int? _maxAmount;
/// <summary>
/// Single item storage component usually have an attached StackedVisualizer.
/// </summary>
[ComponentDependency] private readonly AppearanceComponent? _appearanceComponent = default;
public override string Name => "StorageCounter";
void ISerializationHooks.AfterDeserialization()
{
if (_countTag == null)
{
Logger.Warning("StorageCounterComponent without a `countTag` is useless");
}
}
public void ContainerUpdateAppearance(IContainer container)
{
if(_appearanceComponent is null)
return;
var actual = Count(container.ContainedEntities);
_appearanceComponent.SetData(StackVisuals.Actual, actual);
if (_maxAmount != null)
{
_appearanceComponent.SetData(StackVisuals.MaxCount, _maxAmount);
}
}
private int Count(IReadOnlyList<IEntity> containerContainedEntities)
{
var count = 0;
if (_countTag != null)
{
foreach (var entity in containerContainedEntities)
{
if (entity.HasTag(_countTag))
{
count++;
}
}
}
return count;
}
}
}