Advertisement
Guest User

plop.lua

a guest
Mar 25th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.31 KB | None | 0 0
  1. me=peripheral.wrap("back")
  2. chest=peripheral.wrap("front")
  3. ic=peripheral.wrap("right")
  4.  
  5. function main()
  6.     while true do
  7.         rs.setOutput("left",false)
  8.         parallel.waitForAny(function() flag=craftrequest() return flag end,function() flag=learnrecipe() return flag end)
  9.         if not(flag==nil) then
  10.             if type(flag)=="boolean" then
  11.                 craftstart()
  12.             end
  13.             if type(flag)=="string" then
  14.                 learnstart(flag)
  15.             end
  16.         end
  17.     end
  18. end
  19.  
  20. function learnrecipe()
  21.     io.write("Insert the craft reference item in upper left slot beforehand.\nName of the new recipe : ")
  22.     name=io.read()
  23.     return name
  24. end
  25.  
  26. function craftstart()
  27.     --~ chest.pullItems("north",turtle.getSelectedSlot(),64)
  28.     --~ chest.pushItems("up",1,64)
  29.     recipeName,recipe,number=loadRecipe()
  30.     turtle.select(2)
  31.     --~ print(number)
  32.     --~ for a,b in pairs(recipe) do for c,d in pairs(b) do print(c,d) end end  
  33.  
  34.     for k,v in pairs(recipe) do
  35.     item=v.name.."@"..tostring(v.damage)
  36.     if not(me.findItem(item)==nil) then
  37.         if me.findItem(item).getMetadata().count<v.count then
  38.             me.findItem(item).craft(v.count-me.findItem(item).getMetadata().count)
  39.             while not(me.findItem(item).getMetadata().count>v.count-1) do
  40.                 sleep(1)
  41.             end
  42.         end
  43.         me.findItem(item).export("east",v.count,2)
  44.         rs.setOutput("left",true)
  45.         while turtle.getItemCount()>0 do
  46.             sleep(0.05)
  47.         end
  48.         rs.setOutput("left",false)
  49.     end
  50.     end
  51.    
  52.     while chest.getItem(1)==nil do
  53.             sleep(0.05)
  54.     end
  55.    
  56.     chest.pushItems("west",1,64,2)
  57.     ic.pullItems("north",1,64,1)
  58.     ic.pushItems("west",1,64)
  59.     ic.pullItems("north",2,64,1)
  60.     ic.pushItems("west",1,64)
  61. end
  62.  
  63. function learnstart(flag)
  64.     if turtle.getItemDetail(1)==nil then
  65.         io.write("No reference item !\nPlease retry after you buy a brain ...")
  66.     else
  67.         refitem=turtle.getItemDetail(1).name
  68.         io.write("Recipe nĀ°")
  69.         number=io.read()
  70.  
  71.         io.write("Recipe saving, please wait ...\n")
  72.         ect=peripheral.wrap("front")
  73.         if not(ect==nil) then
  74.             newRecipe=analyse()
  75.             save(newRecipe,flag,refitem,number)
  76.             io.write("The recipe for "..flag.." has been added !\n")
  77.             turtle.dropUp(1)
  78.         else
  79.             io.write("No table found ! \n")
  80.         end
  81.     end
  82. end
  83.  
  84. function craftrequest()
  85.     flag=false
  86.     k=1
  87.     local item=nil
  88.     while not(flag) do
  89.         item=ic.getItem(k)
  90.         if not(item==nil) then
  91.             flag=true
  92.             ic.pushItems("north",1,1,1)
  93.             turtle.select(1)
  94.             recipeName,recipe=loadRecipe(turtle.getItemDetail().name)
  95.             if recipe==nil then
  96.                 flag=false
  97.                 k=k+1
  98.                 if k==chest.size()+1 then
  99.                     k=1
  100.                 end
  101.             end
  102.         else
  103.             k=k+1
  104.             if k==chest.size()+1 then
  105.                 k=1
  106.             end
  107.         end
  108.     end
  109.     return flag
  110. end
  111.  
  112. function analyse()
  113.     ect=peripheral.wrap("front")
  114.     items={}
  115.     for k=1,ect.size() do
  116.         data=ect.getItem(k)
  117.         if not(data==nil) then
  118.             dat=data.getMetadata()
  119.             for k,v in pairs(dat) do print(k,v) end
  120.             table.insert(items,dat)
  121.         end
  122.     end
  123.    
  124.     recipe={}
  125.     for k,v in pairs(items) do
  126.         flag=0
  127.         if not(recipe==nil) then
  128.             for j,c in pairs(recipe) do
  129.                 if (c.name==v.name and c.damage==v.damage) then
  130.                     c.count=c.count+1
  131.                     flag=1                 
  132.                 end
  133.             end
  134.             if flag==0 then
  135.                 newitem={name=v.name,count=1,displayName=v.displayName,damage=v.damage}
  136.                 table.insert(recipe,newitem)
  137.             end
  138.         end
  139.     end
  140.    
  141.     return recipe
  142. end
  143.  
  144. function save(recipe,recipeName,refitem,number)
  145.     sfile=io.open("recipe"..number,"w")
  146.     sfile:write("function therecipe()\nif not(turtle.getItemDetail()==nil) then\nif turtle.getItemDetail().name=='"..refitem.."' then\nreturn '"..recipeName.."',")
  147.     strrecipe=tabletostr(recipe)
  148.     sfile:write(strrecipe)
  149.     sfile:write("\n".."end\nend\nend\n\n")
  150.     sfile:close(recipeName)
  151. end
  152.  
  153. function loadRecipe()
  154.     local flag=false
  155.     n=1
  156.     while (n<100 and not(flag)) do
  157.         if file_exists("recipe"..tostring(n)) then
  158.             dofile("recipe"..tostring(n))
  159.             recipeName,recipe=therecipe()
  160.             if not(recipe==nil) then
  161.                 flag=true
  162.             end
  163.         end
  164.         n=n+1
  165.     end
  166.     n=n-1
  167.     return recipeName,recipe,n
  168. end
  169.  
  170. function file_exists(file)
  171.   local f = io.open(file, "rb")
  172.   if f then f:close() end
  173.   return f ~= nil
  174. end
  175.  
  176. function tabletostr(tbl)
  177.     str="{"
  178.     for k,v in pairs(tbl) do
  179.         str=str.."{"
  180.         for j,c in pairs(v) do
  181.             str=str..tostring(j).."="
  182.             if type(c)=="string" then
  183.                 str=str.."'"..tostring(c).."'"..","
  184.             else
  185.                 str=str..tostring(c)..","
  186.             end
  187.         end
  188.         str=str.."},"
  189.     end
  190.     str=str.."}"
  191.     return str
  192. end
  193.  
  194.  
  195. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement