Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- File paths and ME Bridge setup
- local thresholdFile = "thresholds.txt"
- local meBridge = peripheral.wrap("front") -- Change to the actual side of the ME Bridge
- local editing = false
- -- Load thresholds from file
- local function loadThresholds()
- local thresholds = {}
- if not fs.exists(thresholdFile) then
- print("Threshold file not found. Creating a new one.")
- local file = fs.open(thresholdFile, "w")
- file.close()
- return thresholds
- end
- local file = fs.open(thresholdFile, "r")
- while true do
- local line = file.readLine()
- if not line then break end
- local name, threshold = line:match("(.+)=(%d+)")
- if name and threshold then
- thresholds[name] = tonumber(threshold)
- end
- end
- file.close()
- return thresholds
- end
- -- Save thresholds to file
- local function saveThresholds(thresholds)
- local file = fs.open(thresholdFile, "w")
- for name, threshold in pairs(thresholds) do
- file.writeLine(name .. "=" .. threshold)
- end
- file.close()
- end
- -- Set a new threshold for an item
- local function setThreshold(name, threshold)
- local thresholds = loadThresholds()
- thresholds[name] = threshold
- saveThresholds(thresholds)
- end
- -- Prompt user to set a threshold based on item in turtle slot
- local function promptThreshold()
- -- Check if there's an item in the first slot
- local item = turtle.getItemDetail(1)
- if item then
- local itemName = item.name
- print("Detected item:", itemName)
- print("Enter the threshold for this item:")
- local threshold = tonumber(read())
- term.clear()
- term.setCursorPos(1, 1)
- if threshold then
- setThreshold(itemName, threshold)
- print("Threshold for", itemName, "set to", threshold)
- else
- print("Invalid input. Please enter a number.")
- end
- term.setTextColor(colors.green)
- textutils.slowPrint("Success!", 4)
- term.setTextColor(colors.white)
- sleep(1)
- term.clear()
- else
- term.clear()
- term.setCursorPos(1, 1)
- print("Place item in slot 1 and hit enter.")
- end
- end
- -- Open thresholds.txt in edit mode
- local function editThresholdsFile()
- editing = true
- term.clear()
- term.setCursorPos(1, 1)
- shell.run("edit", thresholdFile) -- Open the editor
- editing = false
- end
- -- Check inventory and craft items if below threshold
- local function checkAndCraft()
- local thresholds = loadThresholds()
- local items = meBridge.listItems()
- for name, threshold in pairs(thresholds) do
- local item = meBridge.getItem({name = name})
- local count = item and item.amount or 0
- if count < threshold then
- local crafting, text = meBridge.isItemCrafting({name = name})
- if crafting == false then
- term.setTextColor(colors.yellow)
- print(">".. name.. "<")
- local success = meBridge.craftItem({name = name, count = threshold - count})
- if success then
- term.setTextColor(colors.green)
- print("Success.")
- term.setTextColor(colors.white)
- else
- term.setTextColor(colors.red)
- print("Fail:", name)
- term.setTextColor(colors.white)
- end
- end
- end
- end
- end
- -- Main loop with parallel execution for key detection and crafting
- local function handleKeyPress()
- while true do
- local event, key = os.pullEvent("key")
- if key == keys.space then
- editThresholdsFile() -- Open the editor
- elseif key == keys.enter then
- promptThreshold() -- Prompt user to set threshold based on turtle slot 1
- end
- sleep(0.1) -- Short sleep to prevent overwhelming the event loop
- end
- end
- local function runCraftingCheck()
- while true do
- if editing == false then
- checkAndCraft() -- Continuously check if any items need to be crafted
- end
- sleep(1) -- Adjust sleep time as needed
- end
- end
- -- Run both functions in parallel
- parallel.waitForAny(handleKeyPress, runCraftingCheck)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement