Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function suck(count, iter)
- local result = false
- local err = ""
- if iter % 2 == 0 then
- -- Suck gunpowder
- result, err = turtle.suckUp(count)
- else
- -- Suck sand
- result, err = turtle.suckDown(count)
- end
- if turtle.getItemCount(turtle.getSelectedSlot() + (iter % 3)) ~= count then
- result = false
- err = "Not enough resources."
- end
- if (iter + 1) % 3 == 0 then
- turtle.select(turtle.getSelectedSlot() + 4)
- end
- if not result then error(err) end
- end
- function clearInventory()
- for i = 1, 16, 1
- do
- turtle.select(i)
- turtle.drop(turtle.getItemCount(turtle.getSelectedSlot()))
- end
- turtle.select(1)
- return true
- end
- function toboolean(str)
- return str == "true"
- end
- function errorPrinter(err)
- print("Error:", err)
- end
- function waitForInput()
- print("Press enter to continue...")
- io.read()
- end
- function setup()
- print("Clearing inventory...")
- clearInventory()
- if redstone.getInput("back") then
- print("Starting crafting...")
- else
- print("Waiting for redstone input...")
- end
- end
- function mainLoop(itemType, craftAmount, waitForAmount, doCrafting)
- for i = 0, 8, 1
- do
- callSuccess, err = pcall(suck, craftAmount, i)
- if not callSuccess then
- -- Failure
- errorPrinter(err)
- if waitForAmount then
- os.sleep(60)
- else
- waitForInput()
- end
- error()
- end
- end
- turtle.select(1)
- success = doCrafting and turtle.craft()
- success = not doCrafting and clearInventory()
- success = doCrafting and print("Crafted " .. craftAmount .. " " .. itemType)
- turtle.drop(craftAmount)
- end
- local args = {...}
- local itemType = args[1]
- local craftAmount = tonumber(args[2])
- local waitForAmount = toboolean(args[3]) or true
- local doCrafting = toboolean(args[4]) or true
- local isCrafting = false
- setup()
- while true
- do
- if redstone.getInput("back") then
- if not isCrafting then
- print("Starting crafting...")
- isCrafting = true
- end
- if not pcall(mainLoop, itemType, craftAmount, waitForAmount, doCrafting) then
- setup()
- end
- else
- isCrafting = false
- os.sleep(0.1)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement