Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Initialize global variables
- local thresholdFile = "thresholds.txt"
- local meBridge = peripheral.wrap("top") -- Change to the actual side of the ME Bridge
- local monitor = peripheral.find("monitor") -- Adjust to the correct monitor
- local editing = false
- local sortBy = "name" -- Default sort by name
- local thresholds = {}
- -- Set monitor properties
- local monX, monY = monitor.getSize()
- local maxRows = monY - 2 -- Adjust based on row size
- local scrollPosition = 1 -- Tracks the first visible row
- monitor.setTextScale(0.5)
- monitor.clear()
- ---get API
- if fs.exists("API") == false then
- shell.run("pastebin", "get", "EzkfU5ZM", "API")
- end
- shaka = require("API")
- -- Load thresholds from file
- local function loadThresholds()
- local file = fs.open(thresholdFile, "r")
- local thresholds = {}
- if file then
- for line in file.readLine do
- local name, threshold = line:match("(.+)=(%d+)")
- if name and threshold then
- thresholds[name] = tonumber(threshold)
- end
- end
- file.close()
- end
- return thresholds
- end
- -- Save thresholds to file
- local function saveThresholds()
- local file = fs.open(thresholdFile, "w")
- for name, threshold in pairs(thresholds) do
- file.writeLine(name .. "=" .. threshold)
- end
- file.close()
- end
- -- Update threshold for a given item
- local function setThreshold(name, value)
- thresholds[name] = value
- saveThresholds()
- end
- -- Sort items by the selected criterion (name, count, or threshold)
- local function sortItems()
- local items = {}
- for name, threshold in pairs(thresholds) do
- local item = meBridge.getItem({name = name}) or {}
- table.insert(items, {name = name, count = item.amount or 0, threshold = threshold})
- end
- table.sort(items, function(a, b)
- a.name = shaka.prettyName(a.name)
- b.name = shaka.prettyName(b.name)
- if sortBy == "name" then
- return a.name < b.name
- elseif sortBy == "count" then
- return a.count > b.count
- elseif sortBy == "threshold" then
- return a.threshold > b.threshold
- end
- end)
- return items
- end
- -- Function to check crafting status for all items in parallel
- local function checkCraftingStatus(items)
- local craftingStatus = {} -- Table to store the crafting status for each item
- local function checkStatusForItem(item)
- local crafting = meBridge.isItemCrafting({name = item.name}) or false
- craftingStatus[item.name] = crafting
- end
- -- Run the crafting checks in parallel for all items
- local parallelTasks = {}
- for _, item in ipairs(items) do
- table.insert(parallelTasks, function()
- checkStatusForItem(item)
- end)
- end
- -- Execute all tasks in parallel
- parallel.waitForAll(table.unpack(parallelTasks))
- return craftingStatus
- end
- -- Function to display item information with parallel rendering of rows
- local function displayMonitorInfoParallel(items)
- local craftingStatus = checkCraftingStatus(items) -- Get crafting statuses in parallel
- -- Clear the monitor before displaying updated content
- monitor.setBackgroundColor(colors.green)
- monitor.setTextColor(colors.black)
- monitor.setCursorPos(1, 1)
- monitor.clear()
- monitor.write("ITEM STATUS (Tap to Sort by " .. (sortBy == "name" and "Count" or "Name") .. ")")
- -- Display headers for item information
- monitor.setCursorPos(1, 2)
- monitor.setBackgroundColor(colors.gray)
- monitor.setTextColor(colors.black)
- monitor.clearLine()
- monitor.write(string.format("%-22s %-8s %-10s %-9s", "Item Name", "Count", "Threshold", "Crafting"))
- -- Row number starts from 3 as headers take the first two lines
- local row = 3
- local totalItems = math.min(#items, scrollPosition + maxRows - 1)
- -- Parallel task creation for each row in the display
- local parallelTasks = {}
- for i = scrollPosition, totalItems do
- table.insert(parallelTasks, function()
- local item = items[i]
- local crafting = craftingStatus[item.name] or false -- Get the crafting status from the parallel check
- -- Debugging: Print crafting status only for items being crafted
- if crafting then
- print(string.format("Item: %s, Crafting: Yes", item.name))
- end
- -- Set row color alternately for readability
- monitor.setCursorPos(1, row)
- monitor.setBackgroundColor(row % 2 == 0 and colors.gray or colors.black)
- monitor.clearLine()
- monitor.setTextColor(colors.white)
- monitor.write(string.format("%-23s", item.name))
- -- Display count with color depending on threshold
- monitor.setTextColor(item.count < item.threshold and colors.red or colors.green)
- monitor.write(string.format("%-9d", item.count))
- -- Display threshold
- monitor.setTextColor(colors.white)
- monitor.write(string.format("%-11d", item.threshold))
- -- Crafting status: Set to orange if crafting, otherwise light gray
- if crafting then
- monitor.setTextColor(colors.orange)
- monitor.write("Yes")
- else
- monitor.setTextColor(colors.lightGray)
- monitor.write("No")
- end
- row = row + 1 -- Move to the next row for the next item
- end)
- end
- -- Run all tasks in parallel
- parallel.waitForAll(table.unpack(parallelTasks))
- -- Display the last update time at the bottom
- monitor.setCursorPos(1, row + 1)
- monitor.setBackgroundColor(colors.black)
- monitor.setTextColor(colors.lightGray)
- monitor.clearLine()
- monitor.write("Last update: " .. textutils.formatTime(os.time(), true))
- end
- -- Handle touch input for sorting and scrolling
- local function monitorTouchHandler(items)
- while true do
- local event, side, x, y = os.pullEvent("monitor_touch")
- if y >= 2 and y < monY / 2 then -- Scroll up
- if scrollPosition > 1 then
- scrollPosition = scrollPosition - 1
- displayMonitorInfoParallel(items)
- end
- elseif y >= monY / 2 then -- Scroll down
- if scrollPosition + maxRows - 1 < #items then
- scrollPosition = scrollPosition + 1
- displayMonitorInfoParallel(items)
- end
- elseif y == 1 then -- Toggle sorting
- sortBy = (sortBy == "name") and "count" or "name"
- displayMonitorInfoParallel(sortItems())
- end
- end
- end
- -- Craft items if their count is below threshold
- local function checkAndCraft()
- local items = meBridge.listItems()
- for name, threshold in pairs(thresholds) do
- local item = meBridge.getItem({name = name}) or {}
- local count = item.amount or 0
- if count < threshold then
- local crafting = meBridge.isItemCrafting({name = name}) or false
- if not crafting then
- print("Crafting " .. name .. "...")
- local success = meBridge.craftItem({name = name, count = threshold - count})
- print(success and "Crafted successfully" or "Crafting failed")
- end
- end
- end
- end
- -- Handle user input for threshold management
- local function promptThreshold()
- editing = true
- local item = turtle.getItemDetail(1)
- if item then
- local name = item.name
- print("Set threshold for " .. name .. ":")
- local threshold = tonumber(read())
- if threshold then
- setThreshold(name, threshold)
- print("Threshold for " .. name .. " set to " .. threshold)
- else
- print("Invalid input")
- end
- else
- print("Place item in slot 1")
- end
- editing = false
- end
- -- Main loop
- local function main()
- thresholds = loadThresholds() -- Load thresholds once at the start
- local items = sortItems() -- Sort items based on the default criterion
- parallel.waitForAny(
- function() -- Handle keypresses for manual editing
- while true do
- local event, key = os.pullEvent("key")
- if key == keys.space then
- editThresholdsFile()
- elseif key == keys.enter then
- promptThreshold()
- end
- sleep(0.1)
- end
- end,
- function() -- Monitor touch for interaction
- monitorTouchHandler(items)
- end,
- function() -- Run crafting checks and monitor updates
- while true do
- checkAndCraft()
- items = sortItems()
- displayMonitorInfoParallel(items)
- sleep(1) -- Refresh every second (adjust as needed)
- end
- end
- )
- end
- -- Run the main loop
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement