Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --What this will do:
- --Craft utilities
- --Easier crafting of items
- --easier item placement for crafting
- local recipesFile = "craftRecipes"
- local mv = 16 --Recommended not to change this
- local recipes = false
- local ourTurtle = false
- function turtleName(name)
- ourTurtle = name
- end
- local function pEr(func,i,exp,got,i2)
- if i2 then
- return func..":"..tostring(i).." (Table Index "..tostring(i2).."): Expected "..exp..", got "..type(got)
- else
- return func..":"..tostring(i)..": Expected "..exp..", got "..type(got)
- end
- end
- function rI(i)
- if i <= 3 then return i
- elseif i > 3 and i <= 6 then return i+1
- else return i + 2
- end
- return false
- end
- local function dumpContents()
- for i = 1,16 do
- end
- end
- function swap(a,b)
- turtle.select(a)
- turtle.transferTo(mv)
- turtle.select(b)
- turtle.transferTo(a)
- turtle.select(mv)
- turtle.transferTo(b)
- end
- function moveItemsForRecipe(recipe)
- assert(type(recipe) == "string",pEr("moveItemsForRecipe",1,"string",recipe))
- local cRecipe = false
- for k,v in pairs(recipes) do
- if k == recipe then
- cRecipe = v
- end
- end
- if not cRecipe then
- return false,"Failed to find recipe!"
- end
- moveToWall()
- for i = 1,9 do
- end
- end
- function grabItemsForRecipe(recipe,chests)
- assert(type(recipe) == "string",pEr("grabItemsForRecipe",1,"string",recipe))
- assert(type(chests) == "table",pEr("grabItemsForRecipe",2,"table",chests))
- assert(type(ourTurtle) == "string",pEr("ourTurtle",0,"string",ourTurtle).."| Make sure to use the function \"<APINAME>.turtleName()\"")
- assert(type(recipes) == "table",pEr("grabItemsForRecipe,",0,"table",recipes))
- local cRecipe = false
- for k,v in pairs(recipes) do
- if k == recipe then
- cRecipe = v
- end
- end
- if not cRecipe then
- return false,"Failed to find recipe!"
- end
- if #chests == 0 then
- return false,"No chests to choose from!"
- end
- local totalGrabbed = {}
- for i = 1,#chests do
- assert(type(chests[i]) == "string",pEr("grabItemsForRecipe",2,"string",chests[i],i))
- local cChest = peripheral.wrap(chests[i])
- if cChest then
- local ls = cChest.list()
- for i = 1,cChest.size() do
- if ls[i] then
- for k,v in pairs(cRecipe.totals) do
- for k2,v2 in pairs(v) do
- if ls[i].name == k and ls[i].damage == k2 then
- if not totalGrabbed[k] then totalGrabbed[k] = {k2 = 0} end
- if not totalGrabbed[k][k2] then totalGrabbed[k][k2] = 0 end
- if totalGrabbed[k][k2] < v2 then
- totalGrabbed[k][k2] = cChest.pushItems(ourTurtle,i,v2-totalGrabbed[k][k2])
- end
- end
- end
- end
- end
- end
- end
- end
- --
- for k,v in pairs(cRecipe.totals) do
- for k2,v2 in pairs(v) do
- local switch = false
- for i = 1,16 do
- local dat = turtle.getItemDetail(i)
- if dat then
- if dat.name == k and dat.damage == k2 and dat.count >= v2 then
- switch = true
- end
- end
- end
- if not switch then dumpContents() return false,k.." not found (damage "..k2..")" end
- end
- end
- return true
- end
- function readRecipesRaw()
- local hndl = fs.open(recipesFile,"r")
- if hndl then
- local all = hndl.readAll()
- hndl.close()
- return all
- end
- return {}
- end
- function readRecipes()
- local hndl = fs.open(recipesFile,"r")
- if hndl then
- local all = textutils.unserialize(hndl.readAll())
- hndl.close()
- return all or {}
- end
- return {}
- end
- function loadRecipes()
- recipes = readRecipes()
- end
- function saveRecipe(name)
- if type(name) ~= "string" then
- printError(pEr("saveRecipe",1,"string",name))
- return false
- end
- recipes = readRecipes()
- if recipes[name] then
- print("Recipe exists, overwrite?")
- local ans = string.lower(io.read())
- if ans ~= "y" then
- print("Cancelling")
- return
- end
- end
- if turtle.craft(0) then
- print("Recipe is savable.")
- else
- error("Recipe is not a valid recipe.")
- end
- local data = {locs = {},totals = {}}
- for i = 1,9 do
- local dat = turtle.getItemDetail(rI(i))
- if dat then
- print(tostring(i)..":",dat.name,dat.count,dat.damage)
- data.locs[rI(i)] = {}
- data.locs[rI(i)].name = dat.name
- data.locs[rI(i)].damage = dat.damage
- if not data.totals[dat.name] then
- data.totals[dat.name] = {}
- data.totals[dat.name][dat.damage] = 0
- end
- data.totals[dat.name][dat.damage] = data.totals[dat.name][dat.damage] + 1
- end
- end
- recipes[name] = data
- local handle = fs.open(recipesFile,"w")
- handle.write(textutils.serialise(recipes))
- handle.close()
- end
- function help(func)
- local function printUsage(str_func,tab_args,tab_info)
- term.clear()
- term.setCursorPos(1,1)
- local old = term.getTextColor()
- --print(str_func)
- term.setTextColor(colors.yellow)
- print("Usage:")
- term.setTextColor(old)
- local args = ""
- for i = 1,#tab_args do
- if i ~= 1 then
- args = args..",\n"
- end
- if tab_args[i][1] then
- args = args.."<"..tab_args[i][2]..">"
- else
- args = args.."["..tab_args[i][2].."]"
- end
- end
- print(str_func.."("..args..")")
- print("-----")
- for i = 1,#tab_info do
- print(tab_info[i])
- end
- end
- func = func or "help"
- func = string.lower(func)
- if func == "help" then
- printUsage(func,
- {
- {
- [1]=false,
- [2]="string functionName"
- }
- },
- {
- "Prints information on how to use the function specified"
- })
- elseif func == "saverecipe" then
- printUsage("saveRecipe",
- {
- {
- [1]=true,
- [2]="string saveName"
- }
- },
- {
- "Saves a recipe to storage for use later.",
- "Requires items to be in the turtle, in the orientation they would be used for crafting the item."
- })
- elseif func == "loadrecipes" then
- printUsage("loadRecipes",
- {},
- {
- "Loads all recipes from the save file into RAM",
- "This is required in order to use functions like \'getItemsForRecipe\'"
- })
- elseif func == "readrecipes" then
- printUsage("readRecipes",
- {
- {
- }
- },
- {
- "Reads the recipes in the savefile, then returns them as a table."
- })
- elseif func == "readrecipesraw" then
- printUsage("readRecipesRaw",
- {
- },
- {
- "Reads the recipes in the savefile, then returns them as a string."
- })
- elseif func == "grabitemsforrecipe" then
- printUsage("grabItemsForRecipe",
- {
- {
- [1]=true,
- [2]="string recipeName"
- },
- {
- [1]=true,
- [2]="table [strings] listOfChestNamesConnectedToStorageNetwork"
- }
- },
- {
- "Grabs the required items for recipeName from the chests listed"
- })
- elseif func == "moveitemsforrecipe" then
- printUsage("moveItemsForRecipe",
- {
- {
- [1]=true,
- [2]="string recipeName"
- }
- },
- {
- "Moves items in the turtle to the recipe locations.",
- "NOTE: THIS WILL BE DEPRECATED"
- })
- elseif func == "swap" then
- printUsage(func,
- {
- {
- [1]=true,
- [2]="number index1"
- },
- {
- [1]=true,
- [2]="number index2",
- }
- },
- {
- "Swaps two items in the turtle, using the 16th slot as an extra slot",
- "Useful for sorting."
- })
- elseif func == "ri" then
- printUsage("rI",
- {
- {
- [1]=true,
- [2]="number index"
- }
- },
- {
- "Converts a crafting grid index (from 1-9) to the turtle's native index, useful for moving items into place."
- })
- elseif func == "turtlename" then
- printUsage("turtleName",
- {
- },
- {
- "Sets the turtle's name, for pushing items to the turtle from chests.",
- "This is required in order to recieve items."
- })
- end
- end
Add Comment
Please, Sign In to add comment