Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Server performant version
- --this script is a server performant (but slower) version
- --a adapter has to be connected to a chest and a refinedstorage exporter
- --just insert any item you want in the chest and wait until the script says that the recipe has been registered
- --the count in the chest is the count the autocrafter will craft for you as a example: put 64 sticks in the chest, after that
- --autocrafter will always try to craft to 64 sticks for you (if there is less than 64 sticks in your refined storage)
- --The amount of crafting co-workers
- local coworker = 1
- --The max allowed tasks in refined storage
- local maxTasks = 4
- --
- function StringSeperate(str, seperator)
- local words = {}
- local word = ""
- if(string.sub(str,str:len(),str:len())~="sperator")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 .. string.sub(str,x,x)
- end
- end
- return words
- end
- local recipeList = {}
- local f = io.open("recipes", "ab")
- for line in io.lines("recipes") do
- local tab = StringSeperate(line, "|")
- 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.1)
- 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()
- local component = require("component")
- local rs = component.block_refinedstorage_exporter
- local inventory = component.inventory_controller
- local side = require("sides")
- local craftingQueue = {}
- 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
- 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
- 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 CheckRequirements(pattern, recipe)
- if(recipe.require~=nil)then
- if(#recipe.require>0)then
- for k,v in pairs(recipe.require)do
- local item = rs.getItem(v)
- if(v.minAmount > item.size)then
- return false
- end
- end
- end
- end
- return true
- end
- function CheckCrafting(pattern)
- local recipe = GetRecipe(pattern)
- if(recipe~=nil)then
- local item = rs.getItem(pattern)
- local amount = 0
- if(item~=nil)then
- amount = GetAmount(item.size, recipe)
- else
- amount = GetAmount(0, recipe)
- end
- if(amount > 0)then
- --if(CheckRequirements(pattern, recipe))then
- table.insert(craftingQueue, {item=pattern, amount=amount})
- --end
- end
- end
- return false
- end
- function CheckItems()
- for k,v in pairs(rs.getPatterns())do
- if(type(v)=="table")then
- CheckCrafting(v)
- os.sleep(0.25)
- end
- end
- end
- function FindKeyInTable(table, item)
- for k,v in pairs(table)do
- if(v==item)then
- return k
- end
- end
- return nil
- end
- function IsTaskActive(task)
- for k,v in pairs(rs.getTasks())do
- if(type(v)=="table")then
- if(v.stack.item.name==task.stack.item.name)and(v.stack.item.damage==task.stack.item.damage)then
- return true
- end
- end
- end
- return false
- 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
- function UpdateCraftingQueue()
- while(#craftingQueue > 0)do
- local activeCoWorkers = {}
- local coworkerAmount = 0
- local craftingQueuesToRemove = {}
- for k,v in pairs(craftingQueue)do
- if(coworkerAmount+1 <= coworker)then
- local recipeKey = GetRecipeKey(v.item)
- 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(v.item.label=="Ultra Ball")then
- if(os.time()>=recipeList[recipeKey].timer+waittimer)then
- print("Time: "..os.time().." "..recipeList[recipeKey].timer+waittimer.." true")
- else
- print("Time: "..os.time().." "..recipeList[recipeKey].timer+waittimer.." false")
- end
- end]]
- if(os.time()>=recipeList[recipeKey].timer+waittimer)then
- local task, info = pcall(rs.scheduleTask, v.item, v.amount)
- if(task)then
- print("Scheduled Task: "..v.amount.." "..v.item.label)
- if(info.missing.n > 0)then
- local missingItems = ""
- for a,b in pairs(info.missing)do
- if(type(b)=="table")then
- missingItems = missingItems .. b.size.."x "..b.label.." "
- end
- end
- print("Missing Items: "..missingItems.." ")
- recipeList[recipeKey].fails = recipeList[recipeKey].fails + 1
- recipeList[recipeKey].timer = os.time()
- else
- table.insert(activeCoWorkers, info)
- coworkerAmount = coworkerAmount + 1
- recipeList[recipeKey].fails = 0
- end
- else
- print("------------------")
- print("Error scheduling task: "..v.amount.." "..v.item.label)
- print("Error message: " ..info)
- print("Recipe got removed from the recipe list")
- print("------------------")
- RemoveRecipe(v.item)
- 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(IsTaskActive(v) ~= true)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
- 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
- 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, 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.." 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)
- end
- end
- if(somethingChanged)then
- SaveToFile()
- end
- end
- end
- print("Autocrafter successfully loaded...")
- while true do
- CheckChestForNewEntrys()
- os.sleep(1)
- if(#rs.getTasks() < maxTasks)then
- CheckItems()
- os.sleep(1)
- UpdateCraftingQueue()
- os.sleep(1)
- else
- os.sleep(5)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement