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 string Name => "Stack";
public sealed override uint? NetID => ContentNetIDs.STACK; public sealed override uint? NetID => ContentNetIDs.STACK;
[DataField("max")]
private int _maxCount = 30;
[ViewVariables(VVAccess.ReadWrite)] [ViewVariables(VVAccess.ReadWrite)]
[DataField("stackType", required:true, customTypeSerializer:typeof(PrototypeIdSerializer<StackPrototype>))] [DataField("stackType", required:true, customTypeSerializer:typeof(PrototypeIdSerializer<StackPrototype>))]
public string StackTypeId { get; private set; } = string.Empty; public string StackTypeId { get; private set; } = string.Empty;
@@ -29,19 +26,12 @@ namespace Content.Shared.Stacks
[DataField("count")] [DataField("count")]
public int Count { get; set; } = 30; public int Count { get; set; } = 30;
[ViewVariables] /// <summary>
public int MaxCount /// Max amount of things that can be in the stack.
{ /// </summary>
get => _maxCount; [ViewVariables(VVAccess.ReadOnly)]
private set [DataField("max")]
{ public int MaxCount { get; set; } = 30;
if (_maxCount == value)
return;
_maxCount = value;
Dirty();
}
}
[ViewVariables] [ViewVariables]
public int AvailableSpace => MaxCount - Count; public int AvailableSpace => MaxCount - Count;
@@ -50,20 +40,10 @@ namespace Content.Shared.Stacks
{ {
return new StackComponentState(Count, MaxCount); return new StackComponentState(Count, MaxCount);
} }
public override void HandleComponentState(ComponentState? curState, ComponentState? nextState)
{
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] [Serializable, NetSerializable]
private sealed class StackComponentState : ComponentState public sealed class StackComponentState : ComponentState
{ {
public int Count { get; } public int Count { get; }
public int MaxCount { get; } public int MaxCount { get; }
@@ -75,4 +55,3 @@ namespace Content.Shared.Stacks
} }
} }
} }
}

View File

@@ -1,6 +1,7 @@
using Content.Shared.Examine; using Content.Shared.Examine;
using JetBrains.Annotations; using JetBrains.Annotations;
using Robust.Shared.GameObjects; using Robust.Shared.GameObjects;
using Robust.Shared.GameStates;
using Robust.Shared.Localization; using Robust.Shared.Localization;
namespace Content.Shared.Stacks namespace Content.Shared.Stacks
@@ -12,6 +13,7 @@ namespace Content.Shared.Stacks
{ {
base.Initialize(); base.Initialize();
SubscribeLocalEvent<SharedStackComponent, ComponentHandleState>(OnStackHandleState);
SubscribeLocalEvent<SharedStackComponent, ComponentStartup>(OnStackStarted); SubscribeLocalEvent<SharedStackComponent, ComponentStartup>(OnStackStarted);
SubscribeLocalEvent<SharedStackComponent, ExaminedEvent>(OnStackExamined); SubscribeLocalEvent<SharedStackComponent, ExaminedEvent>(OnStackExamined);
} }
@@ -60,6 +62,16 @@ namespace Content.Shared.Stacks
RaiseLocalEvent(uid, new StackCountChangedEvent(old, component.Count)); 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) private void OnStackExamined(EntityUid uid, SharedStackComponent component, ExaminedEvent args)
{ {
if (!args.IsInDetailsRange) if (!args.IsInDetailsRange)