Advertisement
Guest User

sorter

a guest
Mar 28th, 2020
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.85 KB | None | 0 0
  1. os.loadAPI("utils")
  2.  
  3.  
  4. inputs = {}
  5. default = "sortForward"
  6.  
  7.  
  8. function loadFilter(filterFile)
  9.   local result = {}
  10.   local file = fs.open(filterFile, "r")
  11.   if file == nil then
  12.     return result
  13.   end
  14.  
  15.   local item = file.readLine()
  16.   while item ~= nil do    
  17.     if item == "DEFAULT" then
  18.       default = filterFile
  19.     elseif item == "INPUT" then
  20.       table.insert(inputs, filterFile)
  21.       -- inputs cannot be outputs
  22.       -- so ignore any filters
  23.       return {}
  24.     else
  25.       table.insert(result, item)
  26.     end
  27.     item = file.readLine()
  28.   end
  29.  
  30.   file.close()
  31.  
  32.   print("Loaded "..#result.." items from "..filterFile)
  33.  
  34.   return result
  35. end
  36.  
  37.  
  38. function dropDefault()
  39.   if default == "sortUp" then
  40.     turtle.dropUp()
  41.   elseif default == "sortDown" then
  42.     turtle.dropDown()
  43.   else
  44.     turtle.drop()
  45.   end
  46. end
  47.  
  48.  
  49. function sortItems()
  50.   for i = 1,16 do
  51.     local item = turtle.getItemDetail(i)
  52.  
  53.     if item ~= nil then
  54.       turtle.select(i)
  55.      
  56.       if utils.itemMatchesAny(item, up) then
  57.         turtle.dropUp()
  58.       elseif utils.itemMatchesAny(item, down) then
  59.         turtle.dropDown()
  60.       elseif utils.itemMatchesAny(item, forward) then
  61.         turtle.drop()
  62.       else
  63.         dropDefault()
  64.       end
  65.     end
  66.   end
  67. end
  68.  
  69.  
  70. function suckItems()
  71.   if utils.tableContains(inputs, "sortUp") then
  72.     turtle.suckUp()
  73.   end
  74.   if utils.tableContains(inputs, "sortDown") then
  75.     turtle.suckDown()
  76.   end
  77.   if utils.tableContains(inputs, "sortForward") then
  78.     turtle.suck()
  79.   end
  80. end
  81.  
  82.  
  83. up = loadFilter("sortUp")
  84. down = loadFilter("sortDown")
  85. forward = loadFilter("sortForward")
  86.  
  87.  
  88. while true do
  89.   suckItems()
  90.   sortItems()
  91.  
  92.   -- schedule an event in 5 seconds
  93.   -- to check for items
  94.   os.startTimer(5)
  95.   -- wait for any event, including
  96.   -- timer or turtle_inventory
  97.   os.pullEvent()
  98. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement