Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local modem = peripheral.find("modem")
- local chatBox = peripheral.find("chatBox")
- local monitor = nil
- local chest = nil
- local turtleToChest = "east"
- local dir = "witcheryRecipes"
- if not fs.exists(dir) then fs.makeDir(dir) end
- function fileExists(name)
- if fs.exists(dir.."/"..name) then return true end
- return false
- end
- function isInt(n)
- local num = tonumber(n)
- if num == nil then return false end
- return num == math.floor(num)
- end
- function deleteRecipe(playerName, name)
- chatBox.tell(playerName, "Deleting recipe for "..name)
- fs.delete(dir.."/"..name)
- end
- function addRecipe(playerName, name)
- chatBox.tell(playerName, "Adding recipe for "..name)
- turtle.turnRight()
- chest = peripheral.wrap("front")
- local h = fs.open(dir.."/"..name, "w")
- for i=1,10,1 do
- local item = chest.getStackInSlot(i)
- if item == nil then break end
- h.writeLine(item.raw_name)
- print("Adding "..item.id.." to "..name.." recipe")
- end
- h.close()
- turtle.turnLeft()
- end
- function dumpItems(amt, isDelay, startingSlot, endingSlot)
- if startingSlot == nil then startingSlot = 1 end
- if endingSlot == nil then endingSlot = 16 end
- for i=startingSlot,endingSlot,1 do
- turtle.select(i)
- if amt == nil then turtle.drop() else turtle.drop(amt) end
- if isDelay == true then os.sleep(1.5) end
- end
- end
- function craft(playerName, name, amt)
- chatBox.tell(playerName, "Crafting "..amt.." of "..name)
- local recipe = {}
- local h = fs.open(dir.."/"..name, "r")
- local numIngred = 0
- --Loads recipe
- local order = 1
- while true do
- local line = h.readLine()
- if line == nil then break end
- recipe[line] = order
- order = order + 1
- numIngred = numIngred + 1
- end
- turtle.turnRight()
- chest = peripheral.wrap("front")
- --Find ingredients
- for i=1,chest.getInventorySize(),1 do
- if next(recipe) == nil then break end
- local item = chest.getStackInSlot(i)
- if item ~= nil then
- if recipe[item.raw_name] ~= nil then
- if item.qty >= tonumber(amt) then
- recipe[item.raw_name] = nil
- chest.pushItem(turtleToChest, i, amt, recipe[item.raw_name])
- print("Pulling "..item.id.." from the chest for "..name.." recipe")
- else
- os.sleep(1)
- chatBox.tell(playerName, "Not enough items! Canceling request.")
- print("Need "..(amt-item.qty).." more of "..item.raw_name.."!")
- dumpItems()
- turtle.turnLeft()
- return false
- end
- end
- end
- end
- --Abort when missing ingredients
- if next(recipe) then
- os.sleep(1)
- chatBox.tell(playerName, "Missing materials! Canceling request.")
- print("Missing:\n")
- for i,_ in pairs(recipe) do
- print(i.."\n")
- end
- dumpItems()
- turtle.turnLeft()
- return false
- end
- turtle.turnLeft()
- turtle.forward() turtle.forward() turtle.forward() turtle.forward()
- --Actual crafting
- for i=0,amt-1,1 do
- dumpItems(1, true, 1, numIngred) turtle.select(1)
- os.sleep(2)
- while not turtle.suck() do os.sleep(1) end
- os.sleep(4)
- end
- turtle.back() turtle.back() turtle.back() turtle.back()
- turtle.turnRight() dumpItems() turtle.turnLeft()
- chatBox.tell(playerName, "Finished crafting "..amt.." batches of "..name.."!")
- end
- --Main function
- while true do
- local _, playerName, msg = os.pullEvent("command")
- if msg[1] == "Sara" then
- if msg[2] == "craft" then
- if msg[3] ~= nil then
- if fileExists(msg[3]) then
- if isInt(msg[4]) then
- craft(playerName, msg[3], msg[4])
- else chatBox.tell(playerName, "Missing amount field") end
- else chatBox.tell(playerName, msg[3].." is not a recipe") end
- else chatBox.tell(playerName, "Missing recipe name field") end
- elseif msg[2] == "addRecipe" then
- if msg[3] ~= nil then
- if not fileExists(msg[3]) then
- addRecipe(playerName, msg[3])
- else chatBox.tell(playerName, msg[3].." is already a recipe") end
- else chatBox.tell(playerName, "Missing recipe name field") end
- elseif msg[2] == "deleteRecipe" then
- if msg[3] ~= nil then
- if fileExists(msg[3]) then
- deleteRecipe(playerName, msg[3])
- else chatBox.tell(playerName, msg[3].." is not a recipe") end
- else chatBox.tell(playerName, "Missing recipe name field") end
- elseif msg[2] == "listRecipes" then
- local list = "Recipes: "
- for _,file in ipairs(fs.list(dir)) do
- list = list..file..", "
- end
- list = string.sub(list, 1, list:len()-2)
- chatBox.tell(playerName, list)
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement