Advertisement
Guest User

Sort

a guest
Nov 6th, 2022
487
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.14 KB | None | 0 0
  1. --cA or chest array is a 2d array of strings
  2. --it correlates to the chests behind the turtle
  3. --simply add a space and the items name
  4. --and that item will go in that chest
  5. --You can also add search terms
  6. --for finding a specific chest
  7. --To add more chests:
  8. --simply add a new array and chests
  9. --just makes sure they're the same length!
  10. --Also move the lava to next to the end chest
  11. local cA = {}
  12. cA[1] = {}
  13. cA[2] = {}
  14. cA[1] = {"cobblestone","dirt","ingot","stone","seed","wood log plank stick"}
  15. cA[2] = {"ore","beef pork wheat slice fish chicken egg cake","bone flesh gun string","coal","diamond emerald lapis", "junk gravel"}
  16.  
  17.  
  18.  
  19. --The functions below are vital to the
  20. --programs function. do not alter them
  21. --unless you know what youre doing
  22. function GO(x)
  23.     checkFuel()
  24.     for i=1,x do
  25.         turtle.forward()
  26.     end
  27. end
  28. function checkFuel()
  29.     if turtle.getFuelLevel() < 30 then
  30.         turtle.select(16)
  31.         turtle.refuel(3)
  32.         turtle.select(1)
  33.     end
  34. end
  35. function TURN(x)
  36.     if x == 3 then
  37.         turtle.turnRight()
  38.     else
  39.         for i=1,x do
  40.             turtle.turnLeft()
  41.         end
  42.     end
  43. end
  44.  
  45. function PLACE(str)
  46.     superBreak = false
  47.     x = 0
  48.     y = 0
  49.     for i=1, #cA,1 do
  50.         for j=1, #cA[1],1 do
  51.             for w in string.gmatch(cA[i][j], "([^".."%s".."]+)") do
  52.                 if string.find(str, w) then
  53.                     superBreak = true
  54.                     x = j
  55.                     y = i
  56.                     break
  57.                 end
  58.                 if superBreak then
  59.                     break
  60.                 end
  61.             end
  62.             if superBreak then
  63.                 break
  64.             end
  65.         end
  66.         if superBreak then
  67.             break
  68.         end
  69.     end
  70.     if x == 0 or y == 0 then
  71.         x = #cA[1]
  72.         y = #cA
  73.     end
  74.     TURN(2)
  75.     GO((2*(y-1))+1)
  76.     TURN(1)
  77.     GO((2*(x-1))+1)
  78.     if not turtle.dropDown(64) then
  79.         turtle.dropUp(64)
  80.     end
  81.     TURN(2)
  82.     GO((2*(x-1))+1)
  83.     TURN(3)
  84.     GO((2*(y-1))+1)            
  85. end
  86.  
  87. function sortJunk()
  88.     while turtle.suckDown(64) do
  89.         cBlock = turtle.getItemDetail(1)
  90.         PLACE(cBlock.name)
  91.     end
  92. end
  93.  
  94. function getStuff(str, stacks)
  95.     superBreak = false
  96.     x = 0
  97.     y = 0
  98.     for i=1, #cA,1 do
  99.         for j=1, #cA[1],1 do
  100.             for w in string.gmatch(cA[i][j], "([^".."%s".."]+)") do
  101.                
  102.                 if string.find(w, str) then
  103.                     superBreak = true
  104.                     x = j
  105.                     y = i
  106.                     break
  107.                 end
  108.             end
  109.         end
  110.         if superBreak then
  111.             break
  112.         end
  113.     end
  114.     if x == 0 or y == 0 then
  115.         print("Item not found... \n did you mean junk? \n")
  116.         return
  117.     end
  118.     TURN(2)
  119.     GO((2*(y-1))+1)
  120.     TURN(1)
  121.     GO((2*(x-1))+1)
  122.     if not turtle.suckUp(64) then
  123.         for i=1, stacks, 1 do
  124.             turtle.suckDown(64)
  125.         end
  126.     else
  127.         for i=1, stacks-1, 1 do
  128.             turtle.suckUp(64)
  129.         end
  130.     end
  131.     TURN(2)
  132.     GO((2*(x-1))+1)
  133.     TURN(3)
  134.     GO((2*(y-1))+1)
  135.     for i=1, stacks, 1 do
  136.         turtle.select(i)
  137.         turtle.dropDown(64)
  138.     end
  139.     turtle.select(1)
  140. end
  141.  
  142. function JUNK(stacks)
  143.     for i=1, stacks,1 do
  144.         turtle.suckDown(64)
  145.     end
  146.     TURN(2)
  147.     GO((2* #cA)-1)
  148.     turtle.down()
  149.     for i=1, stacks,1 do
  150.         turtle.select(i)
  151.         turtle.dropDown(64)
  152.     end
  153.     turtle.select(1)
  154.     turtle.up()
  155.     TURN(2)
  156.     GO((2*#cA)-1)
  157. end
  158.  
  159.  
  160. --Main program logic
  161. while true do
  162.     print("What would you like to do? \n")
  163.     print("sort? get? trash? \n")
  164.     t = tostring(read())
  165.     if t == "sort" then
  166.         print("Right on it!")
  167.         sortJunk()
  168.     elseif t == "get" then
  169.         print("What would you like? \n")
  170.         b = tostring(read())
  171.         print("and how many stacks? \n")
  172.         s = tonumber(read())
  173.         getStuff(b,s)
  174.     elseif t == "junk" or t == "trash" then
  175.         print("How many stacks? \n")
  176.         s = tonumber(read())
  177.         JUNK(s)
  178.     else
  179.         print("Unsupported Function")
  180.     end
  181. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement