Advertisement
Sanwiches

OpenC: findComponents

Jan 21st, 2015
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 KB | None | 0 0
  1. -- This is written for OpenComputers
  2.  
  3. -- Returns a table of method proxies for each component of type
  4. -- Type can be abbreviated, since component.proxy supports that
  5. -- If type is empty, it iterates over ALL components
  6.  
  7. local component = require("component")
  8. local function findComps(type)
  9. local comps = {}
  10. local compNames = {}
  11.  
  12. compList = component.list(type)
  13. for cAddress,cType in compList do
  14. -- Insert proxy methods table into comps table
  15. table.insert(comps, component.proxy(cAddress))
  16. -- Insert component type into compNames table
  17. table.insert(compNames, cType)
  18. end
  19. return comps, compNames
  20. end
  21.  
  22. -- Usage example:
  23. local cellTable = findComps("tile_thermalexpansion_cell_resonant_name")
  24. local firstCellEnergy = cellTable[1].getMaxEnergyStored()
  25. print(firstCellEnergy) -- Prints "50000000", since resonant energy cells have a max storage of 50,000,000
  26.  
  27. -- Abbreviated usage:
  28. print(findComps("tile_thermalexpansion_cell_resonant_name")[1].getMaxEnergyStored())
  29.  
  30. -- Print type of each component
  31. local comps, compNames = findComps()
  32. for i=1, #compNames do
  33. print(compNames[i])
  34. end
  35.  
  36. -- Print methods available for each component
  37. local comps, compNames = findComps()
  38. for i=1, #comps do -- Iterate components
  39. for k,v in pairs(comps[i]) do -- Iterate methods
  40. print(k) -- Print method name
  41. end
  42. end
  43.  
  44. -- Print max value for each cell it finds
  45. -- Note that this abbreviates the type, so it finds all energy cells of all types (leadstone, hardened, redstone, resonant, etc)
  46. local cellTable = findComps("tile_thermalexpansion_cell")
  47. for i=1, #cellTable do
  48. print(cellTable[i].getMaxEnergyStored())
  49. end
  50.  
  51. -- Print current energy percent for each cell it finds
  52. local cellTable = findComps("tile_thermalexpansion_cell")
  53. for i=1, #cellTable do
  54. local prcnt = math.floor(cellTable[i].getEnergyStored()/cellTable[i].getMaxEnergyStored()*100)
  55. print(prcnt.."%")
  56. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement