Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local recipes = {}
- local shortcuts = {}
- local chestInfo = {}
- local dictionary = {}
- function getDict() return dictionary end
- function searchRecipes(item,dmg)
- if recipes[item] then
- if recipes[item].damage == dmg then return true end
- end
- return false
- end
- function setChest(direction,turtleDir)
- chestInfo = {dir = direction,tDir = turtleDir}
- end
- function getChest()
- return chestInfo
- end
- function addRecipe(result,resultCount,nickname,dmg)
- dmg = dmg or 0
- if nickname then dictionary[nickname] = {id = result,damage = dmg}
- recipes[result] = {total = resultCount,damage = dmg}
- recipes[result].items = {}
- end
- for i= 1, 11 do
- if i ~= 4 and i ~= 8 then
- local data = turtle.getItemDetail(i)
- if data ~= nil then
- 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
- table.insert(recipes[result].items[data.name..data.damage].slots,i)
- end
- end
- end
- for name, info in pairs(recipes[result].items) do
- local count = 0
- for o in pairs(info.slots) do count = count + 1
- recipes[result].items[info.name..info.damage].count = count
- end
- end
- end
- function search(direction,item,dmg)
- local damage = dmg or 0
- local inv = peripheral.wrap(direction)
- local list = inv.list()
- for i=1, inv.size() do
- if list[i] ~= nil then
- if list[i].name == item and list[i].damage == damage then return {slot = i, count = list[i].count} end
- end
- end
- return nil
- end
- function isFull(direction)
- local inv = peripheral.wrap(direction)
- local list = inv.list()
- for i = 1, inv.size() do
- if list[i] == nil then return false end
- end
- return true
- end
- function searchSelf(item,damage)
- for i=1, 16 do
- local temp = turtle.getItemDetail(i)
- if temp ~= nil then
- if temp.name == item and temp.damage == damage then return i end
- end
- end
- return nil
- end
- function craft(ammountPre,resultPre,dmg)
- if search(chestInfo.dir,result) then local resultSlot = search(chestInfo.dir,result).slot end
- local chest = peripheral.wrap(chestInfo.dir)
- local pulls = {}
- local result = 'nil'
- local damage = 'nil'
- if dictionary[resultPre] then
- result = dictionary[resultPre].id
- damage = dictionary[resultPre].damage
- else
- result = resultPre
- damage = dmg or 0
- end
- if not searchRecipes(result,damage) then
- print(result,ammountPre,damage)
- return {bool = false item =result,count = ammountPre,damage = damage}
- end
- if isFull(chestInfo.dir) and resultSlot == nil then
- print('inventory full')
- return false
- end
- local ammount = math.ceil(ammountPre/recipes[result].total)
- for name, info in pairs(recipes[result].items) do
- local searchInfo = search(chestInfo.dir,info.name,info.damage)
- while searchInfo.count < (info.count*ammount) do
- if not craft(info.name,info.count*ammount-count,info.damage).bool then return false end
- searchInfo = search(chestInfo.dir,info.name,info.damage)
- end
- pulls[info.name..info.damage] ={name = info.name,slot = searchInfo.slot,count = info.count,damage = info.damage}
- end
- for name, data in pairs(pulls) do
- for a=1, ammount do chest.pushItems(chestInfo.tDir,data.slot,data.count) end
- turtle.select(searchSelf(data.name,data.damage))
- for i2, toSlot in pairs(recipes[result].items[data.name..data.damage].slots) do
- if slot ~= toSlot then turtle.transferTo(toSlot,ammount) end
- end
- end
- turtle.select(16)
- for a=1, ammount do
- turtle.craft()
- chest.pullItems(chestInfo.tDir,16,64,resultSlot)
- if turtle.getItemDetail(16) ~= nil then chest.pullItems(chestInfo.tDir,16,64) end
- end
- return {bool=true}
- end
Add Comment
Please, Sign In to add comment