CreeperNukeBoom

[CC] Battery Monitor

Oct 3rd, 2026 (edited)
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.73 KB | None | 0 0
  1. local function pastebinDownload(id, name, fileName)
  2.     local code, err = http.get(
  3.         "https://pastebin.com/raw/" .. id,
  4.         {["User-Agent"] = "ComputerCraft HTTP API"}
  5.     )
  6.  
  7.     if not code then
  8.         print("Download failed: " .. tostring(err))
  9.         return false
  10.     end
  11.  
  12.     local file = fs.open(fileName, "w")
  13.     file.write(code.readAll())
  14.     file.close()
  15.     code.close()
  16.  
  17.     return true
  18. end
  19.  
  20. local apiDownload = {name = "CGI Core Library", file = "API/CGICoreLib", id = "xuMVS2GP"}
  21.  
  22. if not fs.exists("API/CGICoreLib") then
  23.     print("Installing CGB Core Library...")
  24.     fs.makeDir("API")
  25.     pastebinDownload(
  26.         apiDownload.id,
  27.         apiDownload.name,
  28.         apiDownload.file
  29.     )
  30. end
  31.  
  32. local api = dofile("API/CGICoreLib")
  33.  
  34. api.greeting("Power Storage Monitor V2")
  35.  
  36. while true do
  37.     local monitorsStandard = {}
  38.     local monitorsAdvanced = {}
  39.    
  40.     --gets all monitors then detects standard monitors vs advanced monitors and splits them into their relevant tables
  41.     local monitorsAll = api.cataloguePeripherals("write","setTextScale")
  42.     for _ , name in ipairs(monitorsAll) do
  43.         if peripheral.call(name,"isColor") then
  44.             table.insert(monitorsAdvanced,name)
  45.         else
  46.             table.insert(monitorsStandard,name)
  47.         end
  48.     end
  49.    
  50.     --detects all energy storage, keep in mind that ALL listed methods must match for it to qualify per list
  51.     local batteries1 = api.cataloguePeripherals("getEnergy","getMaxEnergy")
  52.     local batteries2 = api.cataloguePeripherals("getEnergyStored","getMaxEnergyStored")
  53.    
  54.     --adds up all energy
  55.     local energy = 0
  56.     local energyMax = 0
  57.     for _ , name in ipairs(batteries1) do
  58.         energy = energy + peripheral.call(name,"getEnergy")
  59.         energyMax = energyMax + peripheral.call(name,"getMaxEnergy")
  60.     end
  61.    
  62.     for _ , name in ipairs(batteries2) do
  63.         energy = energy + peripheral.call(name,"getEnergyStored")
  64.         energyMax = energyMax + peripheral.call(name,"getMaxEnergyStored")
  65.     end
  66.    
  67.     --creates 2 new strings which simplify the energy readings. Note: as RF
  68.     local energyString = api.simplifyReading("RF",energy,1)
  69.     local energyMaxString = api.simplifyReading("RF",energyMax,1)
  70.    
  71.     local percentage = 0
  72.     if (energy > 0) and (energyMax > 0) then
  73.         percentage = (energy / energyMax) * 100
  74.     end
  75.     local basicText = api.round(percentage,2) .. "%"
  76.    
  77.     for _, name in ipairs(monitorsStandard) do
  78.         peripheral.call(name,"clear")
  79.         peripheral.call(name,"setCursorPos",1,1)
  80.         peripheral.call(name,"write",basicText)
  81.         peripheral.call(name,"setCursorPos",1,2)
  82.         peripheral.call(name,"write",energyString)
  83.         peripheral.call(name,"setCursorPos",1,3)
  84.         peripheral.call(name,"write", "OF:")
  85.         peripheral.call(name,"setCursorPos",1,4)
  86.         peripheral.call(name,"write",energyMaxString)
  87.     end
  88.     sleep(1)
  89. end
  90.  
Advertisement
Add Comment
Please, Sign In to add comment