Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- version = 20230908.1400
- --[[
- https://pastebin.com/xmzXQpyR
- Last edited: see version YYYYMMDD.HHMM
- use ' pastebin get xmzXQpyR mobkiller.lua ' in turtle terminal
- For use on a Crafting Melee Turtle at the exit of a spawner farm
- ]]
- local function trim(text)
- --[[ trim leading and trailing spaces ]]
- return (text:gsub("^%s*(.-)%s*$", "%1"))
- end
- local function isRepairedBow(slot)
- -- eg bow: data.nbt = "552887824c43124013fd24f6edcde0fb"
- local data = turtle.getItemDetail(slot)
- if data ~= nil then
- if data.nbt == "552887824c43124013fd24f6edcde0fb" then -- fully repaired bow
- print("Repaired bow in slot "..slot)
- return true
- end
- end
- return false
- end
- local function clear()
- term.clear()
- term.setCursorPos(1, 1)
- end
- local function checkLabel()
- if os.getComputerLabel() == nil then
- local noname = true
- while noname do
- clear()
- print("Type a name for this computer (no spaces):_")
- name = read()
- if name == '' then
- print("Just pressing Enter does not work")
- elseif name:find(' ') ~= nil then
- print("NO SPACES!")
- else
- noname = false
- end
- if noname then
- sleep(2)
- end
- end
- os.setComputerLabel(name)
- print("Computer label set to "..os.getComputerLabel())
- end
- end
- local function getBlockType(direction)
- --[[ turtle.inspect() returns two values
- 1) boolean (true/false) success
- 2) table with two or more values:
- .name (string) e.g. "minecraft:log"
- .state {axis = "y"}
- .tags (["minecraft:logs"] = true, ["minecraft:logs_that_burn"] = true, ["minecraft:oak_logs"] = true}
- ]]
- local Inspect = turtle.inspect
- if direction == "top" then
- Inspect = turtle.inspectUp
- elseif direction == "bottom" then
- Inspect = turtle.inspectDown
- end
- local success, data = Inspect() -- store information about the block ahead in a table
- if success then -- block found
- return data.name, data -- eg "minecraft:chest", table
- end
- return "", {} -- eg "" , empty table
- end
- local function getAttackDirection()
- local directions = {"up", "forward", "down"} -- table of attack directions
- while true do -- display menu of choices
- clear() -- clear terminal
- print("Attack directions:")
- print(" 1. up")
- print(" 2. forward")
- print(" 3. down")
- write("Which direction? (1, 2 or 3) " )
- local response = read()
- if response == "1" or response == "2" or response == "3" then -- verified response
- return directions[tonumber(response)] -- attack direction returned
- end
- end
- end
- local function getMobType()
- local mobs = {"zombie", "skeleton", "spider", "blaze"} -- table of spawner mobs
- while true do -- display menu of choices
- clear() -- clear terminal
- print("Mob Spawner type:")
- print(" 1. Zombie")
- print(" 2. Skeleton")
- print(" 3. Spider")
- print(" 4. Blaze")
- write("Which mob? (1, 2, 3 or 4) " )
- local response = read()
- if response == "1" or response == "2" or response == "3" or response == "4" then
- return mobs[tonumber(response)] -- mob type returned
- end
- end
- end
- local function createStorage()
- local storage = {}
- storage.top = false
- storage.bottom = false
- storage.left = false
- storage.right = false
- storage.forward = false
- storage.back = false
- storage.onHopper = false
- local lib = {}
- function lib.addStorage(direction, position)
- local blockType = getBlockType(direction)
- if blockType:find("chest") ~= nil or blockType:find("barrel") ~= nil then
- storage[position] = true
- print("Storage found: "..position)
- print("storage."..position.. " = "..tostring(storage[position]))
- end
- end
- if getBlockType("down"):find("hopper") ~= nil then
- storage.onHopper = true
- print("Hopper found below.")
- print("Mob drops will fill it automatically")
- end
- -- direction can be "top", "bottom" or "forward" equivalent to Up(), Down() and ()
- -- position can be "top", "bottom", "forward" "left", "right", "back"
- lib.addStorage("top", "top")
- lib.addStorage("bottom", "bottom")
- lib.addStorage("forward", "forward")
- turtle.turnRight()
- lib.addStorage("forward", "right")
- turtle.turnRight()
- lib.addStorage("forward", "back")
- turtle.turnRight()
- lib.addStorage("forward", "left")
- turtle.turnRight()
- return storage
- end
- local function emptyInventory(storage)
- -- left and right storage for special items only
- local Drop = turtle.drop
- if storage.bottom then
- Drop = turtle.dropDown
- elseif storage.top then
- Drop = turtle.dropUp
- end
- for slot = 1, 16 do
- if turtle.getItemCount(slot) > 0 then
- turtle.select(slot)
- Drop()
- end
- end
- end
- local function processSkeletonFarm(storage, storageFull)
- turtle.select(1)
- local bowCount = 0
- local bowSlot = 0
- for slot = 1, 16 do
- if turtle.getItemCount(slot) > 0 then
- local data = turtle.getItemDetail(slot)
- if data.name:find("bow") == nil then -- not a bow
- turtle.select(slot)
- turtle.dropDown()
- else
- if slot == 4 or slot == 8 or slot >= 12 then
- turtle.select(slot)
- turtle.transferTo(9)
- bowSlot = 9
- end
- bowCount = bowCount + 1
- bowSlot = slot
- end
- end
- end
- -- may be 1 or more bows left in inventory
- if storageFull then
- print("Bow storage full")
- print("Refuelling with excess bows")
- while turtle.refuel() do end
- bowCount = 0
- end
- if bowCount == 2 then
- print("Attempting to repair bows")
- turtle.select(1)
- if turtle.craft() then
- if isRepairedBow(1) then
- print("Bow repair successful")
- if storage.left then
- turtle.turnLeft()
- if turtle.drop() then
- bowCount = 0
- else
- storageFull = true
- end
- turtle.turnRight()
- elseif storage.right then
- turtle.turnRight()
- if turtle.drop() then
- bowCount = 0
- else
- storageFull = true
- end
- turtle.turnLeft()
- else
- if turtle.dropDown() then
- bowCount = 0
- else
- storageFull = true
- end
- end
- else
- print("Partial bow repair successful")
- end
- end
- end
- if bowCount > 2 and bowSlot > 0 then
- turtle.select(bowSlot)
- turtle.refuel()
- turtle.select(1)
- bowSlot = 0
- end
- turtle.select(1)
- return storageFull
- end
- local function getDrops(storage)
- if not storage.forward then -- no storage in front
- turtle.suck()
- end
- if not storage.top then -- no storage above
- turtle.suckUp()
- end
- if not storage.bottom and not storage.onHopper then -- no storage or hopper below
- turtle.suckDown()
- end
- end
- local function attack(storage, direction, mobType)
- local Attack = turtle.attack
- if direction == "up" then
- Attack = turtle.attackUp
- elseif direction == "down" then
- Attack = turtle.attackDown
- end
- local count = 0
- local storageFull = false
- while true do
- if Attack() then
- count = count + 1
- clear()
- print("Attack direction "..direction.." count: "..count)
- end
- getDrops(storage)
- if storage.bottom then -- if chest or barrel below
- if mobType == "skeleton" then
- storageFull = processSkeletonFarm(storage, storageFull)
- else
- emptyInventory(storage)
- end
- end
- end
- end
- local function getConfig()
- if fs.exists("attack.txt") then
- local h = fs.open("attack.txt", "r")
- local attackDirection = trim(h.readLine())
- local mobType = trim(h.readLine())
- h.close()
- print("File 'attack.txt' read:")
- return attackDirection, mobType
- else
- return "", ""
- end
- end
- local function writeConfig(attackDirection, mobType)
- local h = fs.open("attack.txt", "w")
- h.writeLine(attackDirection) --
- h.writeLine(mobType) -- "zombie, "skeleton", "spider", "blaze"
- h.close()
- print("Config.txt file written")
- end
- local function main()
- clear()
- checkLabel() -- make sure turtle label is set
- local storage = createStorage() -- create and populate storage table
- local attackDirection, mobType = getConfig() -- if attack.txt is available
- if attackDirection == "" then -- not in a text file
- attackDirection = getAttackDirection() -- get attackDirection
- mobType = getMobType() -- get mob spawner type
- writeConfig(attackDirection, mobType) -- write to text file
- end
- print("Attack direction: "..attackDirection) -- display to player fo 3 seconds
- print("Mob spawner type: "..mobType)
- sleep(3)
- attack(storage, attackDirection, mobType) -- run attack loop
- end
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement