From 2063566ca27ce564a86d389192b33ca045db15df Mon Sep 17 00:00:00 2001 From: "Clarence \"Sparr\" Risher" Date: Sat, 22 Aug 2020 19:23:45 -0700 Subject: [PATCH 1/2] Fix crash on blueprint book creation, also refactor get_blueprint --- util.lua | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/util.lua b/util.lua index fadba7f..38e5d22 100644 --- a/util.lua +++ b/util.lua @@ -4,14 +4,17 @@ local Util = {} -- Returns the item if it is a blueprint, the selected blueprint in the book if it is a blueprint book, or nil. function Util.get_blueprint(bp) - if not (bp and bp.valid and bp.valid_for_read) then - return nil - end - if bp.is_blueprint_book and bp.active_index then - return Util.get_blueprint(bp.get_inventory(defines.inventory.item_main)[bp.active_index]) - end - if bp.is_blueprint then - return bp + while (bp and bp.valid and bp.valid_for_read) do + if bp.is_blueprint then + return bp + elseif bp.is_blueprint_book and bp.active_index then + local inventory = bp.get_inventory(defines.inventory.item_main) + if bp.active_index <= #inventory then + bp = inventory[bp.active_index] + else + return nil + end + end end return nil end From 42c7de027c1f8818a847f09780c394107bcbbff2 Mon Sep 17 00:00:00 2001 From: "Clarence \"Sparr\" Risher" Date: Sat, 22 Aug 2020 22:57:01 -0700 Subject: [PATCH 2/2] Add exit case for get_blueprint refactor --- util.lua | 2 ++ 1 file changed, 2 insertions(+) diff --git a/util.lua b/util.lua index 38e5d22..c91a4ec 100644 --- a/util.lua +++ b/util.lua @@ -14,6 +14,8 @@ function Util.get_blueprint(bp) else return nil end + else + return nil end end return nil