I'm sorry for not being active here for a long time, I haven't played Factorio for a while. But I'm currently working on fixing the issue with the blueprint library.
something like this might do the trick? my personal adjustments
my changes start at line 160
local function caculatePlayerItem(player)
local stack = player.cursor_stack
local record = player.cursor_record
local target = nil
-- 1. Check Library Record (for blueprints/books directly from the B library)
if record and record.valid then
if record.type == "blueprint" then
target = record
elseif record.type == "blueprint-book" then
-- For library books, we use get_selected_record to find the active blueprint
local active_record = record.get_selected_record(player)
if active_record and active_record.valid and active_record.type == "blueprint" then
target = active_record
end
end
end
-- 2. Check Physical ItemStack (for items in your actual inventory)
if not target and stack and stack.valid_for_read then
if stack.name == "blueprint" then
target = stack
elseif stack.name == "blueprint-book" then
local inventory = stack.get_inventory(defines.inventory.item_main)
-- Physical books use active_index to point to their internal inventory
if inventory and stack.active_index and stack.active_index <= #inventory then
local active_bp = inventory[stack.active_index]
if active_bp and active_bp.valid_for_read then
target = active_bp
end
end
end
end
-- Process the blueprint if we found one
if target then
local count = nil
-- Safely get cost_to_build (prevents crash on empty/unsetup blueprints)
local success, material = pcall(function() return target.cost_to_build end)
if not success or type(material) ~= "table" then return "" end
for _, required in pairs(material) do
local item_count = countPlayerItems(player, required.name, required.quality)
local times = item_count / required.count
if count == nil or times < count then
count = times
end
end
if count == nil then return "" end
return "x" .. format_number(player, math.floor(count))
else
-- Regular item logic
if not stack or not stack.valid_for_read then return "" end
local item_count = stack.count + countPlayerItems(player, stack.name, stack.quality)
return format_number(player, item_count)
end
end
local function refresh(e)
local player = game.players[e.player_index]
local has_item = player.cursor_stack.valid_for_read or (player.cursor_record and player.cursor_record.valid)
if has_item and item_check(player) then
create_gui(player)
player.gui.center.total_item_count.caption = caculatePlayerItem(player)
else
remove_gui(player)
end
end