Yemto's Item Count

by Yemto

Shows the total item amount of what you are holding, unless you hold in a blueprint, then it shows how many times you can complete the blueprint.

Utilities
a day ago
0.16 - 2.0
3.14K

i Support for blueprint library

6 months ago

Would it be possible for it to support blueprints in the blueprint library?

Also an option to enable only showing blueprint count?

4 days ago

+1

3 days ago
(updated 3 days ago)

Would it be possible for it to support blueprints in the blueprint library?

Also an option to enable only showing blueprint count?

think i've mananged to add that. yet to add the option you asked for but as it is now it shows for items, bp and bp books from inventory and both lib's.

let me know if you're still interested, and I'll upload it for you.

spent a few hours trying to get it done with no lua knowledge what so ever lol

2 days ago
(updated 2 days ago)

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.

a day ago

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

New response