Stacks now use ComponentHandleState.

This commit is contained in:
Vera Aguilera Puerto
2021-07-11 11:07:27 +02:00
parent d8bfb7c054
commit 14c2f65b17
2 changed files with 28 additions and 37 deletions

View File

@@ -14,9 +14,6 @@ namespace Content.Shared.Stacks
public sealed override string Name => "Stack";
public sealed override uint? NetID => ContentNetIDs.STACK;
[DataField("max")]
private int _maxCount = 30;
[ViewVariables(VVAccess.ReadWrite)]
[DataField("stackType", required:true, customTypeSerializer:typeof(PrototypeIdSerializer<StackPrototype>))]
public string StackTypeId { get; private set; } = string.Empty;
@@ -29,19 +26,12 @@ namespace Content.Shared.Stacks
[DataField("count")]
public int Count { get; set; } = 30;
[ViewVariables]
public int MaxCount
{
get => _maxCount;
private set
{
if (_maxCount == value)
return;
_maxCount = value;
Dirty();
}
}
/// <summary>
/// Max amount of things that can be in the stack.
/// </summary>
[ViewVariables(VVAccess.ReadOnly)]
[DataField("max")]
public int MaxCount { get; set; } = 30;
[ViewVariables]
public int AvailableSpace => MaxCount - Count;
@@ -50,29 +40,18 @@ namespace Content.Shared.Stacks
{
return new StackComponentState(Count, MaxCount);
}
}
public override void HandleComponentState(ComponentState? curState, ComponentState? nextState)
[Serializable, NetSerializable]
public sealed class StackComponentState : ComponentState
{
public int Count { get; }
public int MaxCount { get; }
public StackComponentState(int count, int maxCount) : base(ContentNetIDs.STACK)
{
if (curState is not StackComponentState cast)
return;
// This will change the count and call events.
EntitySystem.Get<SharedStackSystem>().SetCount(Owner.Uid, this, cast.Count);
MaxCount = cast.MaxCount;
}
[Serializable, NetSerializable]
private sealed class StackComponentState : ComponentState
{
public int Count { get; }
public int MaxCount { get; }
public StackComponentState(int count, int maxCount) : base(ContentNetIDs.STACK)
{
Count = count;
MaxCount = maxCount;
}
Count = count;
MaxCount = maxCount;
}
}
}

View File

@@ -1,6 +1,7 @@
using Content.Shared.Examine;
using JetBrains.Annotations;
using Robust.Shared.GameObjects;
using Robust.Shared.GameStates;
using Robust.Shared.Localization;
namespace Content.Shared.Stacks
@@ -12,6 +13,7 @@ namespace Content.Shared.Stacks
{
base.Initialize();
SubscribeLocalEvent<SharedStackComponent, ComponentHandleState>(OnStackHandleState);
SubscribeLocalEvent<SharedStackComponent, ComponentStartup>(OnStackStarted);
SubscribeLocalEvent<SharedStackComponent, ExaminedEvent>(OnStackExamined);
}
@@ -60,6 +62,16 @@ namespace Content.Shared.Stacks
RaiseLocalEvent(uid, new StackCountChangedEvent(old, component.Count));
}
private void OnStackHandleState(EntityUid uid, SharedStackComponent component, ComponentHandleState args)
{
if (args.Current is not StackComponentState cast)
return;
// This will change the count and call events.
SetCount(uid, component, cast.Count);
component.MaxCount = cast.MaxCount;
}
private void OnStackExamined(EntityUid uid, SharedStackComponent component, ExaminedEvent args)
{
if (!args.IsInDetailsRange)