Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local recipes = {}
- local rSlots = {1,2,3,5,6,7,9,10,11}
- local hSlots = {4,8,12,13,14,15}
- local chest = {input = "up", trash = "front", final = "down"}
- local pause = false
- function init()
- if fs.exists("recipes") then
- f = fs.open("recipes","r")
- recipies = textutils.unserialize(f.readAll())
- f.close()
- else
- f = fs.open("recipes","w")
- f.write(textutils.serialize(recipes))
- f.close()
- end
- end
- function wl(str,posW,posH)
- local l = math.floor(string.len(str)/2)
- term.setCursorPos(posW-l,posH)
- term.write(str)
- end
- function save(path,data)
- f = fs.open(path,"w")
- f.write(textutils.serialize(data))
- f.close()
- end
- function dropItems(place,c)
- if not c then c = 64 end
- if place == "up" then
- turtle.dropUp(c)
- elseif place == "down" then
- turtle.dropDown(c)
- elseif place == "front" then
- turtle.drop(c)
- end
- end
- function suckItems(place,c)
- if not c then c = 64 end
- if place == "up" then
- turtle.suckUp(c)
- elseif place == "down" then
- turtle.suckDown(c)
- elseif place == "front" then
- turtle.suck(c)
- end
- end
- function distribute()
- local slot = turtle.getSelectedSlot()
- local data = turtle.getItemDetail(slot)
- if data then
- local required = {}
- local c = data.count
- for k,v in pairs(recipes[1].grid) do
- if v == data.name then
- table.insert(required, {k,turtle.getItemCount(k)})
- end
- end
- table.sort(required, function(a, b) return a[2] < b[2] end)
- for k,v in pairs(required) do
- required[k][2] = required[#required][2] - required[k][2]
- c = c - required[k][2]
- end
- if c > 0 then
- if c % #required ~= 0 then
- dropItems(chest.input,c % #required)
- end
- for k,v in pairs(required) do
- required[k][2] = math.floor(required[k][2] + (c/#required))
- end
- end
- for k,v in pairs(required) do
- turtle.select(slot)
- turtle.transferTo(required[k][1],required[k][2])
- end
- end
- if turtle.getItemCount(slot) > 0 then
- turtle.select(slot)
- dropItems(chest.trash)
- end
- end
- function checkForItems()
- turtle.select(16)
- suckItems(chest.input)
- if turtle.getItemCount(16) > 0 then
- distribute()
- return true
- else
- sleep(delay)
- return false
- end
- end
- function checkRecipe()
- local good = true
- for k,v in pairs(recipes[1]) do
- local data = turtle.getItemDetail(k)
- if data then
- if data.name ~= v and data.count > 0 then
- turtle.select(k)
- dropItems(chest.input)
- good = false
- end
- else
- good = false
- end
- end
- return good
- end
- function attemptCraft()
- for k,v in pairs(hSlots) do
- if turtle.getItemCount(v) > 0 then
- turtle.select(v)
- dropItems(chest.input)
- end
- end
- turtle.select(16)
- if turtle.craft() then
- dropItems(chest.final)
- if checkRecipe() then
- attemptCraft()
- end
- return true
- end
- end
- function newRecipe()
- while true do
- term.clear()
- term.setCursorPos(10,14)
- term.write("Press [TAB] To exit")
- term.setCursorPos(1,1)
- term.write("Press [SPACE] when ready")
- e,key = os.pullEvent("key")
- if key == keys.space then
- local tmpRecipe = {}
- tmpRecipe.grid = {}
- for k,v in pairs(rSlots) do
- local data = turtle.getItemDetail(v)
- if data then
- tmpRecipe.grid[v] = data.name
- end
- end
- turtle.select(16)
- if turtle.craft() then
- local data = turtle.getItemDetail(16)
- tmpRecipe.name = data.name
- print("SUCCESS")
- for k,v in pairs(tmpRecipe.grid) do
- print("[" .. k .. "] =" .. v)
- end
- print(tmpRecipe.name)
- print("Press [ENTER] to save")
- print("or press any other key to not save")
- e,key = os.pullEvent("key")
- if key == keys.enter then
- table.insert(recipes,1,tmpRecipe)
- save("recipes", recipes)
- print("Saved")
- sleep(3)
- return
- else
- print("Not Saved")
- sleep(3)
- end
- else
- print("INVALID RECIPE")
- sleep(3)
- end
- elseif key == keys.tab then
- return
- end
- end
- end
- function main()
- while true do
- term.clear()
- wl("---CRAFT BOT BOI---", 20,1)
- wl("Press [ENTER] to add new recipe",20,7)
- if recipes[1] then
- wl(recipes[1].name,20,3)
- if checkRecipe() then
- attemptCraft()
- else
- if not pause then
- checkForItems()
- else
- newRecipe()
- pause = false
- end
- end
- else
- newRecipe()
- end
- end
- end
- function waitForKey()
- while true do
- e,key = os.pullEvent("key")
- if key == keys.enter then
- pause = true
- end
- end
- end
- init()
- parallel.waitForAll(main,waitForKey)
Add Comment
Please, Sign In to add comment