From e2739df5cafc9e32a1aa74d52aa7186ca19f62c0 Mon Sep 17 00:00:00 2001
From: Simon Brodtmann <github@accounts.brodtmann.info>
Date: Sun, 2 Feb 2025 17:16:29 +0100
Subject: [PATCH] Recipe code now also works if the main result is not the
 first result in the results list

---
 hot-metals/changelog.txt             |  1 +
 hot-metals/info.json                 |  3 ++-
 hot-metals/prototypes/hot-metals.lua | 22 +++++++++++++++-------
 3 files changed, 18 insertions(+), 8 deletions(-)

diff --git a/hot-metals/changelog.txt b/hot-metals/changelog.txt
index ec2575e..5f2b7be 100644
--- a/hot-metals/changelog.txt
+++ b/hot-metals/changelog.txt
@@ -3,6 +3,7 @@ Version: 1.2.2
 Date: 02.02.2025
   Changes:
     - Recipes always output "fresh" hot metals (https://mods.factorio.com/mod/hot-metals/discussion/6775293c1c4d7b0e4bba8648)
+    - Recipe code now also works if the main result is not the first result in the results list
 ---------------------------------------------------------------------------------------------------
 Version: 1.2.1
 Date: 09.01.2025
diff --git a/hot-metals/info.json b/hot-metals/info.json
index 150ff51..96ba501 100644
--- a/hot-metals/info.json
+++ b/hot-metals/info.json
@@ -7,7 +7,8 @@
    "homepage": "",
    "factorio_version": "2.0",
    "dependencies": [
-      "base"
+      "base",
+      "cf-lib >= 0.0.2"
    ],
    "spoiling_required": true
 }
\ No newline at end of file
diff --git a/hot-metals/prototypes/hot-metals.lua b/hot-metals/prototypes/hot-metals.lua
index 910e3ea..296fa85 100644
--- a/hot-metals/prototypes/hot-metals.lua
+++ b/hot-metals/prototypes/hot-metals.lua
@@ -27,14 +27,22 @@ function createHotVariant(item)
 
     -- Change recipes
     for _, recipe in pairs(data.raw.recipe) do
-        if contains(HotMetals.craftingCategories, recipe.category) and recipe.results[1].name == itemName then
-            recipe.results[1].name = hotItem.name
-            recipe.localised_name = { "item-name." .. itemName }
-            if recipe.main_product == itemName then
-                recipe.main_product = hotItem.name
+        if contains(HotMetals.craftingCategories, recipe.category) then
+            local function isResultMatch(result)
+                return result.name == itemName
+            end
+            local results = table.filter(recipe.results, isResultMatch)
+            if #results > 0 then
+                for _, result in pairs(results) do
+                    result.name = hotItem.name
+                end
+                recipe.localised_name = { "item-name." .. itemName }
+                if recipe.main_product == itemName then
+                    recipe.main_product = hotItem.name
+                end
+                recipe.preserve_products_in_machine_output = true
+                recipe.result_is_always_fresh = true
             end
-            recipe.preserve_products_in_machine_output = true
-            recipe.result_is_always_fresh = true
         end
     end