Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local function turn(direction, inverted)
- if direction == "left" then
- if inverted then
- turtle.turnRight()
- else
- turtle.turnLeft()
- end
- elseif direction == "right" then
- if inverted then
- turtle.turnLeft()
- else
- turtle.turnRight()
- end
- end
- end
- local function readInventory(inventory)
- local data = { slots = {}, totals = {} }
- for i=1, inventory.size() do
- local details = inventory.getItemDetail(i)
- if details ~= nil then
- if data.slots[details.name] == nil then
- data.slots[details.name] = {{ slot = i, count = details.count }}
- else
- table.insert(data.slots[details.name], { slot = i, count = details.count })
- end
- if data.totals[details.name] == nil then
- data.totals[details.name] = details.count
- else
- data.totals[details.name] = data.totals[details.name] + details.count
- end
- end
- end
- return data
- end
- local function pullItems(side, count)
- if side == "up" or side == "top" then
- turtle.suckUp(count)
- elseif side == "down" or side == "bottom" then
- turtle.suckDown(count)
- end
- end
- local function turnTo(direction, inverted, name)
- while true do
- local exists, details = turtle.inspect()
- if exists and details ~= nil and details.name == name then
- break
- end
- turn(direction, inverted)
- end
- end
- local function craftLoop(data)
- local storage = peripheral.wrap("front")
- local inventory = readInventory(storage)
- for k,v in ipairs(data.ingredients) do
- if inventory.totals[v.name] == nil or inventory.totals[v.name] < v.count then
- return
- end
- end
- local localSlots = {}
- local currentSlot = 1
- for k1, ingredient in ipairs(data.ingredients) do
- local count = ingredient.count
- for k2, slot in ipairs(inventory.slots[ingredient.name]) do
- if count <= 0 then
- break
- end
- local moveAmount = math.min(64, count, slot.count)
- storage.pushItems(data.secondaryInventory, slot.slot, moveAmount, 1)
- turtle.select(currentSlot)
- pullItems(data.secondaryInventory, moveAmount)
- count = count - moveAmount
- end
- table.insert(localSlots, { slot = currentSlot, count = ingredient.count })
- currentSlot = currentSlot + 1
- end
- turnTo(data.turnDirection, false, data.craftingFluid)
- for k,v in ipairs(localSlots) do
- turtle.select(v.slot)
- turtle.drop(v.count)
- end
- sleep(data.delay)
- turtle.select(16)
- turtle.suck()
- turnTo(data.turnDirection, true, data.primaryInventory)
- turtle.drop()
- end
- local args = { ... }
- local file = fs.open(args[1], "r")
- local data = textutils.unserializeJSON(file.readAll())
- file.close()
- local ingredientCount = #data.ingredients
- if ingredientCount >= 16 then
- print("Only recipes with 15 or fewer ingredients are supported.")
- else
- turnTo(data.turnDirection, true, data.primaryInventory)
- for i=1, 16 do
- turtle.select(i)
- turtle.drop()
- end
- turtle.select(1)
- while true do
- craftLoop(data)
- end
- end
Add Comment
Please, Sign In to add comment