I know I'm not your parents, but be wary - MineCraft
won't teach you much in the way of real-world skills, don't blow off important stuff for it!
Anyway, best I can make out, "isAddedToEnergyNet()" returns true if the block is plugged into an electric cable leading to other blocks.
Regarding dynamic updating of your MFSU list, the way to go there is to listen for
events - in particular, the ones that fire when adding or removing peripherals, and perhaps a timer event for good measure.
First off, another note about tables: You don't have to use numeric indexes. Instead, you may use strings in the form of "keys":
Code:
local myTable = {}
local myString = "batbox_0"
myTable[myString] = peripheral.wrap(myString)
If you index the peripherals against their own names instead of against numbers, then it becomes very, very easy to check whether or not a given peripheral exists in your table, and to work out WHERE in the table it is... Because its location is simply its name. Given that for the purposes of a conditional, an existing variable counts as "true":
Code:
if myTable["batbox_0"] then print("I have that peripheral!") end
So, with all that in mind, let's say you turned your initial peripheral search into something like this:
Code:
for a,b in pairs(peripheral.getNames()) do
if peripheral.getType(b) == "batbox" then
mfsu[b] = peripheral.wrap(b)
maxEUs = maxEUs + mfsu[b].getCapacity()
end
end
Now let's say you wanted to know the sum total of EU stored in all MFSUs:
Code:
local function getCurrentEUs() -- Note that functions are stored in variables. You must declare them BEFORE you try to call them - including before declarations of other functions that may want to call them!
local result = 0
for a,b in pairs(mfsu) do -- "a" is the indexes ("batbox_0","batbox_1",etc), "b" is the content (the wrapped peripherals).
result = result + b.getStored()
end
return result
end
Meaning that your main program loop can be structured somewhat like this:
Code:
-- Declare variables/functions first,
-- then build your initial table of MFSUs,
-- up here.
-- Note that there's no point in rebuilding the whole table over and over again - that sort of "initialisation" work shouldn't go in your main program loop.
local myTimer = os.startTimer(2) -- Start a two-second timer, store its ID in "myTimer".
while true do
myEvent = {os.pullEvent()} -- Some functions return more then one result. This syntax allows you to stick all such results in a numerically-indexed table.
-- Calling "os.pullEvent()" pauses the program (or rather, causes it to "yield") until an event actually occurs.
if myEvent[1] == "timer" and myEvent[2] == myTimer then -- The event thrown was our timer expiring.
storedEUs = getCurrentEUs() -- Call the function I demonstrated above to get the current amount of EU in the tank(s).
-- Update the screen etc here.
myTimer = os.startTimer(2) -- Set a new timer.
elseif myEvent[1] == "peripheral" and peripheral.getType(myEvent[2]) == "batbox" then -- The event thrown was a new storage unit being added to the system.
mfsu[myEvent[2]] = peripheral.wrap(myEvent[2]) -- Dump it in the table with all the others...
maxEUs = maxEUs + mfsu[myEvent[2]].getCapacity() -- ... and bump up our max capacity figure.
elseif myEvent[1] == "peripheral_detach" and mfsu[myEvent[2]] then -- The event is a peripheral in our table being removed from the system!
maxEUs = maxEUs - mfsu[myEvent[2]].getCapacity() -- Bump the max capacity figure down...
mfsu[myEvent[2]] = nil -- ... and wipe the device from our table.
end
end