Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Script: Zombie Piglin Hunter with Chest Crafting (v2.3)
- -- Description: Moves items to a chest behind the turtle to craft sticks and stone sword, attacks zombie piglins, and deposits drops in the chest.
- -- Function to check if the turtle has enough fuel
- local function checkFuel()
- if turtle.getFuelLevel() < 50 then
- print("Low fuel! Please place fuel (e.g., coal) in slot 1 and press Enter.")
- turtle.select(1)
- os.pullEvent("key")
- if turtle.refuel() then
- print("Refueled. Current fuel level: " .. turtle.getFuelLevel())
- else
- print("Failed to refuel. Please add valid fuel.")
- return false
- end
- end
- return true
- end
- -- Function to validate chest behind the turtle
- local function validateChest()
- local success, data = turtle.inspectBack()
- if success and data.name == "minecraft:chest" then
- print("Chest detected behind turtle.")
- return true
- else
- print("Error: No chest found behind turtle. Please place a chest directly behind.")
- return false
- end
- end
- -- Function to move all items to chest, preserving crafting materials
- local function moveItemsToChest(excludeSlots)
- excludeSlots = excludeSlots or {}
- local exclude = {}
- for _, slot in ipairs(excludeSlots) do
- exclude[slot] = true
- end
- turtle.turnLeft()
- turtle.turnLeft()
- for i = 1, 16 do
- if not exclude[i] and turtle.getItemCount(i) > 0 then
- turtle.select(i)
- turtle.drop()
- print("Moved items from slot " .. i .. " to chest.")
- end
- end
- turtle.turnLeft()
- turtle.turnLeft()
- end
- -- Function to retrieve items from chest
- local function retrieveItemsFromChest()
- turtle.turnLeft()
- turtle.turnLeft()
- for i = 1, 16 do
- turtle.select(i)
- if turtle.getItemCount(i) == 0 then
- turtle.suck()
- if turtle.getItemCount(i) > 0 then
- print("Retrieved items to slot " .. i .. " from chest.")
- end
- end
- end
- turtle.turnLeft()
- turtle.turnLeft()
- end
- -- Function to find an item in the turtle's inventory
- local function findItem(itemName)
- for i = 1, 16 do
- local item = turtle.getItemDetail(i)
- if item and item.name == itemName then
- return i
- end
- end
- return nil
- end
- -- Function to craft sticks (1 plank in slot 1, 1 plank in slot 5)
- local function craftSticks()
- print("Attempting to craft sticks...")
- -- Check slot 5 (planks)
- turtle.select(5)
- local plankDetails = turtle.getItemDetail(5)
- if plankDetails then
- print("Slot 5 contains: " .. plankDetails.name .. " (Count: " .. plankDetails.count .. ")")
- else
- print("Error: Slot 5 is empty.")
- return false
- end
- if turtle.getItemCount(5) < 2 then
- print("Error: Need at least 2 planks in slot 5.")
- return false
- end
- -- Move all items to chest except slot 5
- moveItemsToChest({5})
- -- Set up crafting grid (1 plank in slot 1, 1 plank in slot 5)
- turtle.select(5)
- turtle.transferTo(1, 1)
- turtle.transferTo(5, 1)
- -- Debug: Verify crafting grid
- 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"))
- -- Craft sticks
- local success = turtle.craft()
- local craftedItem = turtle.getItemDetail(1)
- if success and craftedItem and craftedItem.name == "minecraft:stick" then
- print("Sticks crafted successfully!")
- turtle.select(1)
- turtle.transferTo(6, turtle.getItemCount(1)) -- Move sticks to slot 6
- retrieveItemsFromChest()
- return true
- else
- print("Failed to craft sticks. Error: " .. (success and "No sticks produced" or "No matching recipes"))
- retrieveItemsFromChest()
- return false
- end
- end
- -- Function to craft a stone sword (1 cobblestone in slot 1, 1 cobblestone in slot 5, 1 stick in slot 9)
- local function craftStoneSword()
- print("Attempting to craft a stone sword...")
- -- Check slot 4 (cobblestone)
- turtle.select(4)
- local cobbleDetails = turtle.getItemDetail(4)
- if cobbleDetails then
- print("Slot 4 contains: " .. cobbleDetails.name .. " (Count: " .. cobbleDetails.count .. ")")
- else
- print("Error: Slot 4 is empty.")
- return false
- end
- if turtle.getItemCount(4) < 2 then
- print("Error: Need at least 2 cobblestone in slot 4.")
- return false
- end
- -- Check for sticks
- local stickSlot = findItem("minecraft:stick")
- if not stickSlot then
- print("No sticks found. Attempting to craft sticks...")
- if not craftSticks() then
- print("No sticks available and failed to craft them.")
- return false
- end
- stickSlot = 6
- end
- -- Move all items to chest except slot 4 (cobblestone) and stickSlot
- moveItemsToChest({4, stickSlot})
- -- Set up crafting grid (1 cobblestone in slot 1, 1 cobblestone in slot 5, 1 stick in slot 9)
- turtle.select(4)
- turtle.transferTo(1, 1)
- turtle.transferTo(5, 1)
- turtle.select(stickSlot)
- turtle.transferTo(9, 1)
- -- Debug: Verify crafting grid
- 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"))
- -- Craft sword
- local success = turtle.craft()
- local craftedItem = turtle.getItemDetail(1)
- if success and craftedItem and craftedItem.name == "minecraft:stone_sword" then
- print("Stone sword crafted successfully!")
- turtle.select(1)
- turtle.transferTo(4, 1) -- Move sword to slot 4
- retrieveItemsFromChest()
- return true
- else
- print("Failed to craft sword. Error: " .. (success and "No sword produced" or "No matching recipes"))
- retrieveItemsFromChest()
- return false
- end
- end
- -- Function to equip the sword
- local function equipSword()
- turtle.select(4)
- local item = turtle.getItemDetail(4)
- if item and item.name == "minecraft:stone_sword" then
- if turtle.equipLeft() then
- print("Stone sword equipped on left side.")
- return true
- else
- print("Failed to equip sword.")
- return false
- end
- else
- print("No stone sword found in slot 4.")
- return false
- end
- end
- -- Function to deposit items into the chest
- local function depositItems()
- turtle.turnLeft()
- turtle.turnLeft()
- local itemsToDeposit = {
- "minecraft:rotten_flesh",
- "minecraft:gold_nugget",
- "minecraft:gold_ingot"
- }
- for i = 1, 16 do
- local item = turtle.getItemDetail(i)
- if item then
- for _, targetItem in ipairs(itemsToDeposit) do
- if item.name == targetItem then
- turtle.select(i)
- turtle.drop()
- print("Deposited " .. item.name .. " into chest.")
- end
- end
- end
- end
- turtle.turnLeft()
- turtle.turnLeft()
- end
- -- Function to check if the sword is in inventory
- local function hasSword()
- local swordSlot = findItem("minecraft:stone_sword")
- if swordSlot then
- turtle.select(swordSlot)
- return true
- end
- return false
- end
- -- Main loop to detect, attack, and manage items
- local function huntZombiePiglins()
- print("Starting zombie piglin hunter...")
- while true do
- if not checkFuel() then
- print("Stopping due to low fuel.")
- break
- end
- if not validateChest() then
- print("Stopping due to missing chest.")
- break
- end
- if not hasSword() then
- print("No sword detected. Attempting to craft and equip new sword...")
- if not (craftStoneSword() and equipSword()) then
- print("Failed to craft/equip sword. Checking for existing sticks or sword...")
- local stickSlot = findItem("minecraft:stick")
- local swordSlot = findItem("minecraft:stone_sword")
- if swordSlot then
- print("Found stone sword in slot " .. swordSlot .. ". Equipping...")
- turtle.select(swordSlot)
- if equipSword() then
- print("Sword equipped. Continuing...")
- else
- print("Failed to equip sword. Please place a stone sword in slot 4.")
- break
- end
- elseif stickSlot then
- print("Found sticks in slot " .. stickSlot .. ". Please craft a stone sword manually and place it in slot 4.")
- break
- else
- print("No sticks or sword available. Please place a stone sword in slot 4 or ensure cobblestone (slot 4) and planks (slot 5).")
- break
- end
- end
- end
- if turtle.suck() then
- print("Picked up items. Depositing to chest...")
- depositItems()
- end
- local success, data = turtle.inspect()
- if success and data.name == "minecraft:zombie_piglin" then
- print("Zombie piglin detected! Attacking...")
- turtle.attack()
- else
- print("No zombie piglin in front. Waiting...")
- end
- os.sleep(1)
- end
- end
- -- Main program
- print("Zombie Piglin Hunter v2.3 (Chest Crafting)")
- if not validateChest() then
- print("Initialization failed: No chest behind turtle.")
- elseif craftStoneSword() and equipSword() then
- huntZombiePiglins()
- else
- print("Failed to initialize. Check inventory (slot 4: cobblestone, slot 5: planks) and ensure chest behind turtle.")
- end
Advertisement
Add Comment
Please, Sign In to add comment