dadragon84

turtleWheatFarmHand

Mar 6th, 2025
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local function findItem(itemName)
  2.     for slot = 1, 16 do
  3.         local item = turtle.getItemDetail(slot)
  4.         if item and item.name == itemName then
  5.             return slot
  6.         end
  7.     end
  8.     return -1
  9. end
  10.  
  11. function findEmptySlot()
  12.     -- Iterate through all 16 slots
  13.     for slot = 1, 16 do
  14.         local item = turtle.getItemDetail(slot)
  15.         -- If the slot is empty (no item present), return the slot number
  16.         if not item then
  17.             return slot
  18.         end
  19.     end
  20.     -- If no empty slot is found, return -1 (or any other value indicating no empty slot)
  21.     return -1
  22. end
  23.  
  24. function consolidateInventory()
  25.     local nextSlot = 1 -- Start placing items in slot 1
  26.  
  27.     -- Loop through all slots to consolidate items
  28.     for slot = 1, 16 do
  29.         turtle.select(slot)
  30.         local item = turtle.getItemDetail()
  31.         if item then
  32.             -- Try to stack the item with the same type in earlier slots
  33.             for targetSlot = 1, nextSlot - 1 do
  34.                 turtle.select(slot)
  35.                 if turtle.compareTo(targetSlot) then
  36.                     turtle.transferTo(targetSlot)
  37.                 end
  38.             end
  39.  
  40.             -- If items remain, move them to the next available slot
  41.             if turtle.getItemCount(slot) > 0 and slot >= nextSlot then
  42.                 turtle.transferTo(nextSlot)
  43.                 nextSlot = nextSlot + 1
  44.             end
  45.         end
  46.     end
  47.  
  48.     -- Reset selection to the first slot
  49.     turtle.select(1)
  50. end
  51.  
  52. function countWheat()
  53.     local wheatName = "minecraft:wheat"
  54.     local totalWheat = 0
  55.  
  56.     for slot = 1, 16 do
  57.         local item = turtle.getItemDetail(slot)
  58.         if item and item.name == wheatName then
  59.             totalWheat = totalWheat + item.count
  60.         end
  61.     end
  62.  
  63.     return totalWheat
  64. end
  65.  
  66. -- Helper function to check if a value is in a table
  67. function contains(tbl, value)
  68.     for _, v in ipairs(tbl) do
  69.         if v == value then
  70.             return true
  71.         end
  72.     end
  73.     return false
  74. end
  75.  
  76. function craftHayBlocks()
  77.     local craftingSlots = {1, 2, 3, 5, 6, 7, 9, 10, 11}
  78.     local wheatName = "minecraft:wheat"
  79.  
  80.     -- Consolidate wheat into the first slots
  81.     consolidateInventory()
  82.  
  83.     -- Count total wheat
  84.     local totalWheat = countWheat()
  85.     if totalWheat < 9 then
  86.         print("Not enough wheat to craft a hay block.")
  87.         return false
  88.     end
  89.  
  90.     -- Calculate the amount of wheat needed per slot
  91.     local wheatPerSlot = math.floor(totalWheat / #craftingSlots)
  92.  
  93.     -- Step 1: Gather wheat from non-crafting slots to the crafting slots
  94.     for slot = 1, 16 do
  95.         local isCraftingSlot = false
  96.         -- Check if the slot is part of the crafting grid using a for loop
  97.         for _, craftingSlot in ipairs(craftingSlots) do
  98.             if slot == craftingSlot then
  99.                 isCraftingSlot = true
  100.                 break
  101.             end
  102.         end
  103.  
  104.         -- If it's not a crafting slot, we proceed
  105.         if not isCraftingSlot then
  106.             local item = turtle.getItemDetail(slot)
  107.             if item and item.name == wheatName then
  108.                 for _, targetSlot in ipairs(craftingSlots) do
  109.                     local targetItem = turtle.getItemDetail(targetSlot)
  110.                     local currentCount = targetItem and targetItem.count or 0
  111.                     local needed = wheatPerSlot - currentCount
  112.  
  113.                     -- Only transfer wheat if the target slot is not already filled
  114.                     if needed > 0 then
  115.                         turtle.select(slot)
  116.                         local toTransfer = math.min(needed, item.count)
  117.                         turtle.transferTo(targetSlot, toTransfer)
  118.                         item.count = item.count - toTransfer
  119.                         if item.count <= 0 then break end
  120.                     end
  121.                 end
  122.             end
  123.         end
  124.     end
  125.  
  126.  
  127.     -- Ensure each crafting slot has at least wheatPerSlot
  128. for _, slot in ipairs(craftingSlots) do
  129.     local item = turtle.getItemDetail(slot)
  130.     local currentCount = item and item.count or 0
  131.     local needed = wheatPerSlot - currentCount
  132.  
  133.     -- Only move wheat if the slot is underfilled (needed > 0)
  134.     if needed > 0 then
  135.         for sourceSlot = 1, 16 do
  136.             local sourceItem = turtle.getItemDetail(sourceSlot)
  137.             -- Only transfer wheat from slots that have more than wheatPerSlot
  138.             if sourceItem and sourceItem.name == wheatName and sourceItem.count > wheatPerSlot then
  139.                 turtle.select(sourceSlot)
  140.                 local toTransfer = math.min(needed, sourceItem.count - wheatPerSlot) -- Only move the excess
  141.                 turtle.transferTo(slot, toTransfer)
  142.                 needed = needed - toTransfer
  143.                 if needed <= 0 then break end
  144.             end
  145.         end
  146.     end
  147. end
  148.  
  149.     -- Craft hay block
  150.     turtle.select(1) -- Any filled slot in the crafting grid can be selected
  151.     if turtle.craft() then
  152.         print("Hay block crafted successfully!")
  153.         return true
  154.     else
  155.         print("Crafting failed. Check the setup.")
  156.         return false
  157.     end
  158. end
  159.  
  160.  
  161. slot = findEmptySlot()
  162. if slot == -1 then
  163.     print("Error: Empty Turtle")
  164.     os.exit(1)
  165. else
  166.     turtle.select(slot)
  167. end
  168.  
  169. while true do
  170.     if turtle.suckUp() then
  171.         local item = turtle.getItemDetail()
  172.         if item then
  173.             if item.name == "minecraft:wheat_seeds" then
  174.                 turtle.dropDown()
  175.             else
  176.                 craftHayBlocks()
  177.                 slot = findItem("minecraft:hay_block")
  178.                 if slot then
  179.                     slot = findItem("minecraft:hay_block")
  180.                     if slot ~= -1 then
  181.                         turtle.select(slot)
  182.                         turtle.drop()
  183.                     end
  184.                 end
  185.             end
  186.         end
  187.         slot = findEmptySlot()
  188.         if slot == -1 then
  189.             print("Error: Empty Turtle")
  190.             os.exit(1)
  191.         else
  192.             turtle.select(slot)
  193.         end
  194.     end
  195. end
Add Comment
Please, Sign In to add comment