Advertisement
moo3oo3oo3

Witchery Cauldron Automation

Apr 6th, 2020
454
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.39 KB | None | 0 0
  1. local modem = peripheral.find("modem")
  2. local chatBox = peripheral.find("chatBox")
  3. local monitor = nil
  4. local chest = nil
  5.  
  6. local turtleToChest = "east"
  7.  
  8. local dir = "witcheryRecipes"
  9. if not fs.exists(dir) then fs.makeDir(dir) end
  10.  
  11. function fileExists(name)
  12.     if fs.exists(dir.."/"..name) then return true end
  13.     return false
  14. end
  15.  
  16. function isInt(n)
  17.     local num = tonumber(n)
  18.     if num == nil then return false end
  19.     return num == math.floor(num)
  20. end
  21.  
  22. function deleteRecipe(playerName, name)
  23.     chatBox.tell(playerName, "Deleting recipe for "..name)
  24.     fs.delete(dir.."/"..name)
  25. end
  26.  
  27. function addRecipe(playerName, name)
  28.     chatBox.tell(playerName, "Adding recipe for "..name)
  29.     turtle.turnRight()
  30.     chest = peripheral.wrap("front")
  31.    
  32.     local h = fs.open(dir.."/"..name, "w")
  33.    
  34.     for i=1,10,1 do
  35.         local item = chest.getStackInSlot(i)
  36.         if item == nil then break end
  37.         h.writeLine(item.raw_name)
  38.         print("Adding "..item.id.." to "..name.." recipe")
  39.     end
  40.    
  41.     h.close()
  42.     turtle.turnLeft()
  43. end
  44.  
  45. function dumpItems(amt, isDelay, startingSlot, endingSlot)
  46.     if startingSlot == nil then startingSlot = 1 end
  47.     if endingSlot == nil then endingSlot = 16 end
  48.    
  49.     for i=startingSlot,endingSlot,1 do
  50.         turtle.select(i)
  51.         if amt == nil then turtle.drop() else turtle.drop(amt) end
  52.         if isDelay == true then os.sleep(1.5) end
  53.     end
  54. end
  55.  
  56. function craft(playerName, name, amt)
  57.     chatBox.tell(playerName, "Crafting "..amt.." of "..name)
  58.     local recipe = {}
  59.     local h = fs.open(dir.."/"..name, "r")
  60.     local numIngred = 0
  61.    
  62.     --Loads recipe
  63.     local order = 1
  64.     while true do
  65.         local line = h.readLine()
  66.         if line == nil then break end
  67.         recipe[line] = order
  68.         order = order + 1
  69.         numIngred = numIngred + 1
  70.     end
  71.    
  72.     turtle.turnRight()
  73.     chest = peripheral.wrap("front")
  74.    
  75.     --Find ingredients
  76.     for i=1,chest.getInventorySize(),1 do
  77.         if next(recipe) == nil then break end
  78.         local item = chest.getStackInSlot(i)
  79.         if item ~= nil then
  80.             if recipe[item.raw_name] ~= nil then
  81.                 if item.qty >= tonumber(amt) then
  82.                     recipe[item.raw_name] = nil
  83.                     chest.pushItem(turtleToChest, i, amt, recipe[item.raw_name])
  84.                     print("Pulling "..item.id.." from the chest for "..name.." recipe")
  85.                 else
  86.                     os.sleep(1)
  87.                     chatBox.tell(playerName,  "Not enough items! Canceling request.")
  88.                     print("Need "..(amt-item.qty).." more of "..item.raw_name.."!")
  89.                     dumpItems()
  90.                     turtle.turnLeft()
  91.                     return false
  92.                 end
  93.             end
  94.         end
  95.     end
  96.    
  97.     --Abort when missing ingredients
  98.     if next(recipe) then
  99.         os.sleep(1)
  100.         chatBox.tell(playerName,  "Missing materials! Canceling request.")
  101.         print("Missing:\n")
  102.         for i,_ in pairs(recipe) do
  103.             print(i.."\n")
  104.         end
  105.         dumpItems()
  106.         turtle.turnLeft()
  107.         return false
  108.     end
  109.    
  110.    
  111.    
  112.     turtle.turnLeft()
  113.     turtle.forward() turtle.forward() turtle.forward() turtle.forward()
  114.    
  115.     --Actual crafting
  116.     for i=0,amt-1,1 do
  117.         dumpItems(1, true, 1, numIngred) turtle.select(1)
  118.         os.sleep(2)
  119.         while not turtle.suck() do os.sleep(1) end
  120.         os.sleep(4)
  121.     end
  122.    
  123.     turtle.back() turtle.back() turtle.back() turtle.back()
  124.     turtle.turnRight() dumpItems() turtle.turnLeft()
  125.     chatBox.tell(playerName, "Finished crafting "..amt.." batches of "..name.."!")
  126. end
  127.  
  128. --Main function
  129. while true do
  130.     local _, playerName, msg = os.pullEvent("command")
  131.    
  132.     if msg[1] == "Sara" then
  133.         if msg[2] == "craft" then
  134.             if msg[3] ~= nil then
  135.                 if fileExists(msg[3]) then
  136.                     if isInt(msg[4]) then
  137.                         craft(playerName, msg[3], msg[4])
  138.                     else chatBox.tell(playerName, "Missing amount field") end
  139.                 else chatBox.tell(playerName, msg[3].." is not a recipe") end
  140.             else chatBox.tell(playerName, "Missing recipe name field") end
  141.         elseif msg[2] == "addRecipe" then
  142.             if msg[3] ~= nil then
  143.                 if not fileExists(msg[3]) then
  144.                     addRecipe(playerName, msg[3])
  145.                 else chatBox.tell(playerName, msg[3].." is already a recipe") end
  146.             else chatBox.tell(playerName, "Missing recipe name field") end
  147.         elseif msg[2] == "deleteRecipe" then
  148.             if msg[3] ~= nil then
  149.                 if fileExists(msg[3]) then
  150.                     deleteRecipe(playerName, msg[3])
  151.                 else chatBox.tell(playerName, msg[3].." is not a recipe") end
  152.             else chatBox.tell(playerName, "Missing recipe name field") end
  153.         elseif msg[2] == "listRecipes" then
  154.             local list = "Recipes: "
  155.             for _,file in ipairs(fs.list(dir)) do
  156.                 list = list..file..", "
  157.             end
  158.             list = string.sub(list, 1, list:len()-2)
  159.             chatBox.tell(playerName, list)
  160.         end
  161.     end
  162.    
  163. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement