Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- CCsensors: InventoryModule
- This requires the InventoryModule to be placed inside the Sensor
- --]]
- os.unloadAPI("sensors")
- os.loadAPI("/rom/apis/sensors")
- -- Variables
- controllerSide = sensors.getController()
- sizeX, sizeY = term.getSize()
- -- Check to see if there is a controller connected to the computer
- if not controllerSide then
- print("No sensor controller found!\nPlease connect a sensor controller.")
- return
- end
- --[[ Functions ]]--
- -- [[ Other functions ]] --
- function cp(c, x, y) if c==1 then term.clear() end term.setCursorPos(x,y) end
- function select( x, y, title, tData )
- local function writeAt(xPos, yPos, text)
- term.setCursorPos(xPos, yPos)
- write(text)
- end
- writeAt( x, y, title..": "..#tData )
- for i, text in pairs(tData) do
- writeAt( x + 2, y + 1 + i, text )
- end
- local sel = 1
- writeAt( x + 1, y + 1 + sel, "*")
- while true do
- _, key = os.pullEvent("key")
- writeAt( x + 1, y + 1 + sel, " ")
- if key == 200 then
- -- Up
- sel = sel - 1
- elseif key == 208 then
- -- Down
- sel = sel + 1
- elseif key == 28 then
- -- Enter
- return tData[sel]
- end
- if sel < 1 then sel = #tData
- elseif sel > #tData then sel = 1 end
- writeAt( x + 1, y + 1 + sel, "*")
- end
- end
- term.clear()
- -- Get and Select a sensor
- Sensors = sensors.getSensors(controllerSide)
- dataSensor = select( 1, 1, "Select the Inventory Sensor", Sensors )
- dataProbe = "InventoryContent"
- -- Get and select a single target
- Targets = sensors.getAvailableTargetsforProbe( controllerSide, dataSensor, dataProbe )
- sTarget = select(1, #Sensors + 4, "Select the chest", Targets)
- function replace( str, old, new )
- if type( str ) ~= "string" then error( "Bad argument: String expected, got "..type( str ), 2 ) end
- old = tostring( old )
- new = tostring( new )
- local nStr, count = str:gsub( old, new )
- return nStr
- end
- readings = sensors.getSensorReadingAsDict( controllerSide, dataSensor, sTarget, dataProbe)
- t_InvenTable = {}
- for i, v in pairs(readings) do
- if string.find(v, "tile") or string.find(v, "item") then
- t = string.find(v, "*tile.") and "*tile." or "*item."
- tmpText = string.sub(replace(v, t, " "), 1, -3)
- q = string.sub(tmpText, 1, string.find(tmpText, " ") - 1)
- b = string.sub(tmpText, string.find(tmpText, " ")+1, string.find(tmpText, "@"))
- if string.find(b, "@") then b = replace(b, "@", "") end
- table.insert(t_InvenTable, { quantity = q, block = b })
- else
- tmpText = string.sub(replace(v, "*", " "), 1, -3)
- q = string.sub(tmpText, 1, string.find(tmpText, " ") - 1)
- b = string.sub(tmpText, string.find(tmpText, " ")+1, string.find(tmpText, "@"))
- if string.find(b, "@") then b = replace(b, "@", "") end
- table.insert(t_InvenTable, { quantity = q, block = b })
- end
- end
- cp(1, 1, 1) write("Block")
- cp(0, 30, 1) write("Quantity")
- yPos = 1
- sX, sY = term.getSize()
- while true do
- for i = 2, 18 do
- cp(0, 1, i) term.clearLine()
- end
- for i = 1, sY do
- if t_InvenTable[yPos + i - 1] then
- cp(0, 2, i+1)
- write(t_InvenTable[yPos + i - 1].block)
- cp(0, 31, i+1)
- write(t_InvenTable[yPos + i - 1].quantity)
- else break end
- end
- _, key = os.pullEvent("key")
- if key == 200 and yPos > 1 then
- -- Up
- yPos = yPos - 1
- elseif key == 208 and yPos <= #t_InvenTable - (sY - 1) then
- -- Down
- yPos = yPos + 1
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement