From: korczoczek Date: Wed, 14 Jan 2026 21:12:30 +0000 (+0100) Subject: Make lathes refund materials when recipe gets cancelled (#42416) X-Git-Url: https://git.smokeofanarchy.ru/gitweb.cgi?a=commitdiff_plain;h=acdeac6172b3c436f0609eee83573703d29821bf;p=space-station-14.git Make lathes refund materials when recipe gets cancelled (#42416) 30 day free refund --- diff --git a/Content.Server/Lathe/LatheSystem.cs b/Content.Server/Lathe/LatheSystem.cs index 97602ad2ba..cce17590e8 100644 --- a/Content.Server/Lathe/LatheSystem.cs +++ b/Content.Server/Lathe/LatheSystem.cs @@ -182,15 +182,8 @@ namespace Content.Server.Lathe if (!CanProduce(uid, recipe, quantity, component)) return false; - foreach (var (mat, amount) in recipe.Materials) - { - var adjustedAmount = recipe.ApplyMaterialDiscount - ? (int)(-amount * component.MaterialUseMultiplier) - : -amount; - adjustedAmount *= quantity; - - _materialStorage.TryChangeMaterialAmount(uid, mat, adjustedAmount); - } + foreach (var (mat, amount) in GetAdjustedAmount(component, recipe)) + _materialStorage.TryChangeMaterialAmount(uid, mat, -amount * quantity); if (component.Queue.Last is { } node && node.ValueRef.Recipe == recipe.ID) node.ValueRef.ItemsRequested += quantity; @@ -429,6 +422,48 @@ namespace Content.Server.Lathe return GetAvailableRecipes(uid, component).Contains(recipe.ID); } + /// + /// Iterator returning adjusted amount of material needed to + /// produce a given recipe + /// + private static IEnumerable<(ProtoId mat, int amount)> GetAdjustedAmount(LatheComponent lathe, LatheRecipePrototype recipe) + { + foreach (var (mat, amount) in recipe.Materials) + { + var adjustedAmount = recipe.ApplyMaterialDiscount + ? (int)(amount * lathe.MaterialUseMultiplier) + : amount; + + yield return (mat, adjustedAmount); + } + } + + /// + /// Refunds the material cost of the currently running recipe, + /// without cancelling production + /// + private void RefundCurrentRecipe(EntityUid uid, LatheComponent lathe) + { + _proto.Resolve(lathe.CurrentRecipe, out var recipe); + + foreach (var (mat, amount) in GetAdjustedAmount(lathe, recipe!)) + _materialStorage.TryChangeMaterialAmount(uid, mat, amount); + } + + /// + /// Refunds the material cost of a given batch, + /// without deleting it + /// + private void RefundBatch(EntityUid uid, LatheComponent lathe, LatheRecipeBatch batch) + { + var delta = batch.ItemsRequested - batch.ItemsPrinted; + + _proto.Resolve(batch.Recipe, out var recipe); + + foreach (var (mat, amount) in GetAdjustedAmount(lathe, recipe!)) + _materialStorage.TryChangeMaterialAmount(uid, mat, amount * delta); + } + public void AbortProduction(EntityUid uid, LatheComponent? component = null) { if (!Resolve(uid, ref component)) @@ -451,6 +486,7 @@ namespace Content.Server.Lathe } } + RefundCurrentRecipe(uid, component); component.CurrentRecipe = null; } RemCompDeferred(uid); @@ -504,6 +540,7 @@ namespace Content.Server.Lathe LogImpact.Low, $"{ToPrettyString(args.Actor):player} deleted a lathe job for ({batch.ItemsPrinted}/{batch.ItemsRequested}) {GetRecipeName(batch.Recipe)} at {ToPrettyString(uid):lathe}"); + RefundBatch(uid, component, batch); component.Queue.Remove(node); UpdateUserInterfaceState(uid, component); } @@ -562,6 +599,7 @@ namespace Content.Server.Lathe LogImpact.Low, $"{ToPrettyString(args.Actor):player} aborted printing {GetRecipeName(component.CurrentRecipe.Value)} at {ToPrettyString(uid):lathe}"); + RefundCurrentRecipe(uid, component); component.CurrentRecipe = null; FinishProducing(uid, component); }