Advertisement
TheHeodon

Computercraft Thermal Expansion Api

Apr 26th, 2017
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.52 KB | None | 0 0
  1. -- Created by TheHeodon
  2. -- youtube.com/user/TheHeodon
  3. -- If you're using this please give credit.
  4.  
  5. -- Creating empty list to store connected devices
  6. -- later.
  7.  
  8. list={}
  9.  
  10. -- Function for clearing all values in table.
  11.  
  12. function clearTable()
  13.     for k in pairs(list) do
  14.         list[k]=nil
  15.     end
  16.     print("::Table successfully cleared.")
  17. end
  18.  
  19. -- Function for detecting all devices in
  20. -- a network with a specific DeviceName.
  21.  
  22. function getConnectedDevices(DeviceName)
  23.     local i=0
  24.     repeat
  25.         device=peripheral.isPresent(DeviceName.."_"..i)
  26.         if device then
  27.             table.insert(list,peripheral.wrap(DeviceName.."_"..i))
  28.         end
  29.         i=i+1
  30.     until not device
  31.     print("::Found "..#list.." devices of name "..DeviceName.." connected.")
  32. end
  33.  
  34. -- Function for formatting numbers to add a sufix
  35. -- and calculate the new number.
  36.  
  37. function formatNumber(Number)
  38.     output=" "
  39.     if (Number >= 0) and (Number < 1000) then
  40.         output="< 1K"
  41.     elseif (Number >= 1000) and (Number < 1000000) then
  42.         output=tostring(math.floor(Number/1000)).."K"
  43.     elseif (Number >= 1000000) and (Number < 1000000000) then
  44.         output=tostring(math.floor(Number/1000000)).."M"
  45.     elseif (Number >= 1000000000) then
  46.         output=tostring(math.floor(Number/1000000000)).."B"
  47.     end
  48.     return(output)
  49. end
  50.  
  51. -- Clearing the table, console and getting all
  52. -- connected thermal expansion energy cells.
  53.  
  54. term.clear()
  55. clearTable()
  56. getConnectedDevices("cofh_thermalexpansion_energycell")
  57.  
  58. -- Creating variables that are going to be reset
  59. -- with while loop so they don't add up forever.
  60.  
  61. totalEnergy=0
  62. totalMaxEnergy=0
  63. energyPercent=0
  64.  
  65. -- Getting total energy stored and total max energy
  66. -- stored in all energy cells combined.
  67.  
  68. for i, energyCell in ipairs(list) do
  69.     if energyCell then
  70.         local energyStored=energyCell.getEnergyStored("unknown")
  71.         local maxEnergyStored=energyCell.getMaxEnergyStored("unknown")
  72.         totalEnergy=math.floor(totalEnergy+energyStored)
  73.         totalMaxEnergy=math.floor(totalMaxEnergy+maxEnergyStored)
  74.     else
  75.         print("::ERROR - energyCell at index "..i.." is nil")
  76.     end
  77. end
  78.  
  79. -- Calculating energy percent
  80.  
  81. energyPercent=math.floor((totalEnergy/totalMaxEnergy)*100)
  82.  
  83. -- Formatting total energy and total max energy with
  84. -- formatNumber function from before.
  85.  
  86. totalEnergyFormatted=formatNumber(totalEnergy)
  87. totalMaxEnergyFormatted=formatNumber(totalMaxEnergy)
  88.  
  89. -- Debug printing in console
  90.  
  91. print(" ")
  92. print("::Total Energy Stored: "..totalEnergyFormatted)
  93. print("::Max Total Energy Stored: "..totalMaxEnergyFormatted)
  94. print("::Energy Percent: "..energyPercent.."%")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement