Advertisement
sidekick_

CCsensors - Inventory Module

Jan 19th, 2013
517
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.30 KB | None | 0 0
  1. --[[
  2.     CCsensors: InventoryModule
  3.     This requires the InventoryModule to be placed inside the Sensor
  4. --]]
  5.  
  6. os.unloadAPI("sensors")
  7. os.loadAPI("/rom/apis/sensors")
  8.  
  9. -- Variables
  10. controllerSide = sensors.getController()
  11. sizeX, sizeY = term.getSize()
  12.  
  13. -- Check to see if there is a controller connected to the computer
  14. if not controllerSide then
  15.     print("No sensor controller found!\nPlease connect a sensor controller.")
  16.     return
  17. end
  18.  
  19. --[[ Functions ]]--
  20.  
  21. -- [[ Other functions ]] --
  22.  
  23. function cp(c, x, y) if c==1 then term.clear() end term.setCursorPos(x,y) end
  24.  
  25. function select( x, y, title, tData )
  26.     local function writeAt(xPos, yPos, text)
  27.         term.setCursorPos(xPos, yPos)
  28.         write(text)
  29.     end
  30.    
  31.     writeAt( x, y, title..": "..#tData )
  32.    
  33.     for i, text in pairs(tData) do
  34.         writeAt( x + 2, y + 1 + i, text )
  35.     end
  36.    
  37.     local sel = 1
  38.     writeAt( x + 1, y + 1 + sel, "*")
  39.     while true do
  40.         _, key = os.pullEvent("key")
  41.         writeAt( x + 1, y + 1 + sel, " ")
  42.         if key == 200 then
  43.             -- Up
  44.             sel = sel - 1
  45.         elseif key == 208 then
  46.             -- Down
  47.             sel = sel + 1
  48.         elseif key == 28 then
  49.             -- Enter
  50.             return tData[sel]
  51.         end
  52.         if sel < 1 then sel = #tData
  53.         elseif sel > #tData then sel = 1 end
  54.        
  55.         writeAt( x + 1, y + 1 + sel, "*")
  56.     end
  57. end
  58. term.clear()
  59. -- Get and Select a sensor
  60. Sensors = sensors.getSensors(controllerSide)
  61. dataSensor = select( 1, 1, "Select the Inventory Sensor", Sensors )
  62.  
  63. dataProbe = "InventoryContent"
  64.  
  65. -- Get and select a single target
  66. Targets = sensors.getAvailableTargetsforProbe( controllerSide, dataSensor, dataProbe )
  67. sTarget = select(1, #Sensors + 4, "Select the chest", Targets)
  68.  
  69. function replace( str, old, new )
  70.     if type( str ) ~= "string" then error( "Bad argument: String expected, got "..type( str ), 2 ) end
  71.     old = tostring( old )
  72.     new = tostring( new )
  73.     local nStr, count = str:gsub( old, new )
  74.     return nStr
  75. end
  76.  
  77. readings = sensors.getSensorReadingAsDict( controllerSide, dataSensor, sTarget, dataProbe)
  78. t_InvenTable = {}
  79. for i, v in pairs(readings) do
  80.     if string.find(v, "tile") or string.find(v, "item") then
  81.         t = string.find(v, "*tile.") and "*tile." or "*item."
  82.         tmpText = string.sub(replace(v, t, " "), 1, -3)
  83.         q = string.sub(tmpText, 1, string.find(tmpText, " ") - 1)
  84.         b = string.sub(tmpText, string.find(tmpText, " ")+1, string.find(tmpText, "@"))
  85.         if string.find(b, "@") then b = replace(b, "@", "") end
  86.         table.insert(t_InvenTable, { quantity = q,  block = b })
  87.     else
  88.         tmpText = string.sub(replace(v, "*", " "), 1, -3)
  89.         q = string.sub(tmpText, 1, string.find(tmpText, " ") - 1)
  90.         b = string.sub(tmpText, string.find(tmpText, " ")+1, string.find(tmpText, "@"))
  91.         if string.find(b, "@") then b = replace(b, "@", "") end
  92.         table.insert(t_InvenTable, { quantity = q,  block = b })
  93.     end
  94. end
  95.  
  96. cp(1, 1, 1) write("Block")
  97. cp(0, 30, 1) write("Quantity")
  98. yPos = 1
  99. sX, sY = term.getSize()
  100. while true do
  101.     for i = 2, 18 do
  102.         cp(0, 1, i) term.clearLine()
  103.     end
  104.     for i = 1, sY do
  105.         if t_InvenTable[yPos + i - 1] then
  106.             cp(0, 2, i+1)
  107.             write(t_InvenTable[yPos + i - 1].block)
  108.             cp(0, 31, i+1)
  109.             write(t_InvenTable[yPos + i - 1].quantity)
  110.         else break end
  111.     end
  112.     _, key = os.pullEvent("key")
  113.     if key == 200 and yPos > 1 then
  114.         -- Up
  115.         yPos = yPos - 1
  116.     elseif key == 208 and yPos <= #t_InvenTable - (sY - 1) then
  117.         -- Down
  118.         yPos = yPos + 1
  119.     end
  120. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement