2021-02-17 14:02:36 +01:00
|
|
|
|
2021-07-22 11:56:55 +02:00
|
|
|
using System;
|
2021-02-17 14:02:36 +01:00
|
|
|
using System.Collections.Generic;
|
2021-06-09 22:19:39 +02:00
|
|
|
using Content.Shared.Stacks;
|
|
|
|
|
using Content.Shared.Tag;
|
2021-02-17 14:02:36 +01:00
|
|
|
using Robust.Server.GameObjects;
|
2021-03-01 15:24:46 -08:00
|
|
|
using Robust.Shared.Containers;
|
2021-02-17 14:02:36 +01:00
|
|
|
using Robust.Shared.GameObjects;
|
|
|
|
|
using Robust.Shared.Log;
|
|
|
|
|
using Robust.Shared.Serialization;
|
2021-03-05 01:08:38 +01:00
|
|
|
using Robust.Shared.Serialization.Manager.Attributes;
|
2021-02-17 14:02:36 +01:00
|
|
|
|
2021-06-09 22:19:39 +02:00
|
|
|
namespace Content.Server.Storage.Components
|
2021-02-17 14:02:36 +01:00
|
|
|
{
|
|
|
|
|
/// <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>
|
2021-07-22 11:56:55 +02:00
|
|
|
[Obsolete("Should be deprecated in favor of SharedItemCounterSystem")]
|
2021-02-17 14:02:36 +01:00
|
|
|
[RegisterComponent]
|
2021-03-05 01:08:38 +01:00
|
|
|
public class StorageCounterComponent : Component, ISerializationHooks
|
2021-02-17 14:02:36 +01:00
|
|
|
{
|
2021-06-08 19:10:29 -07:00
|
|
|
// TODO Convert to EntityWhitelist
|
2021-03-05 01:08:38 +01:00
|
|
|
[DataField("countTag")]
|
2021-02-17 14:02:36 +01:00
|
|
|
private string? _countTag;
|
2021-03-05 01:08:38 +01:00
|
|
|
|
|
|
|
|
[DataField("amount")]
|
2021-02-17 14:02:36 +01:00
|
|
|
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";
|
|
|
|
|
|
2021-03-05 01:08:38 +01:00
|
|
|
void ISerializationHooks.AfterDeserialization()
|
2021-02-17 14:02:36 +01:00
|
|
|
{
|
|
|
|
|
if (_countTag == null)
|
|
|
|
|
{
|
|
|
|
|
Logger.Warning("StorageCounterComponent without a `countTag` is useless");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-18 01:49:18 -07:00
|
|
|
public void ContainerUpdateAppearance(IContainer container)
|
2021-02-17 14:02:36 +01:00
|
|
|
{
|
2021-06-18 01:49:18 -07:00
|
|
|
if(_appearanceComponent is null)
|
|
|
|
|
return;
|
2021-02-17 14:02:36 +01:00
|
|
|
|
2021-06-18 01:49:18 -07:00
|
|
|
var actual = Count(container.ContainedEntities);
|
|
|
|
|
_appearanceComponent.SetData(StackVisuals.Actual, actual);
|
2021-02-17 14:02:36 +01:00
|
|
|
|
2021-06-18 01:49:18 -07:00
|
|
|
if (_maxAmount != null)
|
|
|
|
|
{
|
|
|
|
|
_appearanceComponent.SetData(StackVisuals.MaxCount, _maxAmount);
|
2021-02-17 14:02:36 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int Count(IReadOnlyList<IEntity> containerContainedEntities)
|
|
|
|
|
{
|
|
|
|
|
var count = 0;
|
|
|
|
|
if (_countTag != null)
|
|
|
|
|
{
|
|
|
|
|
foreach (var entity in containerContainedEntities)
|
|
|
|
|
{
|
|
|
|
|
if (entity.HasTag(_countTag))
|
|
|
|
|
{
|
|
|
|
|
count++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return count;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|