{"record":{"id":"4e14a1a2a6b00187","repo":"babalae/better-genshin-impact","slug":"targetquantity-maxquanti","errorCode":null,"errorMessage":"材料不足以合成指定个数，目标 {targetQuantity}，当前最多可合成 {maxQuantity}。","messagePattern":"材料不足以合成指定个数，目标 (.+?)，当前最多可合成 (.+?)。","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"warning","filePath":"BetterGenshinImpact/GameTask/Common/Job/CraftMaterialTask.cs","lineNumber":392,"sourceCode":"    /// 设置本次合成个数并校验最终数量。\n    /// </summary>\n    /// <param name=\"targetQuantity\">目标合成个数。</param>\n    /// <returns>最终校验到的合成个数。</returns>\n    /// <exception cref=\"InvalidOperationException\">最大可合成个数、当前合成个数读取失败，材料不足或最终数量不匹配时抛出。</exception>\n    private async Task<int> SetCraftQuantity(int targetQuantity)\n    {\n        var addButton = (X: 1614, Y: 672);\n        var reduceButton = (X: 1074, Y: 672);\n\n        var maxQuantity = ReadMaxCraftQuantity();\n        if (maxQuantity <= 0)\n        {\n            throw new InvalidOperationException(\"未能识别最大可合成个数。\");\n        }\n\n        if (targetQuantity > maxQuantity)\n        {\n            throw new InvalidOperationException($\"材料不足以合成指定个数，目标 {targetQuantity}，当前最多可合成 {maxQuantity}。\");\n        }\n\n        if (maxQuantity == 1)\n        {\n            return 1;\n        }\n\n        await DragSliderToRatio(0);\n        await Delay(250, _ct);\n\n        var ratio = Math.Clamp((targetQuantity - 1d) / (maxQuantity - 1d), 0d, 1d);\n        await DragSliderToRatio(ratio);\n        await Delay(350, _ct);\n\n        var actualQuantity = ReadCurrentCraftQuantity();\n        if (actualQuantity <= 0)\n        {\n            throw new InvalidOperationException(\"未能读取当前合成个数。\");","sourceCodeStart":374,"sourceCodeEnd":410,"githubUrl":"https://github.com/babalae/better-genshin-impact/blob/a7cb36712dcb409be610257d877fcea3597e9d6b/BetterGenshinImpact/GameTask/Common/Job/CraftMaterialTask.cs#L374-L410","documentation":"Thrown by CraftMaterialTask.SetCraftQuantity when the requested targetQuantity exceeds the max craftable quantity read from the crafting UI. The max is obtained via ReadMaxCraftQuantity(), which OCRs the right-hand detail panel. This is a hard precondition guard: the task refuses to drag the slider beyond what the detected materials allow.","triggerScenarios":"Caller passes targetQuantity greater than the value returned by ReadMaxCraftQuantity() (OCR of Rect1080(1522,653,72,32) or the material fraction text). Triggered by requesting e.g. 10 crafts when the UI shows max 3, or when OCR under-reads the real max (e.g. reads 3 instead of 30).","commonSituations":"Recipe config requests more units than the account holds materials for; a game version change shifted the OCR region for the max-quantity number so it reads a smaller value; condensed-resin or material counts were spent between plan time and craft time; UI scaling/resolution misalignment crops the wrong digits.","solutions":["Verify the materials actually exist in-game at the requested quantity before scheduling the craft.","Check that the OCR region Rect1080(1522,653,72,32) still matches the current game layout; re-capture and adjust if a version moved the number.","If the value is genuinely insufficient, lower targetQuantity to <= maxQuantity or gather more materials first.","Add a retry of ReadMaxCraftQuantity before throwing, since a transient OCR miss can understate the max."],"exampleFix":"// before\nif (targetQuantity > maxQuantity)\n{\n    throw new InvalidOperationException($\"材料不足以合成指定个数，目标 {targetQuantity}，当前最多可合成 {maxQuantity}。\");\n}\n\n// after: re-read once to absorb transient OCR misses\nif (targetQuantity > maxQuantity)\n{\n    await Delay(300, _ct);\n    maxQuantity = ReadMaxCraftQuantity();\n}\nif (maxQuantity <= 0 || targetQuantity > maxQuantity)\n{\n    throw new InvalidOperationException($\"材料不足以合成指定个数，目标 {targetQuantity}，当前最多可合成 {maxQuantity}。\");\n}","handlingStrategy":"validation","validationCode":"// Before calling SetCraftQuantity, cap target to the detected max\nvar maxQ = ReadMaxCraftQuantity();\nvar safeTarget = Math.Min(requestedQuantity, maxQ);\nif (maxQ <= 0 || safeTarget <= 0)\n    throw new InvalidOperationException(\"无法确定最大可合成个数，拒绝执行合成。\");\nawait SetCraftQuantity(safeTarget);","typeGuard":"null","tryCatchPattern":"null","preventionTips":["Read the max craftable quantity once and pass a target <= max into SetCraftQuantity.","Treat a ReadMaxCraftQuantity() <= 0 as a hard stop rather than proceeding.","Re-check materials inventory before scheduling a craft batch."],"tags":["ocr","crafting","precondition","genshin-ui"],"backgroundTag":null,"analyzedSha":"a7cb36712dcb409be610257d877fcea3597e9d6b","analyzedAt":"2026-08-13T16:44:57.548Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}