CCCPaste

Refined Storage CC Script

-- Refined Storage Craftable Browser + Craft Queue
-- Requires: CC:Tweaked + Advanced Peripherals (rsBridge)

local bridge = peripheral.find("rsBridge")
if not bridge then error("No RS Bridge found. Attach an RS Bridge block next to the computer!") end

local craftables = {}
local scroll = 0
local pageSize = 15
local running = true

-- Pull craftable items from RS
local function refreshCraftables()
    craftables = bridge.listCraftableItems()
    table.sort(craftables, function(a, b) return a.displayName < b.displayName end)
end

-- Draw UI
local function draw()
    term.clear()
    term.setCursorPos(1,1)
    print("=== Refined Storage Crafting Browser ===")
    print("Scroll: Up/Down | Craft: Enter | Refresh: R | Quit: Q")
    print("----------------------------------------")

    for i = 1, pageSize do
        local idx = i + scroll
        if craftables[idx] then
            print(string.format("%3d: %s", idx, craftables[idx].displayName))
        end
    end
end

-- Ask the user how many to craft
local function craftItem(item)
    term.clear()
    term.setCursorPos(1,1)
    print("Crafting: " .. item.displayName)
    write("Enter amount: ")
    local amt = tonumber(read())

    if not amt or amt < 1 then
        print("Invalid amount.")
        sleep(1.5)
        return
    end

    print("Sending craft request...")
    local success, err = bridge.craftItem({name = item.name, count = amt})

    if success then
        print("Craft queued successfully!")
    else
        print("Craft failed: "..tostring(err))
    end

    print("Press any key to return.")
    os.pullEvent("key")
end

-- Main event loop
local function loop()
    refreshCraftables()
    draw()

    while running do
        local event, key = os.pullEvent()

        if event == "key" then
            if key == keys.q then
                running = false
                return
            elseif key == keys.r then
                refreshCraftables()
                draw()
            elseif key == keys.up then
                if scroll > 0 then scroll = scroll - 1 end
                draw()
            elseif key == keys.down then
                if scroll < #craftables - pageSize then scroll = scroll + 1 end
                draw()
            elseif key == keys.enter then
                term.setCursorPos(1, pageSize + 4)
                write("Enter ID to craft: ")
                local id = tonumber(read())

                if id and craftables[id] then
                    craftItem(craftables[id])
                else
                    print("Invalid ID!")
                    sleep(1)
                end

                draw()
            end
        end
    end
end

loop()