emily99

CC Turtle: Crafty

Jun 21st, 2023 (edited)
812
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.64 KB | Gaming | 0 0
  1. if not turtle then
  2.     printError("Requires a Turtle")
  3.     return
  4. end
  5.  
  6. local function unpackString(s)
  7.     local t = {}
  8.     for i = 1,string.len(s) do
  9.         if string.sub(s, i, i) == "0" then
  10.             table.insert(t," ")
  11.         else table.insert(t,"X") end
  12.     end
  13.     return t
  14. end
  15.  
  16. local function printRecipe(r)
  17.     local fmt = "%s%s%s"
  18.     for y = 1,3 do
  19.         local i = (y-1)*3
  20.         print(fmt:format(r[i+1],r[i+2],r[i+3]))
  21.     end
  22. end
  23.  
  24. local tmp = ""
  25. local strpat = "^[10][10][10][10][10][10][10][10][10]$"
  26. local args = {...}
  27. if #args > 1 then
  28.     printError("Maximum of 1 argument")
  29.     return
  30. elseif #args == 1 then
  31.     if args[1]:match(strpat) == nil then
  32.         printError("First argument, if supplied, must be a 9-length string of 1s and 0s.")
  33.         return
  34.     end
  35.     tmp = args[1]
  36. else
  37.     tmp = tostring(settings.get("crafty.recipe",0))
  38.     if tmp:match(strpat) == nil then
  39.         print("Set a recipe using 'set crafty.recipe 111101111'")
  40.         print("Recipe string must be exactly 9 0s or 1s, where 0 represents empty and 1 represents item.")
  41.         print("Default recipe is '111111111'")
  42.         tmp = "111111111"
  43.     end
  44. end
  45. shape = unpackString(tmp)
  46.  
  47. print("Running crafty mode. Recipe:")
  48. printRecipe(shape)
  49.  
  50. local outputIndex = 16
  51. local slots = {1,2,3,5,6,7,9,10,11}
  52.  
  53. local function nextIndex()
  54.     for i = 1, 9 do
  55.         local s = slots[i]
  56.         if shape[i] == "X" then
  57.             if turtle.getItemCount(s) == 0 then
  58.                 turtle.select(s)
  59.                 return s
  60.             end
  61.         end
  62.     end
  63.     return -1
  64. end
  65.  
  66. while true do
  67.     if nextIndex() == -1 then
  68.         turtle.select(outputIndex)
  69.         turtle.craft()
  70.         turtle.dropUp()
  71.     else
  72.         turtle.suck(1)
  73.     end
  74. end
Add Comment
Please, Sign In to add comment