Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Script: Zombie Piglin Hunter with Item Storage
- -- Description: Crafts sticks and stone sword, attacks zombie piglins, collects drops (rotten flesh, gold nuggets, ingots), and deposits them in a chest behind the turtle.
- 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
- 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
- local function craftSticks()
- print("Attempting to craft sticks...")
- turtle.select(5)
- if turtle.getItemCount(5) < 2 then
- print("Error: Need at least 2 planks in slot 5 to craft sticks.")
- return false
- end
- for i = 1, 2 do
- turtle.select(i)
- if turtle.getItemCount(i) > 0 then
- turtle.drop()
- end
- end
- turtle.select(5)
- turtle.transferTo(1, 1)
- turtle.transferTo(2, 1)
- if turtle.craft() then
- print("Sticks crafted successfully!")
- return true
- else
- print("Failed to craft sticks. Ensure 2 planks in slot 5.")
- return false
- end
- end
- local function craftStoneSword()
- print("Attempting to craft a stone sword...")
- turtle.select(4)
- if turtle.getItemCount(4) < 2 then
- print("Error: Need at least 2 cobblestone in slot 4.")
- return false
- end
- local stickSlot = findItem("minecraft:stick")
- if not stickSlot then
- if not craftSticks() then
- return false
- end
- stickSlot = 1
- end
- for i = 1, 3 do
- turtle.select(i)
- if turtle.getItemCount(i) > 0 then
- turtle.drop()
- end
- end
- turtle.select(4)
- turtle.transferTo(1, 1)
- turtle.transferTo(2, 1)
- turtle.select(stickSlot)
- turtle.transferTo(3, 1)
- if turtle.craft() then
- print("Stone sword crafted successfully!")
- return true
- else
- print("Failed to craft sword. Ensure correct items and slots.")
- return false
- end
- end
- local function equipSword()
- turtle.select(1)
- local item = turtle.getItemDetail(1)
- 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 1.")
- return false
- end
- end
- 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
- local function hasSword()
- local swordSlot = findItem("minecraft:stone_sword")
- if swordSlot then
- return true
- end
- return false
- end
- 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 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. Stopping.")
- break
- 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
- print("Zombie Piglin Hunter v2.0")
- if craftStoneSword() and equipSword() then
- huntZombiePiglins()
- else
- print("Failed to initialize. Check inventory (slot 4: cobblestone, slot 5: planks) and try again.")
- end
Add Comment
Please, Sign In to add comment