Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local inventory = peripheral.wrap("right")
- local monitor = peripheral.wrap("top")
- local inventoryTable = nil
- local a, b = monitor.getSize()
- local y = 3
- local sleepTime = 1
- local quit = false
- -- Set the background color to black and the text color to white
- monitor.setBackgroundColor(colors.black)
- monitor.clear()
- function writeTitle()
- monitor.setCursorPos(1, 1)
- monitor.setTextColor(colors.yellow)
- monitor.setBackgroundColor(colors.gray)
- monitor.clearLine()
- monitor.write("Inventory changes:")
- monitor.setBackgroundColor(colors.black)
- end
- function doall()
- local monitorWidth, monitorHeight = monitor.getSize()
- local lineHeight = 1 -- adjust this if your font size is larger than 1
- local maxLines = math.floor((monitorHeight - 2) / lineHeight)
- local lines = {}
- while true do
- local newInventoryTable = {}
- for slot, item in pairs(inventory.list()) do
- -- Replace the "_" characters with spaces
- local itemName = string.gsub(item.name, "_", " ")
- -- Remove everything before the ":" character
- itemName = string.match(itemName, ": *(.*)") or itemName
- newInventoryTable[slot] = {name = itemName, count = item.count}
- end
- -- Compare the new table to the previous table if both are not nil
- if inventoryTable then
- for slot, item in pairs(newInventoryTable) do
- local prevItem = inventoryTable[slot] or {name = "", count = 0}
- if prevItem.name ~= item.name or prevItem.count ~= item.count then
- -- The item has changed, add it to the lines table
- local change = item.count - prevItem.count
- local sign = ""
- if change > 0 then
- sign = "+"
- elseif change < 0 then
- sign = ""
- end
- table.insert(lines, {text = item.name .. " " .. sign .. change, change = change})
- end
- end
- -- Check for items that were removed
- for slot, prevItem in pairs(inventoryTable) do
- if not newInventoryTable[slot] then
- -- The item was removed, add it to the lines table
- table.insert(lines, {text = prevItem.name .. " " .. -prevItem.count, change = -prevItem.count})
- end
- end
- end
- -- Update the previous table
- inventoryTable = newInventoryTable
- -- Write the title
- writeTitle()
- -- Scroll the lines table
- local startLine = math.max(#lines - maxLines + 1, 1)
- local endLine = #lines
- for i = startLine, endLine do
- local line = lines[i]
- monitor.setCursorPos(1, i - startLine + 3)
- monitor.setTextColor(colors.white)
- if line.change > 0 then
- monitor.setTextColor(colors.green)
- elseif line.change < 0 then
- monitor.setTextColor(colors.orange)
- end
- monitor.clearLine()
- -- centerText(monitor, i - startLine + 2, line.text)
- monitor.write(line.text)
- end
- -- Truncate the lines table if it's too long
- while #lines > maxLines do
- table.remove(lines, 1)
- end
- -- Sleep for a bit
- os.sleep(sleepTime)
- end
- end
- inventoryTable = nil
- function getMonitorTouch()
- local event, side, x, y = os.pullEvent("monitor_touch")
- if event then
- quit = true
- end
- end
- while true do
- if quit == true then
- break
- end
- parallel.waitForAny(doall, getMonitorTouch)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement