Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- me=peripheral.wrap("back")
- chest=peripheral.wrap("front")
- ic=peripheral.wrap("right")
- function main()
- while true do
- rs.setOutput("left",false)
- parallel.waitForAny(function() flag=craftrequest() return flag end,function() flag=learnrecipe() return flag end)
- if not(flag==nil) then
- if type(flag)=="boolean" then
- craftstart()
- end
- if type(flag)=="string" then
- learnstart(flag)
- end
- end
- end
- end
- function learnrecipe()
- io.write("Insert the craft reference item in upper left slot beforehand.\nName of the new recipe : ")
- name=io.read()
- return name
- end
- function craftstart()
- --~ chest.pullItems("north",turtle.getSelectedSlot(),64)
- --~ chest.pushItems("up",1,64)
- recipeName,recipe,number=loadRecipe()
- turtle.select(2)
- --~ print(number)
- --~ for a,b in pairs(recipe) do for c,d in pairs(b) do print(c,d) end end
- for k,v in pairs(recipe) do
- item=v.name.."@"..tostring(v.damage)
- if not(me.findItem(item)==nil) then
- if me.findItem(item).getMetadata().count<v.count then
- me.findItem(item).craft(v.count-me.findItem(item).getMetadata().count)
- while not(me.findItem(item).getMetadata().count>v.count-1) do
- sleep(1)
- end
- end
- me.findItem(item).export("east",v.count,2)
- rs.setOutput("left",true)
- while turtle.getItemCount()>0 do
- sleep(0.05)
- end
- rs.setOutput("left",false)
- end
- end
- while chest.getItem(1)==nil do
- sleep(0.05)
- end
- chest.pushItems("west",1,64,2)
- ic.pullItems("north",1,64,1)
- ic.pushItems("west",1,64)
- ic.pullItems("north",2,64,1)
- ic.pushItems("west",1,64)
- end
- function learnstart(flag)
- if turtle.getItemDetail(1)==nil then
- io.write("No reference item !\nPlease retry after you buy a brain ...")
- else
- refitem=turtle.getItemDetail(1).name
- io.write("Recipe nĀ°")
- number=io.read()
- io.write("Recipe saving, please wait ...\n")
- ect=peripheral.wrap("front")
- if not(ect==nil) then
- newRecipe=analyse()
- save(newRecipe,flag,refitem,number)
- io.write("The recipe for "..flag.." has been added !\n")
- turtle.dropUp(1)
- else
- io.write("No table found ! \n")
- end
- end
- end
- function craftrequest()
- flag=false
- k=1
- local item=nil
- while not(flag) do
- item=ic.getItem(k)
- if not(item==nil) then
- flag=true
- ic.pushItems("north",1,1,1)
- turtle.select(1)
- recipeName,recipe=loadRecipe(turtle.getItemDetail().name)
- if recipe==nil then
- flag=false
- k=k+1
- if k==chest.size()+1 then
- k=1
- end
- end
- else
- k=k+1
- if k==chest.size()+1 then
- k=1
- end
- end
- end
- return flag
- end
- function analyse()
- ect=peripheral.wrap("front")
- items={}
- for k=1,ect.size() do
- data=ect.getItem(k)
- if not(data==nil) then
- dat=data.getMetadata()
- for k,v in pairs(dat) do print(k,v) end
- table.insert(items,dat)
- end
- end
- recipe={}
- for k,v in pairs(items) do
- flag=0
- if not(recipe==nil) then
- for j,c in pairs(recipe) do
- if (c.name==v.name and c.damage==v.damage) then
- c.count=c.count+1
- flag=1
- end
- end
- if flag==0 then
- newitem={name=v.name,count=1,displayName=v.displayName,damage=v.damage}
- table.insert(recipe,newitem)
- end
- end
- end
- return recipe
- end
- function save(recipe,recipeName,refitem,number)
- sfile=io.open("recipe"..number,"w")
- sfile:write("function therecipe()\nif not(turtle.getItemDetail()==nil) then\nif turtle.getItemDetail().name=='"..refitem.."' then\nreturn '"..recipeName.."',")
- strrecipe=tabletostr(recipe)
- sfile:write(strrecipe)
- sfile:write("\n".."end\nend\nend\n\n")
- sfile:close(recipeName)
- end
- function loadRecipe()
- local flag=false
- n=1
- while (n<100 and not(flag)) do
- if file_exists("recipe"..tostring(n)) then
- dofile("recipe"..tostring(n))
- recipeName,recipe=therecipe()
- if not(recipe==nil) then
- flag=true
- end
- end
- n=n+1
- end
- n=n-1
- return recipeName,recipe,n
- end
- function file_exists(file)
- local f = io.open(file, "rb")
- if f then f:close() end
- return f ~= nil
- end
- function tabletostr(tbl)
- str="{"
- for k,v in pairs(tbl) do
- str=str.."{"
- for j,c in pairs(v) do
- str=str..tostring(j).."="
- if type(c)=="string" then
- str=str.."'"..tostring(c).."'"..","
- else
- str=str..tostring(c)..","
- end
- end
- str=str.."},"
- end
- str=str.."}"
- return str
- end
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement