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

@@ -16,9 +16,8 @@ namespace Content.Server.Construction.Completions
public async Task PerformAction(IEntity entity, IEntity? user)
{
if (entity.Deleted) return;
if(!entity.TryGetComponent<StackComponent>(out var stack)) return;
EntitySystem.Get<StackSystem>().SetCount(entity.Uid, stack, Amount);
EntitySystem.Get<StackSystem>().SetCount(entity.Uid, Amount);
}
}
}

View File

@@ -27,7 +27,7 @@ namespace Content.Server.Construction.Completions
{
var stackEnt = entityManager.SpawnEntity(Prototype, coordinates);
var stack = stackEnt.GetComponent<StackComponent>();
EntitySystem.Get<StackSystem>().SetCount(stackEnt.Uid, stack, Amount);
EntitySystem.Get<StackSystem>().SetCount(stackEnt.Uid, Amount, stack);
}
else
{

View File

@@ -265,7 +265,7 @@ namespace Content.Server.Construction.Components
if (materialStep.EntityValid(eventArgs.Using, out var stack)
&& await doAfterSystem.WaitDoAfter(doAfterArgs) == DoAfterStatus.Finished)
{
var splitStack = EntitySystem.Get<StackSystem>().Split(eventArgs.Using.Uid, stack, materialStep.Amount, eventArgs.User.Transform.Coordinates);
var splitStack = EntitySystem.Get<StackSystem>().Split(eventArgs.Using.Uid, materialStep.Amount, eventArgs.User.Transform.Coordinates, stack);
if (splitStack != null)
{

View File

@@ -313,7 +313,7 @@ namespace Content.Server.Construction.Components
return true;
}
var splitStack = EntitySystem.Get<StackSystem>().Split(eventArgs.Using.Uid, stack, needed, Owner.Transform.Coordinates);
var splitStack = EntitySystem.Get<StackSystem>().Split(eventArgs.Using.Uid, needed, Owner.Transform.Coordinates, stack);
if (splitStack == null)
return false;

View File

@@ -59,7 +59,7 @@ namespace Content.Server.Construction.Components
// TODO: If something has a stack... Just use a prototype with a single thing in the stack.
// This is not a good way to do it.
if (droppedEnt.TryGetComponent<StackComponent>(out var stack))
EntitySystem.Get<StackSystem>().SetCount(droppedEnt.Uid, stack, 1);
EntitySystem.Get<StackSystem>().SetCount(droppedEnt.Uid,1, stack);
}
return true;

View File

@@ -225,7 +225,7 @@ namespace Content.Server.Construction
if (!materialStep.EntityValid(entity, out var stack))
continue;
var splitStack = _stackSystem.Split(entity.Uid, stack, materialStep.Amount, user.ToCoordinates());
var splitStack = _stackSystem.Split(entity.Uid, materialStep.Amount, user.ToCoordinates(), stack);
if (splitStack == null)
continue;

View File

@@ -33,7 +33,7 @@ namespace Content.Server.Destructible.Thresholds.Behaviors
{
var spawned = owner.EntityManager.SpawnEntity(entityId, owner.Transform.MapPosition);
var stack = spawned.GetComponent<StackComponent>();
EntitySystem.Get<StackSystem>().SetCount(spawned.Uid, stack, count);
EntitySystem.Get<StackSystem>().SetCount(spawned.Uid, count, stack);
spawned.RandomOffset(0.5f);
}
else

View File

@@ -61,7 +61,7 @@ namespace Content.Server.Engineering.EntitySystems
return;
if (component.Owner.TryGetComponent<SharedStackComponent>(out var stackComp)
&& component.RemoveOnInteract && !_stackSystem.Use(uid, stackComp, 1))
&& component.RemoveOnInteract && !_stackSystem.Use(uid, 1, stackComp))
{
return;
}

View File

@@ -238,7 +238,7 @@ namespace Content.Server.Hands.Systems
if (throwEnt.TryGetComponent(out StackComponent? stack) && stack.Count > 1 && stack.ThrowIndividually)
{
var splitStack = _stackSystem.Split(throwEnt.Uid, stack, 1, playerEnt.Transform.Coordinates);
var splitStack = _stackSystem.Split(throwEnt.Uid, 1, playerEnt.Transform.Coordinates, stack);
if (splitStack == null)
return false;

View File

@@ -43,7 +43,7 @@ namespace Content.Server.Medical.Components
return true;
}
if (Owner.TryGetComponent<SharedStackComponent>(out var stack) && !EntitySystem.Get<StackSystem>().Use(Owner.Uid, stack, 1))
if (Owner.TryGetComponent<SharedStackComponent>(out var stack) && !EntitySystem.Get<StackSystem>().Use(Owner.Uid, 1, stack))
{
return true;
}

View File

@@ -43,7 +43,7 @@ namespace Content.Server.Power.Components
// TODO: Literally just use a prototype that has a single thing in the stack, it's not that complicated...
if (droppedEnt.TryGetComponent<StackComponent>(out var stack))
EntitySystem.Get<StackSystem>().SetCount(droppedEnt.Uid, stack, 1);
EntitySystem.Get<StackSystem>().SetCount(droppedEnt.Uid, 1, stack);
return true;
}

View File

@@ -47,7 +47,7 @@ namespace Content.Server.Power.Components
}
if (Owner.TryGetComponent<StackComponent>(out var stack)
&& !EntitySystem.Get<StackSystem>().Use(Owner.Uid, stack, 1))
&& !EntitySystem.Get<StackSystem>().Use(Owner.Uid, 1, stack))
return true;
Owner.EntityManager.SpawnEntity(_cablePrototypeID, grid.GridTileToLocal(snapPos));

View File

@@ -31,15 +31,18 @@ namespace Content.Server.Stack
/// <summary>
/// Try to split this stack into two. Returns a non-null <see cref="IEntity"/> if successful.
/// </summary>
public IEntity? Split(EntityUid uid, SharedStackComponent stack, int amount, EntityCoordinates spawnPosition)
public IEntity? Split(EntityUid uid, int amount, EntityCoordinates spawnPosition, SharedStackComponent? stack = null)
{
if (!Resolve(uid, ref stack))
return null;
// Get a prototype ID to spawn the new entity. Null is also valid, although it should rarely be picked...
var prototype = _prototypeManager.TryIndex<StackPrototype>(stack.StackTypeId, out var stackType)
? stackType.Spawn
: stack.Owner.Prototype?.ID ?? null;
// Try to remove the amount of things we want to split from the original stack...
if (!Use(uid, stack, amount))
if (!Use(uid, amount, stack))
return null;
// Set the output parameter in the event instance to the newly split stack.
@@ -48,7 +51,7 @@ namespace Content.Server.Stack
if (ComponentManager.TryGetComponent(entity.Uid, out SharedStackComponent? stackComp))
{
// Set the split stack's count.
SetCount(entity.Uid, stackComp, amount);
SetCount(entity.Uid, amount, stackComp);
}
return entity;
@@ -64,7 +67,7 @@ namespace Content.Server.Stack
var stack = ComponentManager.GetComponent<StackComponent>(entity.Uid);
// And finally, set the correct amount!
SetCount(entity.Uid, stack, amount);
SetCount(entity.Uid, amount, stack);
return entity;
}
@@ -77,8 +80,8 @@ namespace Content.Server.Stack
return;
var toTransfer = Math.Min(stack.Count, otherStack.AvailableSpace);
SetCount(uid, stack, stack.Count - toTransfer);
SetCount(args.Used.Uid, otherStack, otherStack.Count + toTransfer);
SetCount(uid, stack.Count - toTransfer, stack);
SetCount(args.Used.Uid, otherStack.Count + toTransfer, otherStack);
var popupPos = args.ClickLocation;
if (!popupPos.IsValid(EntityManager))

View File

@@ -83,7 +83,7 @@ namespace Content.Server.Tiles
if (HasBaseTurf(currentTileDefinition, baseTurf.Name))
{
if (!EntitySystem.Get<StackSystem>().Use(Owner.Uid, stack, 1))
if (!EntitySystem.Get<StackSystem>().Use(Owner.Uid, 1, stack))
continue;
PlaceAt(mapGrid, location, currentTileDefinition.TileId);

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)