legg0028

magma_create

Aug 2nd, 2025 (edited)
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.35 KB | None | 0 0
  1. local function printClear(msg)
  2.     term.clear()
  3.     term.setCursorPos(1, 1)
  4.     print(msg)
  5. end
  6.  
  7. -- Clear the turtle's inventory by dropping all items into the inventory below
  8. local function clearInventory()
  9.     for i = 1, 16 do
  10.         turtle.select(i)
  11.         while turtle.getItemCount(i) > 0 do
  12.             if not turtle.dropDown() then
  13.                 printClear("Waiting for space in inventory below to clear slot", i)
  14.                 sleep(2)
  15.             end
  16.         end
  17.     end
  18. end
  19.  
  20. -- Pull 9 items from the inventory below (enough for one craft)
  21. local function getCraftingItems()
  22.     clearInventory()  -- just to be safe before fetching
  23.     while true do
  24.         turtle.select(1)
  25.         if not turtle.suckDown(9) then
  26.             printClear("Waiting for items...")
  27.             sleep(2)
  28.         else
  29.             local item = turtle.getItemDetail(1)
  30.             if item and item.count >= 9 then
  31.                 return
  32.             else
  33.                 printClear("Not enough items pulled, retrying...")
  34.                 clearInventory()
  35.                 sleep(2)
  36.             end
  37.         end
  38.     end
  39. end
  40.  
  41. -- Arrange 9 items into the crafting grid: slots 1,2,3,5,6,7,9,10,11
  42. local function arrangeItemsForCraft()
  43.     local craftingSlots = {
  44.         [1] = true, [2] = true, [3] = true,
  45.         [5] = true, [6] = true, [7] = true,
  46.         [9] = true, [10] = true, [11] = true
  47.     }
  48.  
  49.     -- Ensure at least 9 items in slot 1
  50.     printClear("Crafting block")
  51.     turtle.select(1)
  52.     local itemDetail = turtle.getItemDetail(1)
  53.     if not itemDetail or itemDetail.count < 9 then
  54.         clearInventory()
  55.         getCraftingItems()
  56.         return arrangeItemsForCraft()  -- Retry with fresh items
  57.     end
  58.  
  59.     -- Distribute 1 item each to crafting grid slots
  60.     local targetSlots = {2, 3, 5, 6, 7, 9, 10, 11}
  61.     for _, slot in ipairs(targetSlots) do
  62.         turtle.select(1)
  63.         turtle.transferTo(slot, 1)
  64.     end
  65.  
  66.     -- Cleanup: ensure each crafting slot has exactly 1 item, and dump extras elsewhere
  67.     for slot = 1, 16 do
  68.         turtle.select(slot)
  69.         local item = turtle.getItemDetail(slot)
  70.         if item then
  71.             local count = item.count
  72.             if craftingSlots[slot] then
  73.                 if count > 1 then
  74.                     turtle.dropDown(count - 1)
  75.                 end
  76.             else
  77.                 -- Not part of the crafting grid: drop it
  78.                 turtle.dropDown()
  79.             end
  80.         end
  81.     end
  82.  
  83.     return true
  84. end
  85.  
  86. -- Craft the item (requires turtle with crafting upgrade)
  87. local function craftItem()
  88.     if not turtle.craft() then
  89.         printClear("Crafting failed")
  90.         return false
  91.     end
  92.     return true
  93. end
  94.  
  95. -- Place crafted item in front (avoid crafting slots to reduce error chance)
  96. local function placeCraftedItem()
  97.     local craftingGrid = {
  98.         [1] = true, [2] = true, [3] = true,
  99.         [5] = true, [6] = true, [7] = true,
  100.         [9] = true, [10] = true, [11] = true
  101.     }
  102.  
  103.     for i = 1, 16 do
  104.         if not craftingGrid[i] then
  105.             turtle.select(i)
  106.             if turtle.getItemCount(i) > 0 then
  107.                 if turtle.place() then
  108.                     return true
  109.                 end
  110.             end
  111.         end
  112.     end
  113.  
  114.     printClear("Failed to place crafted item")
  115.     return false
  116. end
  117.  
  118. -- Wait until the block in front becomes a magma block, then dig it and store it above
  119. local function waitForMagma()
  120.     while true do
  121.         local success, data = turtle.inspect()
  122.         if success and data.name == "minecraft:magma_block" then
  123.             turtle.dig()
  124.             -- Deposit magma block into inventory above
  125.             for i = 1, 16 do
  126.                 turtle.select(i)
  127.                 local detail = turtle.getItemDetail(i)
  128.                 if detail and detail.name == "minecraft:magma_block" then
  129.                     turtle.dropUp()
  130.                 end
  131.             end
  132.             return
  133.         else
  134.             sleep(2)
  135.         end
  136.     end
  137. end
  138.  
  139. -- Startup check: if there's already a block in front, handle it
  140. local function handleStartupBlock()
  141.     local success, data = turtle.inspect()
  142.     if success then
  143.         if data.name == "minecraft:magma_block" then
  144.             turtle.dig()
  145.             for i = 1, 16 do
  146.                 turtle.select(i)
  147.                 local detail = turtle.getItemDetail(i)
  148.                 if detail and detail.name == "minecraft:magma_block" then
  149.                     turtle.dropUp()
  150.                 end
  151.             end
  152.         else
  153.             -- Assume it's a crafted block still transforming
  154.             printClear("Found block in front, waiting for it to become magma...")
  155.             waitForMagma()
  156.         end
  157.     end
  158. end
  159.  
  160. -- Main loop: run forever
  161. local success, data = turtle.inspect()
  162. if success and data.name == "minecraft:magma_block" then
  163.     turtle.dig()
  164.     -- Deposit magma block into inventory above
  165.     for i = 1, 16 do
  166.         turtle.select(i)
  167.         local detail = turtle.getItemDetail(i)
  168.         if detail and detail.name == "minecraft:magma_block" then
  169.             turtle.dropUp()
  170.         end
  171.     end
  172. end
  173.  
  174. handleStartupBlock()
  175. while true do
  176.     clearInventory()
  177.     getCraftingItems()
  178.     arrangeItemsForCraft()
  179.     if craftItem() then
  180.         if placeCraftedItem() then
  181.             waitForMagma()
  182.         end
  183.     end
  184.     sleep(1)
  185. end
  186.  
Add Comment
Please, Sign In to add comment