Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local function printClear(msg)
- term.clear()
- term.setCursorPos(1, 1)
- print(msg)
- end
- -- Clear the turtle's inventory by dropping all items into the inventory below
- local function clearInventory()
- for i = 1, 16 do
- turtle.select(i)
- while turtle.getItemCount(i) > 0 do
- if not turtle.dropDown() then
- printClear("Waiting for space in inventory below to clear slot", i)
- sleep(2)
- end
- end
- end
- end
- -- Pull 9 items from the inventory below (enough for one craft)
- local function getCraftingItems()
- clearInventory() -- just to be safe before fetching
- while true do
- turtle.select(1)
- if not turtle.suckDown(9) then
- printClear("Waiting for items...")
- sleep(2)
- else
- local item = turtle.getItemDetail(1)
- if item and item.count >= 9 then
- return
- else
- printClear("Not enough items pulled, retrying...")
- clearInventory()
- sleep(2)
- end
- end
- end
- end
- -- Arrange 9 items into the crafting grid: slots 1,2,3,5,6,7,9,10,11
- local function arrangeItemsForCraft()
- local craftingSlots = {
- [1] = true, [2] = true, [3] = true,
- [5] = true, [6] = true, [7] = true,
- [9] = true, [10] = true, [11] = true
- }
- -- Ensure at least 9 items in slot 1
- printClear("Crafting block")
- turtle.select(1)
- local itemDetail = turtle.getItemDetail(1)
- if not itemDetail or itemDetail.count < 9 then
- clearInventory()
- getCraftingItems()
- return arrangeItemsForCraft() -- Retry with fresh items
- end
- -- Distribute 1 item each to crafting grid slots
- local targetSlots = {2, 3, 5, 6, 7, 9, 10, 11}
- for _, slot in ipairs(targetSlots) do
- turtle.select(1)
- turtle.transferTo(slot, 1)
- end
- -- Cleanup: ensure each crafting slot has exactly 1 item, and dump extras elsewhere
- for slot = 1, 16 do
- turtle.select(slot)
- local item = turtle.getItemDetail(slot)
- if item then
- local count = item.count
- if craftingSlots[slot] then
- if count > 1 then
- turtle.dropDown(count - 1)
- end
- else
- -- Not part of the crafting grid: drop it
- turtle.dropDown()
- end
- end
- end
- return true
- end
- -- Craft the item (requires turtle with crafting upgrade)
- local function craftItem()
- if not turtle.craft() then
- printClear("Crafting failed")
- return false
- end
- return true
- end
- -- Place crafted item in front (avoid crafting slots to reduce error chance)
- local function placeCraftedItem()
- local craftingGrid = {
- [1] = true, [2] = true, [3] = true,
- [5] = true, [6] = true, [7] = true,
- [9] = true, [10] = true, [11] = true
- }
- for i = 1, 16 do
- if not craftingGrid[i] then
- turtle.select(i)
- if turtle.getItemCount(i) > 0 then
- if turtle.place() then
- return true
- end
- end
- end
- end
- printClear("Failed to place crafted item")
- return false
- end
- -- Wait until the block in front becomes a magma block, then dig it and store it above
- local function waitForMagma()
- while true do
- local success, data = turtle.inspect()
- if success and data.name == "minecraft:magma_block" then
- turtle.dig()
- -- Deposit magma block into inventory above
- for i = 1, 16 do
- turtle.select(i)
- local detail = turtle.getItemDetail(i)
- if detail and detail.name == "minecraft:magma_block" then
- turtle.dropUp()
- end
- end
- return
- else
- sleep(2)
- end
- end
- end
- -- Startup check: if there's already a block in front, handle it
- local function handleStartupBlock()
- local success, data = turtle.inspect()
- if success then
- if data.name == "minecraft:magma_block" then
- turtle.dig()
- for i = 1, 16 do
- turtle.select(i)
- local detail = turtle.getItemDetail(i)
- if detail and detail.name == "minecraft:magma_block" then
- turtle.dropUp()
- end
- end
- else
- -- Assume it's a crafted block still transforming
- printClear("Found block in front, waiting for it to become magma...")
- waitForMagma()
- end
- end
- end
- -- Main loop: run forever
- local success, data = turtle.inspect()
- if success and data.name == "minecraft:magma_block" then
- turtle.dig()
- -- Deposit magma block into inventory above
- for i = 1, 16 do
- turtle.select(i)
- local detail = turtle.getItemDetail(i)
- if detail and detail.name == "minecraft:magma_block" then
- turtle.dropUp()
- end
- end
- end
- handleStartupBlock()
- while true do
- clearInventory()
- getCraftingItems()
- arrangeItemsForCraft()
- if craftItem() then
- if placeCraftedItem() then
- waitForMagma()
- end
- end
- sleep(1)
- end
Add Comment
Please, Sign In to add comment