antrobot123

craftAPI

Jun 17th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.72 KB | None | 0 0
  1. local recipes = {}
  2. local shortcuts = {}
  3. local chestInfo = {}
  4. local dictionary = {}
  5.  
  6. function getDict() return dictionary end
  7.  
  8. function searchRecipes(item,dmg)
  9.         if recipes[item] then
  10.             if recipes[item].damage == dmg then return true end
  11.         end
  12.     return false
  13. end
  14. function setChest(direction,turtleDir)
  15.     chestInfo = {dir = direction,tDir = turtleDir}
  16. end
  17. function getChest()
  18.     return chestInfo
  19. end
  20. function addRecipe(result,resultCount,nickname,dmg)
  21.     dmg = dmg or 0
  22.     if nickname then dictionary[nickname] = {id = result,damage = dmg}
  23.         recipes[result] = {total = resultCount,damage = dmg}
  24.         recipes[result].items = {}
  25.     end
  26.     for i= 1, 11 do
  27.         if i ~= 4 and i ~= 8 then
  28.             local data = turtle.getItemDetail(i)
  29.             if data ~= nil then
  30.                 if not recipes[result].items[data.name..data.damage] then recipes[result].items[data.name..data.damage] = {name=data.name,damage = data.damage,slots = {}} end
  31.                 table.insert(recipes[result].items[data.name..data.damage].slots,i)
  32.             end
  33.         end
  34.     end
  35.     for name, info in pairs(recipes[result].items) do
  36.         local count = 0
  37.         for o in pairs(info.slots) do count = count + 1
  38.         recipes[result].items[info.name..info.damage].count = count
  39.         end
  40.     end
  41. end
  42.  
  43. function search(direction,item,dmg)
  44.     local damage = dmg or 0
  45.     local inv = peripheral.wrap(direction)
  46.     local list = inv.list()
  47.     for i=1, inv.size() do
  48.         if list[i] ~= nil then
  49.             if list[i].name == item and list[i].damage == damage then return {slot = i, count = list[i].count} end
  50.         end
  51.     end
  52.     return nil
  53. end
  54. function isFull(direction)
  55.     local inv = peripheral.wrap(direction)
  56.     local list = inv.list()
  57.     for i = 1, inv.size() do
  58.         if list[i] == nil then return false end
  59.     end
  60.     return true
  61. end
  62.  
  63.  
  64. function searchSelf(item,damage)
  65.     for i=1, 16 do
  66.         local temp = turtle.getItemDetail(i)
  67.         if temp ~= nil then
  68.             if temp.name == item and temp.damage == damage then return i end
  69.         end
  70.     end
  71.     return nil
  72. end
  73.  
  74. function craft(ammountPre,resultPre,dmg)
  75.     if search(chestInfo.dir,result) then local resultSlot = search(chestInfo.dir,result).slot end
  76.     local chest = peripheral.wrap(chestInfo.dir)
  77.     local pulls = {}
  78.     local result = 'nil'
  79.     local damage = 'nil'
  80.     if dictionary[resultPre] then
  81.         result = dictionary[resultPre].id
  82.         damage = dictionary[resultPre].damage
  83.     else
  84.     result = resultPre
  85.     damage = dmg or 0
  86.     end
  87.    
  88.     if not searchRecipes(result,damage) then
  89.         print(result,ammountPre,damage)
  90.         return {bool = false item =result,count = ammountPre,damage = damage}
  91.     end
  92.     if isFull(chestInfo.dir) and resultSlot == nil then
  93.         print('inventory full')
  94.         return false
  95.     end
  96.    
  97.     local ammount = math.ceil(ammountPre/recipes[result].total)
  98.    
  99.     for name, info in pairs(recipes[result].items) do
  100.         local searchInfo = search(chestInfo.dir,info.name,info.damage)
  101.         while searchInfo.count < (info.count*ammount) do
  102.             if not craft(info.name,info.count*ammount-count,info.damage).bool then return false end
  103.             searchInfo = search(chestInfo.dir,info.name,info.damage)
  104.         end
  105.         pulls[info.name..info.damage] ={name = info.name,slot = searchInfo.slot,count = info.count,damage = info.damage}
  106.     end
  107.     for name, data in pairs(pulls) do
  108.         for a=1, ammount do chest.pushItems(chestInfo.tDir,data.slot,data.count) end
  109.         turtle.select(searchSelf(data.name,data.damage))
  110.         for i2, toSlot in pairs(recipes[result].items[data.name..data.damage].slots) do
  111.             if slot ~= toSlot then turtle.transferTo(toSlot,ammount) end
  112.         end
  113.     end
  114.     turtle.select(16)
  115.     for a=1, ammount do
  116.         turtle.craft()
  117.         chest.pullItems(chestInfo.tDir,16,64,resultSlot)
  118.         if turtle.getItemDetail(16) ~= nil then chest.pullItems(chestInfo.tDir,16,64) end
  119.     end
  120.     return {bool=true}
  121. end
Add Comment
Please, Sign In to add comment