-- -- me.lua -- -- Reusable ME-network subsystem logic on an Advanced Peripherals ME Bridge. -- No UI, no comms - collect() returns an aggregate snapshot (power, storage, -- crafting CPUs) and there are a couple of on-demand item ops. -- -- local me = require("me").new(cfg) -- local st = me.collect() -- local ok, info = me.get_item("minecraft:iron_ingot") -- -- Aggregate only: listItems() can be thousands of entries, far too big to push -- over rednet every couple seconds, so item lookups are per-request commands. -- -- cfg fields used: me_bridge (side/name, optional - auto-detected), bridge_type. -- local me = {} function me.new(cfg) cfg = cfg or {} local BRIDGE_TYPE = cfg.bridge_type or "meBridge" local name = cfg.me_bridge if not (name and peripheral.isPresent(name) and peripheral.getType(name) == BRIDGE_TYPE) then name = nil for _, n in ipairs(peripheral.getNames()) do if peripheral.getType(n) == BRIDGE_TYPE then name = n; break end end end if not name then error("me: no ME Bridge ('" .. BRIDGE_TYPE .. "') found. Place one and attach it to this computer.", 0) end local b = peripheral.wrap(name) -- ME Bridge calls return (value, err) and can throw; guard each one. local function num(fn) local ok, v = pcall(fn) return (ok and type(v) == "number") and v or 0 end local function collect() local stored = num(b.getEnergyStorage) local emax = num(b.getMaxEnergyStorage) local usage = num(b.getEnergyUsage) local itot = num(b.getTotalItemStorage) local iused = num(b.getUsedItemStorage) local iavail = num(b.getAvailableItemStorage) local ftot = num(b.getTotalFluidStorage) local fused = num(b.getUsedFluidStorage) local favail = num(b.getAvailableFluidStorage) local cpu_total, cpu_busy = 0, 0 local ok, cpus = pcall(b.getCraftingCPUs) if ok and type(cpus) == "table" then cpu_total = #cpus for _, c in ipairs(cpus) do if c.isBusy then cpu_busy = cpu_busy + 1 end end end local epct = emax > 0 and math.floor((stored / emax) * 100) or 0 local ipct = itot > 0 and math.floor((iused / itot) * 100) or 0 return { headline = string.format("%d%% cells, %d/%d CPU busy", ipct, cpu_busy, cpu_total), power = { stored = stored, max = emax, usage = usage, pct = epct }, items = { total = itot, used = iused, available = iavail, pct = ipct }, fluids = { total = ftot, used = fused, available = favail }, cpus = { total = cpu_total, busy = cpu_busy }, } end -- On-demand: how much of an item is stored + whether it's craftable. local function getItem(item_name) if not item_name then return false, "no item name" end local ok, info = pcall(b.getItem, { name = item_name }) if not ok or type(info) ~= "table" then return false, "not found" end return true, { name = info.name, displayName = info.displayName, amount = info.amount or 0, craftable = info.isCraftable and true or false, } end -- On-demand: kick off an autocraft job. local function craftItem(item_name, count) if not item_name then return false, "no item name" end local ok, started = pcall(b.craftItem, { name = item_name, count = count or 1 }) if not ok then return false, tostring(started) end if not started then return false, "could not start (missing pattern or materials?)" end return true, { name = item_name, count = count or 1 } end return { bridge = name, collect = collect, get_item = getItem, craft_item = craftItem, } end return me