2021-02-22 04:08:17 +00:00
|
|
|
using System;
|
2021-02-25 06:18:29 +01:00
|
|
|
using Content.Shared.Stacks;
|
2020-01-09 00:27:52 +01:00
|
|
|
using Robust.Shared.GameObjects;
|
2020-04-12 01:15:16 +02:00
|
|
|
using Robust.Shared.IoC;
|
2021-02-25 06:18:29 +01:00
|
|
|
using Robust.Shared.Log;
|
2021-02-18 09:09:07 +01:00
|
|
|
using Robust.Shared.Players;
|
2021-02-25 06:18:29 +01:00
|
|
|
using Robust.Shared.Prototypes;
|
2020-01-09 00:27:52 +01:00
|
|
|
using Robust.Shared.Serialization;
|
2021-03-05 01:08:38 +01:00
|
|
|
using Robust.Shared.Serialization.Manager.Attributes;
|
2020-04-12 01:15:16 +02:00
|
|
|
using Robust.Shared.ViewVariables;
|
2020-01-09 00:27:52 +01:00
|
|
|
|
|
|
|
|
namespace Content.Shared.GameObjects.Components
|
|
|
|
|
{
|
2021-03-05 01:08:38 +01:00
|
|
|
public abstract class SharedStackComponent : Component, ISerializationHooks
|
2020-01-09 00:27:52 +01:00
|
|
|
{
|
2021-02-25 06:18:29 +01:00
|
|
|
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
|
|
|
|
|
|
2020-04-12 01:15:16 +02:00
|
|
|
private const string SerializationCache = "stack";
|
|
|
|
|
|
2020-01-09 00:27:52 +01:00
|
|
|
public sealed override string Name => "Stack";
|
|
|
|
|
public sealed override uint? NetID => ContentNetIDs.STACK;
|
|
|
|
|
|
2021-03-05 01:08:38 +01:00
|
|
|
[DataField("count")]
|
|
|
|
|
private int _count = 30;
|
|
|
|
|
|
|
|
|
|
[DataField("max")]
|
|
|
|
|
private int _maxCount = 30;
|
2020-04-12 01:15:16 +02:00
|
|
|
|
|
|
|
|
[ViewVariables(VVAccess.ReadWrite)]
|
|
|
|
|
public virtual int Count
|
|
|
|
|
{
|
|
|
|
|
get => _count;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_count = value;
|
|
|
|
|
if (_count <= 0)
|
|
|
|
|
{
|
|
|
|
|
Owner.Delete();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Dirty();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[ViewVariables]
|
|
|
|
|
public int MaxCount
|
|
|
|
|
{
|
|
|
|
|
get => _maxCount;
|
|
|
|
|
private set
|
|
|
|
|
{
|
|
|
|
|
_maxCount = value;
|
|
|
|
|
Dirty();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[ViewVariables] public int AvailableSpace => MaxCount - Count;
|
|
|
|
|
|
2021-03-05 01:08:38 +01:00
|
|
|
[ViewVariables]
|
|
|
|
|
[field: DataField("stackType")]
|
|
|
|
|
public string StackTypeId { get; } = string.Empty;
|
2021-02-25 06:18:29 +01:00
|
|
|
|
|
|
|
|
public StackPrototype StackType => _prototypeManager.Index<StackPrototype>(StackTypeId);
|
2020-04-12 01:15:16 +02:00
|
|
|
|
2021-02-25 06:18:29 +01:00
|
|
|
protected override void Startup()
|
|
|
|
|
{
|
|
|
|
|
base.Startup();
|
|
|
|
|
|
|
|
|
|
if (!_prototypeManager.HasIndex<StackPrototype>(StackTypeId))
|
2020-04-12 01:15:16 +02:00
|
|
|
{
|
2021-02-26 18:20:19 +11:00
|
|
|
Logger.Error($"No {nameof(StackPrototype)} found with id {StackTypeId} for {Owner.Prototype?.ID ?? Owner.Name}");
|
2020-04-12 01:15:16 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-18 09:09:07 +01:00
|
|
|
public override ComponentState GetComponentState(ICommonSession player)
|
2020-04-12 01:15:16 +02:00
|
|
|
{
|
|
|
|
|
return new StackComponentState(Count, MaxCount);
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-15 21:55:49 +01:00
|
|
|
public override void HandleComponentState(ComponentState? curState, ComponentState? nextState)
|
2020-04-12 01:15:16 +02:00
|
|
|
{
|
2020-11-26 14:33:31 +01:00
|
|
|
if (curState is not StackComponentState cast)
|
2020-04-12 01:15:16 +02:00
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Count = cast.Count;
|
|
|
|
|
MaxCount = cast.MaxCount;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2020-01-09 00:27:52 +01:00
|
|
|
[Serializable, NetSerializable]
|
2020-04-12 01:15:16 +02:00
|
|
|
private sealed class StackComponentState : ComponentState
|
2020-01-09 00:27:52 +01:00
|
|
|
{
|
|
|
|
|
public int Count { get; }
|
|
|
|
|
public int MaxCount { get; }
|
|
|
|
|
|
2020-06-08 16:49:05 -04:00
|
|
|
public StackComponentState(int count, int maxCount) : base(ContentNetIDs.STACK)
|
2020-01-09 00:27:52 +01:00
|
|
|
{
|
|
|
|
|
Count = count;
|
|
|
|
|
MaxCount = maxCount;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|