Advertisement
EmidioMata

Crafter

Jul 31st, 2015
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local storage
  2. local sideredcable
  3. local sidemodem
  4.  
  5.  
  6. function createsettings()
  7.         local settings={}
  8.         print("Number of Storage:")
  9.         settings[1]="s"..read()
  10.         print("Side of Modem:")
  11.         settings[2]=read()
  12.         print("Side of RedCable:")
  13.         settings[3]=read()
  14.         save(settings,"settings.txt")
  15. end
  16.  
  17. function loadsettings()
  18.         local settings=load("settings.txt")
  19.         storage=settings[1]
  20.         sidemodem=settings[2]
  21.         sideredcable=settings[3]
  22. end
  23.  
  24. function save(table,name)
  25.         local file = fs.open(name,"w")
  26.         file.write(textutils.serialize(table))
  27.         file.close()
  28. end
  29.  
  30. function load(name)
  31.         local file = fs.open(name,"r")
  32.         local data = file.readAll()
  33.         file.close()
  34.         return textutils.unserialize(data)
  35. end
  36.  
  37. function exist(name)
  38.         file=io.open(name)
  39.         if file~=nil then io.close(file) return true else return false end
  40. end
  41.  
  42. function loadwork()
  43. local work=load("work")
  44. local w=peripheral.wrap(sideredcable)
  45. local names = w.getNamesRemote()
  46. table.sort(names)
  47. local z=0
  48. while z~=#work do
  49.     local craftable=false
  50.     local recipe=load(work[z+1])
  51.     repeat
  52.     for x=1,9 do
  53.         craftable=false
  54.         local cur=0
  55.         for c=1,#names do
  56.                 local f=peripheral.wrap(names[c])
  57.                 for b=1, f.getInventorySize() do
  58.                     if f.getStackInSlot(b)~= nil then
  59.                         if f.getStackInSlot(b).display_name==recipe[x*2] then
  60.                             cur=cur+f.getStackInSlot(b).qty
  61.                         end
  62.                     end
  63.                 end
  64.         end
  65.         if cur>=recipe[x*2+1] then
  66.             craftable=true
  67.         end
  68.         if recipe[x*2]==" " then
  69.             craftable=true
  70.         end
  71.         if not craftable then break end
  72.     end
  73.     if craftable then
  74.         for x=1,9 do
  75.         local done=false
  76.         local togo=recipe[x*2+1]
  77.             for c=1,#names do
  78.             sleep(0)
  79.                 local f=peripheral.wrap(names[c])
  80.                     for b=1, f.getInventorySize() do
  81.                         if f.getStackInSlot(b)~= nil then
  82.                             if f.getStackInSlot(b).display_name==recipe[x*2]  then
  83.                                 local num=f.getStackInSlot(b).qty
  84.                                 if num>=togo then
  85.                                 local n=x
  86.                                 if n>=4 then
  87.                                     n=n+1
  88.                                 end
  89.                                 if n>=8 then
  90.                                     n=n+1
  91.                                 end
  92.                                 if c==1 then
  93.                                     f.pushItem("north",b,togo,n)
  94.                                 end
  95.                                 if c==2 then
  96.                                     f.pushItem("east",b,togo,n)
  97.                                 end
  98.                                 if c==3 then
  99.                                     f.pushItem("south",b,togo,n)
  100.                                 end
  101.                                 if c==4 then
  102.                                     f.pushItem("west",b,togo,n)
  103.                                 end
  104.                                 done=true
  105.  
  106.                                 else
  107.  
  108.                                 local n=x
  109.                                 if n>=4 then
  110.                                     n=n+1
  111.                                 end
  112.                                 if n>=8 then
  113.                                     n=n+1
  114.                                 end
  115.                                 if c==1 then
  116.                                     f.pushItem("north",b,num,n)
  117.                                 end
  118.                                 if c==2 then
  119.                                     f.pushItem("east",b,num,n)
  120.                                 end
  121.                                 if c==3 then
  122.                                     f.pushItem("south",b,num,n)
  123.                                 end
  124.                                 if c==4 then
  125.                                     f.pushItem("west",b,num,n)
  126.                                 end
  127.                                 togo=togo-num
  128.                                 end
  129.                             end
  130.                         end
  131.                     if done then break end
  132.                     end
  133.             if done then break end
  134.             end
  135.         end
  136.         redstone.setOutput("bottom",true)
  137.         work[z+2]=work[z+2]-recipe[1]
  138.         if work[z+2]<=0 then
  139.             local newwork={}
  140.                         for v=1,#work do
  141.                              if v~=z+1 and v~=z+2 then
  142.                                    newwork[#nework+1]=work[v]
  143.                              end
  144.             end
  145.             work=newwork
  146.         end
  147.         redstone.setOutput("bottom",false)
  148.         save(work,"work")
  149.         work=load("work")
  150.     end
  151.     until craftable==false
  152. end
  153. end
  154.  
  155.        
  156.        
  157.  
  158. -----------------------------
  159. if not exist("settings.txt") then
  160.         createsettings()      
  161. end
  162.  
  163. if not exist("work") then
  164.     local work={}
  165.     save(work,"work")
  166. end
  167.  
  168. loadsettings()
  169. rednet.open(sidemodem)
  170.  
  171.  
  172. while true do
  173. local id, mens, protocol =rednet.receive(5)
  174.         if protocol =="Crafter"..storage then
  175.                 local work={}
  176.                 if exist("work") then
  177.                 changes=load("work")
  178.                 end
  179.                 local newwork=textutils.unserialize(mens)
  180.                 if work[1]==nil then
  181.                          work[1]=newwork[1]
  182.                 else
  183.                         work[#work+1]=newwork[1]
  184.                 end
  185.                 work[#work+1]=newwork[2]
  186.                 save(work,"work")
  187.                 rednet.send(id,"ok","confirmCrafter"..storage)
  188.         end
  189.     if protocol~=nil then
  190.         if (string.sub(protocol,1,9)..storage)==("newRecCra"..storage) then
  191.                 local file = fs.open(   (string.sub(protocol,1+string.len((string.sub(protocol,1,9)..storage)),string.len(protocol))),"w")
  192.         file.write(mens)
  193.         file.close()
  194.                 rednet.send(id,"ok","confirmNewRecipe"..storage)
  195.         end
  196.         if protocol==("DelRecCra"..storage) then
  197.                 if exist(mens) then
  198.             fs.delete(mens)
  199.         end
  200.                 rednet.broadcast("ok","confirmDeleteRecipe"..storage)
  201.         end
  202.     end
  203.     loadwork()
  204. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement