A little API I've been messing around with..
Some of you might find this useful. Some of you, I'm sure, will want to rewrite it! I'm not very good at lua.
Usage:
os.loadAPI("openperipheral")
-- wrap all of the mfsus and batboxes on the whole network in one go
local energy = openperipheral:new():types("mfsu", "batbox");
-- sum up the eu capacity of all of them
-- note, in 1.6.2 it's EUCapacity, in 1.5.2 versions it's probably getCapacity or similar
local totalStorage = table.reduce(
energy:getEUCapacity(),
function(a, b )
return a + b
end
)
print("total storage on network = "..totalStorage)
You can also target things individually:
os.loadAPI("openperipheral")
local noteblocks = openperipheral:new():wrap("noteblock_1", "noteblock_2");
noteblocks.triggerNote()
Or append to a previous list
os.loadAPI("openperipheral")
local chests = openperipheral:new():types("chest"):types("trapped_chest"):wrap("barrel_0"):wrap("something_else_0")
chests.doSomething()
Any function call will return all of the results in a lua table.
Name this file "openperipheral"
local sideNames = rs.getSides()
table.reduce = function (list, fn)
local acc
for k, v in ipairs(list) do
if 1 == k then
acc = v
else
acc = fn(acc, v)
end
end
return acc
end
local openperipheral = {
-- wrap multiple targets at once
wrap = function (self, ...)
local meta = getmetatable(self)
for k, name in pairs({...}) do
table.insert(self.names, name)
for k2, methodName in pairs(peripheral.getMethods(name)) do
meta.__index[methodName] = function (a, ...)
local returnVal = {}
for k3, pName in pairs(a.names) do
for k4, mName in pairs(peripheral.getMethods(pName)) do
if mName == methodName then
table.insert(returnVal, peripheral.call(pName, methodName, ...))
end
end
end
return returnVal
end
end
end
return self
end,
-- wrap multiple targets by their type
types = function(self, ...)
local checkTypes = { ... }
local addNames = {}
for k, side in pairs(sideNames) do
local sideType = peripheral.getType(side)
if sideType == "modem" then
for k1, remoteName in pairs(peripheral.call(side, "getNamesRemote")) do
local remoteType = peripheral.call(side, "getTypeRemote", remoteName)
for k2, checkType in pairs(checkTypes) do
if remoteType == checkType then
table.insert(addNames, remoteName)
end
end
end
else
for k2, checkType in pairs(checkTypes) do
if sideType == checkType then
table.insert(addNames, side)
end
end
end
end
return self:wrap(unpack(addNames))
end,
getNames = function(self)
return self.names
end
}
local gmetatable = {
__index = openperipheral,
__tostring = function(g) return g:tostring() end,
}
function new()
local g = {
names = {}
}
setmetatable(g, gmetatable)
return g
end