Advertisement
Blackhome

Filter

Apr 7th, 2025 (edited)
340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.63 KB | Gaming | 0 0
  1. -- Path to store current task ID on the computer
  2. local TASK_ID_PATH = "/task_id.txt"
  3. local DISK_NAME = "Incoming Task"
  4.  
  5. local strItem = ""
  6. local numItems = 0
  7. local limitStack = 64
  8.  
  9. local function transportFirstType()
  10.     local chestFront = peripheral.wrap("front")
  11.     local chestBack = peripheral.wrap("back")
  12.  
  13.     local firstItemName = ""
  14.  
  15.     local cnt = 1
  16.     while cnt <= 54 do
  17.         local item = chestFront.getItemDetail(cnt)
  18.         if item then
  19.             firstItemName = item.name
  20.             break
  21.         end
  22.         cnt = cnt + 1
  23.     end
  24.     if string.len(firstItemName) > 0 then
  25.         local cnt = 1
  26.         local cnt2 = 0
  27.        
  28.         strItem = firstItemName
  29.         numItems = 0
  30.  
  31.         while cnt <= 54 do
  32.             local item = chestFront.getItemDetail(cnt)
  33.             if item then
  34.                 if firstItemName == item.name then
  35.                     numItems = numItems + item.count
  36.                     limitStack = chestFront.getItemLimit(cnt)
  37.                     chestBack.pullItems("front", cnt)
  38.                     cnt2 = cnt2 + 1
  39.                 end
  40.                 if cnt2 >= 16 then
  41.                     break
  42.                 end
  43.             end
  44.             cnt = cnt + 1
  45.         end
  46.     end
  47. end
  48.  
  49. local function chestEmpty(chest)
  50.     for slot, item in pairs(chest.list()) do
  51.         return false
  52.     end
  53.     return true
  54. end
  55.  
  56. -- Load the current task ID from computer (or initialize to 1)
  57. local function loadTaskId()
  58.     if fs.exists(TASK_ID_PATH) then
  59.         local file = fs.open(TASK_ID_PATH, "r")
  60.         local id = tonumber(file.readLine())
  61.         file.close()
  62.         return id or 1
  63.     else
  64.         return 1
  65.     end
  66. end
  67.  
  68. -- Save the current task ID to the computer
  69. local function saveTaskId(id)
  70.     local file = fs.open(TASK_ID_PATH, "w")
  71.     file.writeLine(tostring(id))
  72.     file.close()
  73. end
  74.  
  75. -- Save a task (itemName, itemCount, stackLimit) to floppy disk
  76. function saveTaskToDisk(itemName, itemCount, stackLimit)
  77.     local disk = peripheral.wrap("bottom")
  78.     local diskPath = disk.getMountPath()
  79.  
  80.     if not (disk.getDiskLabel() == DISK_NAME) then
  81.         disk.setDiskLabel(DISK_NAME)
  82.     end
  83.  
  84.     if not diskPath then
  85.         error("No disk detected under the computer!")
  86.     end
  87.  
  88.     local tasksPath = fs.combine(diskPath, "tasks")
  89.     if not fs.exists(tasksPath) then
  90.         fs.makeDir(tasksPath)
  91.     end
  92.  
  93.     local taskId = loadTaskId()
  94.     local filename = fs.combine(tasksPath, tostring(taskId) .. ".txt")
  95.    
  96.     local file = fs.open(filename, "w")
  97.     file.writeLine("item=" .. itemName)
  98.     file.writeLine("count=" .. tostring(itemCount))
  99.     file.writeLine("stack_limit=" .. tostring(stackLimit))
  100.     file.close()
  101.  
  102.     print("item=" .. itemName)
  103.     print("count=" .. tostring(itemCount))
  104.     print("stack_limit=" .. tostring(stackLimit))
  105.     print("")
  106.  
  107.     -- Update the ID
  108.     saveTaskId(taskId + 1)
  109. end
  110.  
  111. -- Check if there are no task files currently on the disk (i.e., the slot is free)
  112. function isTaskSlotFree()
  113.     local disk = peripheral.wrap("bottom")
  114.     local diskPath = disk.getMountPath()
  115.  
  116.     if not diskPath then
  117.         error("No disk detected under the computer!")
  118.     end
  119.  
  120.     local tasksPath = fs.combine(diskPath, "tasks")
  121.  
  122.     if not fs.exists(tasksPath) then
  123.         -- No tasks folder means no tasks
  124.         return true
  125.     end
  126.  
  127.     local files = fs.list(tasksPath)
  128.     return #files == 0
  129. end
  130.  
  131.  
  132. while true do
  133.     if isTaskSlotFree() and chestEmpty(peripheral.wrap("back")) then
  134.         transportFirstType()
  135.         saveTaskToDisk(strItem, numItems, limitStack)
  136.     else
  137.         sleep(0.2)
  138.     end
  139. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement