Update StackSystem to use Resolves. (#4664)

* Update StackSystem to use Resolves.

* Compile
This commit is contained in:
Vera Aguilera Puerto
2021-09-20 13:39:05 +02:00
committed by GitHub
parent ae2b70072f
commit 61d8852799
15 changed files with 42 additions and 33 deletions

View File

@@ -3,6 +3,7 @@ using JetBrains.Annotations;
using Robust.Shared.GameObjects;
using Robust.Shared.GameStates;
using Robust.Shared.Localization;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom;
namespace Content.Shared.Stacks
{
@@ -19,18 +20,11 @@ namespace Content.Shared.Stacks
SubscribeLocalEvent<SharedStackComponent, ExaminedEvent>(OnStackExamined);
}
private void OnStackStarted(EntityUid uid, SharedStackComponent component, ComponentStartup args)
public void SetCount(EntityUid uid, int amount, SharedStackComponent? component = null)
{
if (!ComponentManager.TryGetComponent(uid, out SharedAppearanceComponent? appearance))
if (!Resolve(uid, ref component))
return;
appearance.SetData(StackVisuals.Actual, component.Count);
appearance.SetData(StackVisuals.MaxCount, component.MaxCount);
appearance.SetData(StackVisuals.Hide, false);
}
public void SetCount(EntityUid uid, SharedStackComponent component, int amount)
{
// Do nothing if amount is already the same.
if (amount == component.Count)
return;
@@ -66,8 +60,11 @@ namespace Content.Shared.Stacks
/// <summary>
/// Try to use an amount of items on this stack. Returns whether this succeeded.
/// </summary>
public bool Use(EntityUid uid, SharedStackComponent stack, int amount)
public bool Use(EntityUid uid, int amount, SharedStackComponent? stack = null)
{
if (!Resolve(uid, ref stack))
return false;
// Check if we have enough things in the stack for this...
if (stack.Count < amount)
{
@@ -76,10 +73,20 @@ namespace Content.Shared.Stacks
}
// We do have enough things in the stack, so remove them and change.
SetCount(uid, stack, stack.Count - amount);
SetCount(uid, stack.Count - amount, stack);
return true;
}
private void OnStackStarted(EntityUid uid, SharedStackComponent component, ComponentStartup args)
{
if (!ComponentManager.TryGetComponent(uid, out SharedAppearanceComponent? appearance))
return;
appearance.SetData(StackVisuals.Actual, component.Count);
appearance.SetData(StackVisuals.MaxCount, component.MaxCount);
appearance.SetData(StackVisuals.Hide, false);
}
private void OnStackGetState(EntityUid uid, SharedStackComponent component, ref ComponentGetState args)
{
args.State = new StackComponentState(component.Count, component.MaxCount);
@@ -90,9 +97,9 @@ namespace Content.Shared.Stacks
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;
// This will change the count and call events.
SetCount(uid, cast.Count, component);
}
private void OnStackExamined(EntityUid uid, SharedStackComponent component, ExaminedEvent args)