Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Path to store current task ID on the computer
- local TASK_ID_PATH = "/task_id.txt"
- local DISK_NAME = "Incoming Task"
- local strItem = ""
- local numItems = 0
- local limitStack = 64
- local function transportFirstType()
- local chestFront = peripheral.wrap("front")
- local chestBack = peripheral.wrap("back")
- local firstItemName = ""
- local cnt = 1
- while cnt <= 54 do
- local item = chestFront.getItemDetail(cnt)
- if item then
- firstItemName = item.name
- break
- end
- cnt = cnt + 1
- end
- if string.len(firstItemName) > 0 then
- local cnt = 1
- local cnt2 = 0
- strItem = firstItemName
- numItems = 0
- while cnt <= 54 do
- local item = chestFront.getItemDetail(cnt)
- if item then
- if firstItemName == item.name then
- numItems = numItems + item.count
- limitStack = chestFront.getItemLimit(cnt)
- chestBack.pullItems("front", cnt)
- cnt2 = cnt2 + 1
- end
- if cnt2 >= 16 then
- break
- end
- end
- cnt = cnt + 1
- end
- end
- end
- local function chestEmpty(chest)
- for slot, item in pairs(chest.list()) do
- return false
- end
- return true
- end
- -- Load the current task ID from computer (or initialize to 1)
- local function loadTaskId()
- if fs.exists(TASK_ID_PATH) then
- local file = fs.open(TASK_ID_PATH, "r")
- local id = tonumber(file.readLine())
- file.close()
- return id or 1
- else
- return 1
- end
- end
- -- Save the current task ID to the computer
- local function saveTaskId(id)
- local file = fs.open(TASK_ID_PATH, "w")
- file.writeLine(tostring(id))
- file.close()
- end
- -- Save a task (itemName, itemCount, stackLimit) to floppy disk
- function saveTaskToDisk(itemName, itemCount, stackLimit)
- local disk = peripheral.wrap("bottom")
- local diskPath = disk.getMountPath()
- if not (disk.getDiskLabel() == DISK_NAME) then
- disk.setDiskLabel(DISK_NAME)
- end
- if not diskPath then
- error("No disk detected under the computer!")
- end
- local tasksPath = fs.combine(diskPath, "tasks")
- if not fs.exists(tasksPath) then
- fs.makeDir(tasksPath)
- end
- local taskId = loadTaskId()
- local filename = fs.combine(tasksPath, tostring(taskId) .. ".txt")
- local file = fs.open(filename, "w")
- file.writeLine("item=" .. itemName)
- file.writeLine("count=" .. tostring(itemCount))
- file.writeLine("stack_limit=" .. tostring(stackLimit))
- file.close()
- print("item=" .. itemName)
- print("count=" .. tostring(itemCount))
- print("stack_limit=" .. tostring(stackLimit))
- print("")
- -- Update the ID
- saveTaskId(taskId + 1)
- end
- -- Check if there are no task files currently on the disk (i.e., the slot is free)
- function isTaskSlotFree()
- local disk = peripheral.wrap("bottom")
- local diskPath = disk.getMountPath()
- if not diskPath then
- error("No disk detected under the computer!")
- end
- local tasksPath = fs.combine(diskPath, "tasks")
- if not fs.exists(tasksPath) then
- -- No tasks folder means no tasks
- return true
- end
- local files = fs.list(tasksPath)
- return #files == 0
- end
- while true do
- if isTaskSlotFree() and chestEmpty(peripheral.wrap("back")) then
- transportFirstType()
- saveTaskToDisk(strItem, numItems, limitStack)
- else
- sleep(0.2)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement