Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Created by TheHeodon
- -- youtube.com/user/TheHeodon
- -- If you're using this please give credit.
- -- Creating empty list to store connected devices
- -- later.
- list={}
- -- Function for clearing all values in table.
- function clearTable()
- for k in pairs(list) do
- list[k]=nil
- end
- print("::Table successfully cleared.")
- end
- -- Function for detecting all devices in
- -- a network with a specific DeviceName.
- function getConnectedDevices(DeviceName)
- local i=0
- repeat
- device=peripheral.isPresent(DeviceName.."_"..i)
- if device then
- table.insert(list,peripheral.wrap(DeviceName.."_"..i))
- end
- i=i+1
- until not device
- print("::Found "..#list.." devices of name "..DeviceName.." connected.")
- end
- -- Function for formatting numbers to add a sufix
- -- and calculate the new number.
- function formatNumber(Number)
- output=" "
- if (Number >= 0) and (Number < 1000) then
- output="< 1K"
- elseif (Number >= 1000) and (Number < 1000000) then
- output=tostring(math.floor(Number/1000)).."K"
- elseif (Number >= 1000000) and (Number < 1000000000) then
- output=tostring(math.floor(Number/1000000)).."M"
- elseif (Number >= 1000000000) then
- output=tostring(math.floor(Number/1000000000)).."B"
- end
- return(output)
- end
- -- Clearing the table, console and getting all
- -- connected thermal expansion energy cells.
- term.clear()
- clearTable()
- getConnectedDevices("cofh_thermalexpansion_energycell")
- -- Creating variables that are going to be reset
- -- with while loop so they don't add up forever.
- totalEnergy=0
- totalMaxEnergy=0
- energyPercent=0
- -- Getting total energy stored and total max energy
- -- stored in all energy cells combined.
- for i, energyCell in ipairs(list) do
- if energyCell then
- local energyStored=energyCell.getEnergyStored("unknown")
- local maxEnergyStored=energyCell.getMaxEnergyStored("unknown")
- totalEnergy=math.floor(totalEnergy+energyStored)
- totalMaxEnergy=math.floor(totalMaxEnergy+maxEnergyStored)
- else
- print("::ERROR - energyCell at index "..i.." is nil")
- end
- end
- -- Calculating energy percent
- energyPercent=math.floor((totalEnergy/totalMaxEnergy)*100)
- -- Formatting total energy and total max energy with
- -- formatNumber function from before.
- totalEnergyFormatted=formatNumber(totalEnergy)
- totalMaxEnergyFormatted=formatNumber(totalMaxEnergy)
- -- Debug printing in console
- print(" ")
- print("::Total Energy Stored: "..totalEnergyFormatted)
- print("::Max Total Energy Stored: "..totalMaxEnergyFormatted)
- print("::Energy Percent: "..energyPercent.."%")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement