Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local function findItem(itemName)
- for slot = 1, 16 do
- local item = turtle.getItemDetail(slot)
- if item and item.name == itemName then
- return slot
- end
- end
- return -1
- end
- function findEmptySlot()
- -- Iterate through all 16 slots
- for slot = 1, 16 do
- local item = turtle.getItemDetail(slot)
- -- If the slot is empty (no item present), return the slot number
- if not item then
- return slot
- end
- end
- -- If no empty slot is found, return -1 (or any other value indicating no empty slot)
- return -1
- end
- function consolidateInventory()
- local nextSlot = 1 -- Start placing items in slot 1
- -- Loop through all slots to consolidate items
- for slot = 1, 16 do
- turtle.select(slot)
- local item = turtle.getItemDetail()
- if item then
- -- Try to stack the item with the same type in earlier slots
- for targetSlot = 1, nextSlot - 1 do
- turtle.select(slot)
- if turtle.compareTo(targetSlot) then
- turtle.transferTo(targetSlot)
- end
- end
- -- If items remain, move them to the next available slot
- if turtle.getItemCount(slot) > 0 and slot >= nextSlot then
- turtle.transferTo(nextSlot)
- nextSlot = nextSlot + 1
- end
- end
- end
- -- Reset selection to the first slot
- turtle.select(1)
- end
- function countWheat()
- local wheatName = "minecraft:wheat"
- local totalWheat = 0
- for slot = 1, 16 do
- local item = turtle.getItemDetail(slot)
- if item and item.name == wheatName then
- totalWheat = totalWheat + item.count
- end
- end
- return totalWheat
- end
- -- Helper function to check if a value is in a table
- function contains(tbl, value)
- for _, v in ipairs(tbl) do
- if v == value then
- return true
- end
- end
- return false
- end
- function craftHayBlocks()
- local craftingSlots = {1, 2, 3, 5, 6, 7, 9, 10, 11}
- local wheatName = "minecraft:wheat"
- -- Consolidate wheat into the first slots
- consolidateInventory()
- -- Count total wheat
- local totalWheat = countWheat()
- if totalWheat < 9 then
- print("Not enough wheat to craft a hay block.")
- return false
- end
- -- Calculate the amount of wheat needed per slot
- local wheatPerSlot = math.floor(totalWheat / #craftingSlots)
- -- Step 1: Gather wheat from non-crafting slots to the crafting slots
- for slot = 1, 16 do
- local isCraftingSlot = false
- -- Check if the slot is part of the crafting grid using a for loop
- for _, craftingSlot in ipairs(craftingSlots) do
- if slot == craftingSlot then
- isCraftingSlot = true
- break
- end
- end
- -- If it's not a crafting slot, we proceed
- if not isCraftingSlot then
- local item = turtle.getItemDetail(slot)
- if item and item.name == wheatName then
- for _, targetSlot in ipairs(craftingSlots) do
- local targetItem = turtle.getItemDetail(targetSlot)
- local currentCount = targetItem and targetItem.count or 0
- local needed = wheatPerSlot - currentCount
- -- Only transfer wheat if the target slot is not already filled
- if needed > 0 then
- turtle.select(slot)
- local toTransfer = math.min(needed, item.count)
- turtle.transferTo(targetSlot, toTransfer)
- item.count = item.count - toTransfer
- if item.count <= 0 then break end
- end
- end
- end
- end
- end
- -- Ensure each crafting slot has at least wheatPerSlot
- for _, slot in ipairs(craftingSlots) do
- local item = turtle.getItemDetail(slot)
- local currentCount = item and item.count or 0
- local needed = wheatPerSlot - currentCount
- -- Only move wheat if the slot is underfilled (needed > 0)
- if needed > 0 then
- for sourceSlot = 1, 16 do
- local sourceItem = turtle.getItemDetail(sourceSlot)
- -- Only transfer wheat from slots that have more than wheatPerSlot
- if sourceItem and sourceItem.name == wheatName and sourceItem.count > wheatPerSlot then
- turtle.select(sourceSlot)
- local toTransfer = math.min(needed, sourceItem.count - wheatPerSlot) -- Only move the excess
- turtle.transferTo(slot, toTransfer)
- needed = needed - toTransfer
- if needed <= 0 then break end
- end
- end
- end
- end
- -- Craft hay block
- turtle.select(1) -- Any filled slot in the crafting grid can be selected
- if turtle.craft() then
- print("Hay block crafted successfully!")
- return true
- else
- print("Crafting failed. Check the setup.")
- return false
- end
- end
- slot = findEmptySlot()
- if slot == -1 then
- print("Error: Empty Turtle")
- os.exit(1)
- else
- turtle.select(slot)
- end
- while true do
- if turtle.suckUp() then
- local item = turtle.getItemDetail()
- if item then
- if item.name == "minecraft:wheat_seeds" then
- turtle.dropDown()
- else
- craftHayBlocks()
- slot = findItem("minecraft:hay_block")
- if slot then
- slot = findItem("minecraft:hay_block")
- if slot ~= -1 then
- turtle.select(slot)
- turtle.drop()
- end
- end
- end
- end
- slot = findEmptySlot()
- if slot == -1 then
- print("Error: Empty Turtle")
- os.exit(1)
- else
- turtle.select(slot)
- end
- end
- end
Add Comment
Please, Sign In to add comment