From 61d88527998323d9b6bdb3ed13725607c9b69cef Mon Sep 17 00:00:00 2001 From: Vera Aguilera Puerto <6766154+Zumorica@users.noreply.github.com> Date: Mon, 20 Sep 2021 13:39:05 +0200 Subject: [PATCH] Update StackSystem to use Resolves. (#4664) * Update StackSystem to use Resolves. * Compile --- .../Construction/Completions/SetStackCount.cs | 3 +- .../Completions/SpawnPrototype.cs | 2 +- .../Components/ConstructionComponent.cs | 2 +- .../Components/MachineFrameComponent.cs | 2 +- .../Components/WelderRefinableComponent.cs | 2 +- .../Construction/ConstructionSystem.cs | 2 +- .../Behaviors/SpawnEntitiesBehavior.cs | 2 +- .../EntitySystems/SpawnAfterInteractSystem.cs | 2 +- Content.Server/Hands/Systems/HandsSystem.cs | 2 +- .../Medical/Components/HealingComponent.cs | 2 +- .../Power/Components/CableComponent.cs | 2 +- .../Power/Components/CablePlacerComponent.cs | 2 +- Content.Server/Stack/StackSystem.cs | 15 +++++---- .../Tiles/FloorTileItemComponent.cs | 2 +- Content.Shared/Stacks/SharedStackSystem.cs | 33 +++++++++++-------- 15 files changed, 42 insertions(+), 33 deletions(-) diff --git a/Content.Server/Construction/Completions/SetStackCount.cs b/Content.Server/Construction/Completions/SetStackCount.cs index 1d25b42748..1e10189150 100644 --- a/Content.Server/Construction/Completions/SetStackCount.cs +++ b/Content.Server/Construction/Completions/SetStackCount.cs @@ -16,9 +16,8 @@ namespace Content.Server.Construction.Completions public async Task PerformAction(IEntity entity, IEntity? user) { if (entity.Deleted) return; - if(!entity.TryGetComponent(out var stack)) return; - EntitySystem.Get().SetCount(entity.Uid, stack, Amount); + EntitySystem.Get().SetCount(entity.Uid, Amount); } } } diff --git a/Content.Server/Construction/Completions/SpawnPrototype.cs b/Content.Server/Construction/Completions/SpawnPrototype.cs index caeeb7124a..30ce414bcf 100644 --- a/Content.Server/Construction/Completions/SpawnPrototype.cs +++ b/Content.Server/Construction/Completions/SpawnPrototype.cs @@ -27,7 +27,7 @@ namespace Content.Server.Construction.Completions { var stackEnt = entityManager.SpawnEntity(Prototype, coordinates); var stack = stackEnt.GetComponent(); - EntitySystem.Get().SetCount(stackEnt.Uid, stack, Amount); + EntitySystem.Get().SetCount(stackEnt.Uid, Amount, stack); } else { diff --git a/Content.Server/Construction/Components/ConstructionComponent.cs b/Content.Server/Construction/Components/ConstructionComponent.cs index e124f00980..1ba1aca3dd 100644 --- a/Content.Server/Construction/Components/ConstructionComponent.cs +++ b/Content.Server/Construction/Components/ConstructionComponent.cs @@ -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().Split(eventArgs.Using.Uid, stack, materialStep.Amount, eventArgs.User.Transform.Coordinates); + var splitStack = EntitySystem.Get().Split(eventArgs.Using.Uid, materialStep.Amount, eventArgs.User.Transform.Coordinates, stack); if (splitStack != null) { diff --git a/Content.Server/Construction/Components/MachineFrameComponent.cs b/Content.Server/Construction/Components/MachineFrameComponent.cs index b430a2f5be..12772ca336 100644 --- a/Content.Server/Construction/Components/MachineFrameComponent.cs +++ b/Content.Server/Construction/Components/MachineFrameComponent.cs @@ -313,7 +313,7 @@ namespace Content.Server.Construction.Components return true; } - var splitStack = EntitySystem.Get().Split(eventArgs.Using.Uid, stack, needed, Owner.Transform.Coordinates); + var splitStack = EntitySystem.Get().Split(eventArgs.Using.Uid, needed, Owner.Transform.Coordinates, stack); if (splitStack == null) return false; diff --git a/Content.Server/Construction/Components/WelderRefinableComponent.cs b/Content.Server/Construction/Components/WelderRefinableComponent.cs index eb2042a29a..620f1f3c05 100644 --- a/Content.Server/Construction/Components/WelderRefinableComponent.cs +++ b/Content.Server/Construction/Components/WelderRefinableComponent.cs @@ -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(out var stack)) - EntitySystem.Get().SetCount(droppedEnt.Uid, stack, 1); + EntitySystem.Get().SetCount(droppedEnt.Uid,1, stack); } return true; diff --git a/Content.Server/Construction/ConstructionSystem.cs b/Content.Server/Construction/ConstructionSystem.cs index 12b708bb8a..d699f5f4db 100644 --- a/Content.Server/Construction/ConstructionSystem.cs +++ b/Content.Server/Construction/ConstructionSystem.cs @@ -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; diff --git a/Content.Server/Destructible/Thresholds/Behaviors/SpawnEntitiesBehavior.cs b/Content.Server/Destructible/Thresholds/Behaviors/SpawnEntitiesBehavior.cs index 6cbbc49b07..dbbbe7ac17 100644 --- a/Content.Server/Destructible/Thresholds/Behaviors/SpawnEntitiesBehavior.cs +++ b/Content.Server/Destructible/Thresholds/Behaviors/SpawnEntitiesBehavior.cs @@ -33,7 +33,7 @@ namespace Content.Server.Destructible.Thresholds.Behaviors { var spawned = owner.EntityManager.SpawnEntity(entityId, owner.Transform.MapPosition); var stack = spawned.GetComponent(); - EntitySystem.Get().SetCount(spawned.Uid, stack, count); + EntitySystem.Get().SetCount(spawned.Uid, count, stack); spawned.RandomOffset(0.5f); } else diff --git a/Content.Server/Engineering/EntitySystems/SpawnAfterInteractSystem.cs b/Content.Server/Engineering/EntitySystems/SpawnAfterInteractSystem.cs index 61bfad8791..7fcad8a294 100644 --- a/Content.Server/Engineering/EntitySystems/SpawnAfterInteractSystem.cs +++ b/Content.Server/Engineering/EntitySystems/SpawnAfterInteractSystem.cs @@ -61,7 +61,7 @@ namespace Content.Server.Engineering.EntitySystems return; if (component.Owner.TryGetComponent(out var stackComp) - && component.RemoveOnInteract && !_stackSystem.Use(uid, stackComp, 1)) + && component.RemoveOnInteract && !_stackSystem.Use(uid, 1, stackComp)) { return; } diff --git a/Content.Server/Hands/Systems/HandsSystem.cs b/Content.Server/Hands/Systems/HandsSystem.cs index 13bddd4408..3b51c70af2 100644 --- a/Content.Server/Hands/Systems/HandsSystem.cs +++ b/Content.Server/Hands/Systems/HandsSystem.cs @@ -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; diff --git a/Content.Server/Medical/Components/HealingComponent.cs b/Content.Server/Medical/Components/HealingComponent.cs index 1548e1b908..f03333c57e 100644 --- a/Content.Server/Medical/Components/HealingComponent.cs +++ b/Content.Server/Medical/Components/HealingComponent.cs @@ -43,7 +43,7 @@ namespace Content.Server.Medical.Components return true; } - if (Owner.TryGetComponent(out var stack) && !EntitySystem.Get().Use(Owner.Uid, stack, 1)) + if (Owner.TryGetComponent(out var stack) && !EntitySystem.Get().Use(Owner.Uid, 1, stack)) { return true; } diff --git a/Content.Server/Power/Components/CableComponent.cs b/Content.Server/Power/Components/CableComponent.cs index 4c0623f3cc..31310b79fd 100644 --- a/Content.Server/Power/Components/CableComponent.cs +++ b/Content.Server/Power/Components/CableComponent.cs @@ -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(out var stack)) - EntitySystem.Get().SetCount(droppedEnt.Uid, stack, 1); + EntitySystem.Get().SetCount(droppedEnt.Uid, 1, stack); return true; } diff --git a/Content.Server/Power/Components/CablePlacerComponent.cs b/Content.Server/Power/Components/CablePlacerComponent.cs index 9542636d85..131016654e 100644 --- a/Content.Server/Power/Components/CablePlacerComponent.cs +++ b/Content.Server/Power/Components/CablePlacerComponent.cs @@ -47,7 +47,7 @@ namespace Content.Server.Power.Components } if (Owner.TryGetComponent(out var stack) - && !EntitySystem.Get().Use(Owner.Uid, stack, 1)) + && !EntitySystem.Get().Use(Owner.Uid, 1, stack)) return true; Owner.EntityManager.SpawnEntity(_cablePrototypeID, grid.GridTileToLocal(snapPos)); diff --git a/Content.Server/Stack/StackSystem.cs b/Content.Server/Stack/StackSystem.cs index e64197d106..483221de99 100644 --- a/Content.Server/Stack/StackSystem.cs +++ b/Content.Server/Stack/StackSystem.cs @@ -31,15 +31,18 @@ namespace Content.Server.Stack /// /// Try to split this stack into two. Returns a non-null if successful. /// - 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(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(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)) diff --git a/Content.Server/Tiles/FloorTileItemComponent.cs b/Content.Server/Tiles/FloorTileItemComponent.cs index 4cb9b5de34..ce04224e2e 100644 --- a/Content.Server/Tiles/FloorTileItemComponent.cs +++ b/Content.Server/Tiles/FloorTileItemComponent.cs @@ -83,7 +83,7 @@ namespace Content.Server.Tiles if (HasBaseTurf(currentTileDefinition, baseTurf.Name)) { - if (!EntitySystem.Get().Use(Owner.Uid, stack, 1)) + if (!EntitySystem.Get().Use(Owner.Uid, 1, stack)) continue; PlaceAt(mapGrid, location, currentTileDefinition.TileId); diff --git a/Content.Shared/Stacks/SharedStackSystem.cs b/Content.Shared/Stacks/SharedStackSystem.cs index 5b33ec8fa2..6092b5afcf 100644 --- a/Content.Shared/Stacks/SharedStackSystem.cs +++ b/Content.Shared/Stacks/SharedStackSystem.cs @@ -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(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 /// /// Try to use an amount of items on this stack. Returns whether this succeeded. /// - 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)