Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- print("Loading autocrafter...")
- local component = require("component")
- local ae2 = component.me_interface
- local inventory = component.inventory_controller
- local side = require("sides")
- local maxTaskCount = 4
- local maxCoWorker = 1
- local recipeList = {}
- local craftingQueue = {}
- local function StringSeperate(str, seperator)
- local words = {}
- local word = ""
- if(string.sub(str, str:len(), str:len())~=seperator)then
- str = str.. "" .. seperator
- end
- for x=1,str:len() do
- local s = string.sub(str,x,x)
- if(s == seperator)then
- table.insert(words, word)
- word = ""
- else
- word = word .. s
- end
- end
- return words
- end
- local f = io.open("recipes", "ab")
- for line in io.lines("recipes")do
- local tab = StringSeperate(line, "|")
- --print("asd"..tab[1])
- if(tab[1]~=nil)and(tab[2]~=nil)and(tab[3]~=nil)and(tab[4]~=nil)and(tab[5]~=nil)then
- print("Registered recipe: "..tab[3].."x "..tab[1]..":"..tab[2])
- os.sleep(0.2)
- local recipe = {name=tab[1],damage=tonumber(tab[2]),minAmount=tonumber(tab[3]),maxCraftAmount=tonumber(tab[4]),fails=0,timer=0}
- if(tab[5]=="true")then
- recipe.useDmg = true
- else
- recipe.useDmg = false
- end
- table.insert(recipeList, recipe)
- end
- end
- f:close()
- function SaveToFile()
- local file = io.open("recipes", "wb")
- for k,v in pairs(recipeList)do
- if(v.useDmg)then
- file:write(v.name.."|"..v.damage.."|"..v.minAmount.."|"..v.maxCraftAmount.."|true|", "\n")
- else
- file:write(v.name.."|"..v.damage.."|"..v.minAmount.."|"..v.maxCraftAmount.."|false|", "\n")
- end
- end
- file:close()
- end
- local function GetActiveTaskCount()
- local n = 0
- for k,v in pairs(ae2.getCpus())do
- if(type(v)=="table")then
- if(v.busy==true)then
- n = n+1
- end
- end
- end
- return n
- end
- local function GetAvailableCpuCount()
- return ae2.getCpus() - GetActiveTaskCount()
- end
- local function GetRecipe(pattern)
- for k,v in pairs(recipeList)do
- if(v.name==pattern.name)then
- if(v.useDmg)then
- if(v.damage == pattern.damage)then
- return v
- end
- else
- return v
- end
- end
- end
- return nil
- end
- local function GetStoredItemAmountNetwork(recipe)
- local count = 0
- for k,v in pairs(ae2.getItemsInNetwork())do
- if(type(v)=="number")then break end
- if(v.name == recipe.name)then
- if(recipe.useDmg)then
- if(v.damage == recipe.damage)then
- count = count + v.size
- end
- else
- count = count + v.size
- end
- end
- end
- return count
- end
- local function GetAmount(itemAmount, recipe)
- local amount = 0
- if(itemAmount < recipe.minAmount)then
- if(recipe.maxCraftAmount > 0)then
- if(recipe.minAmount-itemAmount > recipe.maxCraftAmount)then
- amount = recipe.maxCraftAmount
- else
- amount = recipe.minAmount-itemAmount
- end
- else
- amount = recipe.minAmount-itemAmount
- end
- end
- return amount
- end
- function GetRecipeKey(pattern)
- for k,v in pairs(recipeList)do
- if(v.name == pattern.name)then
- if(v.useDmg)then
- if(v.damage == pattern.damage)then
- return k
- end
- else
- return k
- end
- end
- end
- return nil
- end
- local function CheckCraftingRecipe(pattern)
- local stack = pattern.getItemStack()
- local recipe = GetRecipe(stack)
- if(recipe~=nil)then
- local storedAmount = GetStoredItemAmountNetwork(recipe)
- local neededItemAmount = GetAmount(storedAmount, recipe)
- if(neededItemAmount > 0)then
- table.insert(craftingQueue, {item=pattern, amount=neededItemAmount})
- return true
- end
- end
- return false
- end
- local function CheckAllRecipes()
- for k,v in pairs(ae2.getCraftables())do
- if(type(v)=="number")then break end
- CheckCraftingRecipe(v)
- end
- end
- function RemoveRecipe(item)
- local key = -1
- for k,v in pairs(recipeList)do
- if(v.name == item.name)and(v.damage== item.damage)then
- key = k
- end
- end
- if(key>=0)then
- table.remove(recipeList, key)
- end
- SaveToFile()
- end
- local function GetCraftable(item)
- for k,v in pairs(ae2.getCraftables())do
- if(item.useDmg)then
- if(v.getItemStack().name==item.name)then
- if(item.useDmg)then
- if(v.damage==item.damage)then
- return v
- end
- end
- return v
- end
- end
- end
- end
- local function FindKeyInTable(table, item)
- for k,v in pairs(table)do
- if(v==item)then
- return k
- end
- end
- return nil
- end
- local function UpdateCraftingQueue()
- while(#craftingQueue > 0)do
- local activeCoWorkers = {}
- local coworkerAmount = 0
- local craftingQueuesToRemove = {}
- for k,v in pairs(craftingQueue)do
- if(coworkerAmount+1 <= maxCoWorker)then
- local stack = v.item.getItemStack()
- local recipeKey = GetRecipeKey(stack)
- local waittimer = 0
- local multiplier = 1
- if(recipeList[recipeKey].fails > 0)and(recipeList[recipeKey].fails <=10)then
- multiplier = recipeList[recipeKey].fails
- elseif(recipeList[recipeKey].fails > 10)then
- multiplier = 10
- end
- waittimer = multiplier * 3000
- if(os.time()>=recipeList[recipeKey].timer+waittimer)then
- local task = v.item.request(v.amount)
- if(task~=nil)then
- print("Scheduled Task: "..v.amount.." "..stack.label)
- table.insert(activeCoWorkers, task)
- coworkerAmount = coworkerAmount + 1
- recipeList[recipeKey].fails = 0
- else
- print("------------------")
- print("Error scheduling task: "..v.amount.." "..stack.label)
- print("Recipe got removed from the recipe list")
- print("------------------")
- RemoveRecipe(stack)
- end
- end
- table.insert(craftingQueuesToRemove, v)
- end
- end
- if(#craftingQueuesToRemove > 0)then
- for k,v in pairs(craftingQueuesToRemove)do
- local id = FindKeyInTable(craftingQueue, v)
- table.remove(craftingQueue, id)
- end
- end
- while(#activeCoWorkers > 0)do
- local finishedCoworker = {}
- for k,v in pairs(activeCoWorkers)do
- if(v.isDone() or v.isCanceled())then
- table.insert(finishedCoworker, v)
- end
- end
- if(#finishedCoworker>0)then
- for k,v in pairs(finishedCoworker)do
- local id = FindKeyInTable(activeCoWorkers, v)
- table.remove(activeCoWorkers, id)
- end
- end
- os.sleep(0.75)
- end
- os.sleep(0.75)
- end
- end
- function FindKeyWithItemName(table, itemname, damage)
- for k,v in pairs(table)do
- if(v.name==itemname)and(v.damage == damage)then
- return k
- end
- end
- return nil
- end
- local function CheckChestForNewEntrys()
- local items = {}
- local itemAmounts = {}
- local sides = {0,1,2,3,4,5}
- local somethingChanged = false
- if(inventory~=nil)then
- for k,v in pairs(sides)do
- local chest = inventory.getAllStacks(v)
- if(chest~=nil)then
- local chestItems = chest.getAll()
- for a,b in pairs(chestItems)do
- if(b.name~="minecraft:air")then
- table.insert(items, b)
- end
- os.sleep(0.5)
- end
- end
- os.sleep(1)
- end
- end
- if(#items > 0)then
- for k,v in pairs(items)do
- local key = FindKeyWithItemName(itemAmounts, v.name, v.damage)
- if(key~=nil)then
- itemAmounts[key].size = itemAmounts[key].size + v.size
- else
- table.insert( itemAmounts, {name=v.name, damage=v.damage, label = v.label, size = v.size})
- end
- end
- end
- if(#itemAmounts > 0)then
- for k,v in pairs(itemAmounts)do
- local key = FindKeyWithItemName(recipeList, v.name, v.damage)
- if(key~=nil)then
- if(recipeList[key].minAmount ~= v.size)then
- print("Edited recipe: "..v.name.. ":"..v.damage.." ("..v.label..") new count: "..v.size)
- end
- somethingChanged = true
- recipeList[key].minAmount = v.size
- else
- table.insert( recipeList, {name=v.name, damage=v.damage, minAmount = v.size, maxCraftAmount = 0, useDmg = true, fails = 0, timer = 0})
- somethingChanged = true
- print("Registered recipe: "..v.size.."x "..v.name.. ":"..v.damage.." ("..v.label..")")
- end
- end
- if(somethingChanged)then
- SaveToFile()
- end
- end
- end
- print("Autocrafter successfully loaded!")
- while true do
- CheckChestForNewEntrys()
- os.sleep(1)
- if(GetActiveTaskCount()<maxTaskCount)then
- CheckAllRecipes()
- os.sleep(1)
- UpdateCraftingQueue()
- else
- os.sleep(5)
- end
- os.sleep(1)
- end
Add Comment
Please, Sign In to add comment