MadCat_G

huntv2

Aug 14th, 2025
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.10 KB | Gaming | 0 0
  1. -- Script: Zombie Piglin Hunter with Chest Crafting (v2.3)
  2. -- Description: Moves items to a chest behind the turtle to craft sticks and stone sword, attacks zombie piglins, and deposits drops in the chest.
  3.  
  4. -- Function to check if the turtle has enough fuel
  5. local function checkFuel()
  6.     if turtle.getFuelLevel() < 50 then
  7.         print("Low fuel! Please place fuel (e.g., coal) in slot 1 and press Enter.")
  8.         turtle.select(1)
  9.         os.pullEvent("key")
  10.         if turtle.refuel() then
  11.             print("Refueled. Current fuel level: " .. turtle.getFuelLevel())
  12.         else
  13.             print("Failed to refuel. Please add valid fuel.")
  14.             return false
  15.         end
  16.     end
  17.     return true
  18. end
  19.  
  20. -- Function to validate chest behind the turtle
  21. local function validateChest()
  22.     local success, data = turtle.inspectBack()
  23.     if success and data.name == "minecraft:chest" then
  24.         print("Chest detected behind turtle.")
  25.         return true
  26.     else
  27.         print("Error: No chest found behind turtle. Please place a chest directly behind.")
  28.         return false
  29.     end
  30. end
  31.  
  32. -- Function to move all items to chest, preserving crafting materials
  33. local function moveItemsToChest(excludeSlots)
  34.     excludeSlots = excludeSlots or {}
  35.     local exclude = {}
  36.     for _, slot in ipairs(excludeSlots) do
  37.         exclude[slot] = true
  38.     end
  39.     turtle.turnLeft()
  40.     turtle.turnLeft()
  41.     for i = 1, 16 do
  42.         if not exclude[i] and turtle.getItemCount(i) > 0 then
  43.             turtle.select(i)
  44.             turtle.drop()
  45.             print("Moved items from slot " .. i .. " to chest.")
  46.         end
  47.     end
  48.     turtle.turnLeft()
  49.     turtle.turnLeft()
  50. end
  51.  
  52. -- Function to retrieve items from chest
  53. local function retrieveItemsFromChest()
  54.     turtle.turnLeft()
  55.     turtle.turnLeft()
  56.     for i = 1, 16 do
  57.         turtle.select(i)
  58.         if turtle.getItemCount(i) == 0 then
  59.             turtle.suck()
  60.             if turtle.getItemCount(i) > 0 then
  61.                 print("Retrieved items to slot " .. i .. " from chest.")
  62.             end
  63.         end
  64.     end
  65.     turtle.turnLeft()
  66.     turtle.turnLeft()
  67. end
  68.  
  69. -- Function to find an item in the turtle's inventory
  70. local function findItem(itemName)
  71.     for i = 1, 16 do
  72.         local item = turtle.getItemDetail(i)
  73.         if item and item.name == itemName then
  74.             return i
  75.         end
  76.     end
  77.     return nil
  78. end
  79.  
  80. -- Function to craft sticks (1 plank in slot 1, 1 plank in slot 5)
  81. local function craftSticks()
  82.     print("Attempting to craft sticks...")
  83.    
  84.     -- Check slot 5 (planks)
  85.     turtle.select(5)
  86.     local plankDetails = turtle.getItemDetail(5)
  87.     if plankDetails then
  88.         print("Slot 5 contains: " .. plankDetails.name .. " (Count: " .. plankDetails.count .. ")")
  89.     else
  90.         print("Error: Slot 5 is empty.")
  91.         return false
  92.     end
  93.     if turtle.getItemCount(5) < 2 then
  94.         print("Error: Need at least 2 planks in slot 5.")
  95.         return false
  96.     end
  97.  
  98.     -- Move all items to chest except slot 5
  99.     moveItemsToChest({5})
  100.  
  101.     -- Set up crafting grid (1 plank in slot 1, 1 plank in slot 5)
  102.     turtle.select(5)
  103.     turtle.transferTo(1, 1)
  104.     turtle.transferTo(5, 1)
  105.    
  106.     -- Debug: Verify crafting grid
  107.     print("Crafting grid: Slot 1 = " .. (turtle.getItemDetail(1) and turtle.getItemDetail(1).name or "empty") .. ", Slot 5 = " .. (turtle.getItemDetail(5) and turtle.getItemDetail(5).name or "empty"))
  108.  
  109.     -- Craft sticks
  110.     local success = turtle.craft()
  111.     local craftedItem = turtle.getItemDetail(1)
  112.     if success and craftedItem and craftedItem.name == "minecraft:stick" then
  113.         print("Sticks crafted successfully!")
  114.         turtle.select(1)
  115.         turtle.transferTo(6, turtle.getItemCount(1)) -- Move sticks to slot 6
  116.         retrieveItemsFromChest()
  117.         return true
  118.     else
  119.         print("Failed to craft sticks. Error: " .. (success and "No sticks produced" or "No matching recipes"))
  120.         retrieveItemsFromChest()
  121.         return false
  122.     end
  123. end
  124.  
  125. -- Function to craft a stone sword (1 cobblestone in slot 1, 1 cobblestone in slot 5, 1 stick in slot 9)
  126. local function craftStoneSword()
  127.     print("Attempting to craft a stone sword...")
  128.  
  129.     -- Check slot 4 (cobblestone)
  130.     turtle.select(4)
  131.     local cobbleDetails = turtle.getItemDetail(4)
  132.     if cobbleDetails then
  133.         print("Slot 4 contains: " .. cobbleDetails.name .. " (Count: " .. cobbleDetails.count .. ")")
  134.     else
  135.         print("Error: Slot 4 is empty.")
  136.         return false
  137.     end
  138.     if turtle.getItemCount(4) < 2 then
  139.         print("Error: Need at least 2 cobblestone in slot 4.")
  140.         return false
  141.     end
  142.  
  143.     -- Check for sticks
  144.     local stickSlot = findItem("minecraft:stick")
  145.     if not stickSlot then
  146.         print("No sticks found. Attempting to craft sticks...")
  147.         if not craftSticks() then
  148.             print("No sticks available and failed to craft them.")
  149.             return false
  150.         end
  151.         stickSlot = 6
  152.     end
  153.  
  154.     -- Move all items to chest except slot 4 (cobblestone) and stickSlot
  155.     moveItemsToChest({4, stickSlot})
  156.  
  157.     -- Set up crafting grid (1 cobblestone in slot 1, 1 cobblestone in slot 5, 1 stick in slot 9)
  158.     turtle.select(4)
  159.     turtle.transferTo(1, 1)
  160.     turtle.transferTo(5, 1)
  161.     turtle.select(stickSlot)
  162.     turtle.transferTo(9, 1)
  163.    
  164.     -- Debug: Verify crafting grid
  165.     print("Crafting grid: Slot 1 = " .. (turtle.getItemDetail(1) and turtle.getItemDetail(1).name or "empty") .. ", Slot 5 = " .. (turtle.getItemDetail(5) and turtle.getItemDetail(5).name or "empty") .. ", Slot 9 = " .. (turtle.getItemDetail(9) and turtle.getItemDetail(9).name or "empty"))
  166.  
  167.     -- Craft sword
  168.     local success = turtle.craft()
  169.     local craftedItem = turtle.getItemDetail(1)
  170.     if success and craftedItem and craftedItem.name == "minecraft:stone_sword" then
  171.         print("Stone sword crafted successfully!")
  172.         turtle.select(1)
  173.         turtle.transferTo(4, 1) -- Move sword to slot 4
  174.         retrieveItemsFromChest()
  175.         return true
  176.     else
  177.         print("Failed to craft sword. Error: " .. (success and "No sword produced" or "No matching recipes"))
  178.         retrieveItemsFromChest()
  179.         return false
  180.     end
  181. end
  182.  
  183. -- Function to equip the sword
  184. local function equipSword()
  185.     turtle.select(4)
  186.     local item = turtle.getItemDetail(4)
  187.     if item and item.name == "minecraft:stone_sword" then
  188.         if turtle.equipLeft() then
  189.             print("Stone sword equipped on left side.")
  190.             return true
  191.         else
  192.             print("Failed to equip sword.")
  193.             return false
  194.         end
  195.     else
  196.         print("No stone sword found in slot 4.")
  197.         return false
  198.     end
  199. end
  200.  
  201. -- Function to deposit items into the chest
  202. local function depositItems()
  203.     turtle.turnLeft()
  204.     turtle.turnLeft()
  205.     local itemsToDeposit = {
  206.         "minecraft:rotten_flesh",
  207.         "minecraft:gold_nugget",
  208.         "minecraft:gold_ingot"
  209.     }
  210.     for i = 1, 16 do
  211.         local item = turtle.getItemDetail(i)
  212.         if item then
  213.             for _, targetItem in ipairs(itemsToDeposit) do
  214.                 if item.name == targetItem then
  215.                     turtle.select(i)
  216.                     turtle.drop()
  217.                     print("Deposited " .. item.name .. " into chest.")
  218.                 end
  219.             end
  220.         end
  221.     end
  222.     turtle.turnLeft()
  223.     turtle.turnLeft()
  224. end
  225.  
  226. -- Function to check if the sword is in inventory
  227. local function hasSword()
  228.     local swordSlot = findItem("minecraft:stone_sword")
  229.     if swordSlot then
  230.         turtle.select(swordSlot)
  231.         return true
  232.     end
  233.     return false
  234. end
  235.  
  236. -- Main loop to detect, attack, and manage items
  237. local function huntZombiePiglins()
  238.     print("Starting zombie piglin hunter...")
  239.     while true do
  240.         if not checkFuel() then
  241.             print("Stopping due to low fuel.")
  242.             break
  243.         end
  244.         if not validateChest() then
  245.             print("Stopping due to missing chest.")
  246.             break
  247.         end
  248.         if not hasSword() then
  249.             print("No sword detected. Attempting to craft and equip new sword...")
  250.             if not (craftStoneSword() and equipSword()) then
  251.                 print("Failed to craft/equip sword. Checking for existing sticks or sword...")
  252.                 local stickSlot = findItem("minecraft:stick")
  253.                 local swordSlot = findItem("minecraft:stone_sword")
  254.                 if swordSlot then
  255.                     print("Found stone sword in slot " .. swordSlot .. ". Equipping...")
  256.                     turtle.select(swordSlot)
  257.                     if equipSword() then
  258.                         print("Sword equipped. Continuing...")
  259.                     else
  260.                         print("Failed to equip sword. Please place a stone sword in slot 4.")
  261.                         break
  262.                     end
  263.                 elseif stickSlot then
  264.                     print("Found sticks in slot " .. stickSlot .. ". Please craft a stone sword manually and place it in slot 4.")
  265.                     break
  266.                 else
  267.                     print("No sticks or sword available. Please place a stone sword in slot 4 or ensure cobblestone (slot 4) and planks (slot 5).")
  268.                     break
  269.                 end
  270.             end
  271.         end
  272.         if turtle.suck() then
  273.             print("Picked up items. Depositing to chest...")
  274.             depositItems()
  275.         end
  276.         local success, data = turtle.inspect()
  277.         if success and data.name == "minecraft:zombie_piglin" then
  278.             print("Zombie piglin detected! Attacking...")
  279.             turtle.attack()
  280.         else
  281.             print("No zombie piglin in front. Waiting...")
  282.         end
  283.         os.sleep(1)
  284.     end
  285. end
  286.  
  287. -- Main program
  288. print("Zombie Piglin Hunter v2.3 (Chest Crafting)")
  289. if not validateChest() then
  290.     print("Initialization failed: No chest behind turtle.")
  291. elseif craftStoneSword() and equipSword() then
  292.     huntZombiePiglins()
  293. else
  294.     print("Failed to initialize. Check inventory (slot 4: cobblestone, slot 5: planks) and ensure chest behind turtle.")
  295. end
Advertisement
Add Comment
Please, Sign In to add comment