-- -- bridge.lua -- -- Capability-probed adapter for an Advanced Peripherals ME/RS storage bridge. -- A bridge is identified by the METHODS it exposes, not a type string (packs -- differ: "meBridge" vs "me_bridge" vs namespaced), and this adapts to whichever -- API generation it has (listItems/exportItemToPeripheral OR getItems/exportItem). -- -- local bridge = require("bridge") -- local b = bridge.wrap("me_bridge_0") -- nil if not a storage bridge -- for _, it in ipairs(b.list()) do ... end -- name/displayName/amount/fingerprint -- local moved, err = b.export({fingerprint=fp, count=1}, "minecraft:barrel_0") -- local moved, err = b.import({name="...", count=1}, "minecraft:barrel_0") -- local function wrapBridge(name) if not (name and peripheral.isPresent(name)) then return nil end local methods = peripheral.getMethods(name) if not methods then return nil end local has = {} for _, mth in ipairs(methods) do has[mth] = true end local listFn = has.listItems and "listItems" or (has.getItems and "getItems" or nil) local exportFn = has.exportItemToPeripheral and "exportItemToPeripheral" or (has.exportItem and "exportItem" or nil) if not (listFn and exportFn) then return nil end -- not a storage bridge local importFn = has.importItemFromPeripheral and "importItemFromPeripheral" or (has.importItem and "importItem" or nil) local p = peripheral.wrap(name) local function list() local ok, items if listFn == "listItems" then ok, items = pcall(p.listItems) else ok, items = pcall(p.getItems, {}) end -- empty filter = everything if not ok or type(items) ~= "table" then return {} end return items end -- both API generations take (filter, peripheralName); older returns a plain -- moved-count, newer may return a result table local function normalizeMoved(ok, result) if not ok then return 0, tostring(result) end if type(result) == "table" then return result.count or result.amount or 0 end if type(result) == "number" then return result end return 0 end local function export(filter, dest) return normalizeMoved(pcall(p[exportFn], filter, dest)) end local function import(filter, src) if not importFn then return 0, "no import method on this bridge" end return normalizeMoved(pcall(p[importFn], filter, src)) end return { name = name, list = list, export = export, import = import } end return { wrap = wrapBridge }