Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ---@diagnostic disable: undefined-global
- local input = { ... }
- local size = 5
- local depth = 0
- local chestName = 'minecraft:chest'
- local coalName = 'minecraft:coal'
- local bedrockName = 'minecraft:bedrock'
- local chestSlot = 15
- local coalSlot = 16
- -- start of input logic
- if input[1] == nil then
- print("Usage: 'mine' 'size' ['coalslot'] ['chestslot']")
- print("The Coal has to be in the last slot (16) and at least 2 chests should be in the second last slot (15) or one in the slot and one already be placed on top of the turtle.")
- end
- if input[1] ~= nil and tonumber(input[1]) <= 0 then
- print("Size is 0 or negative!")
- return
- end
- if input[1] ~= nil and tonumber(input[1]) > 0 then
- size = tonumber(input[1])
- end
- if size == nil or size <= 0 then
- print("Size: " .. size .. "\n" .. "Is simply invalid!")
- return
- end
- if input[2] ~= nil then
- if tonumber(input[2]) >= 2 and tonumber(input[2]) <= 16 then
- coalSlot = tonumber(input[2])
- end
- end
- if input[3] ~= nil then
- if tonumber(input[3]) >= 2 and tonumber(input[3]) <= 16 then
- if tonumber(input[3]) ~= coalSlot then
- chestSlot = tonumber(input[3])
- end
- end
- end
- -- end of input logic
- -- Stacks all of the coal found in the local inventory to slot 16
- local function coalSetup()
- for slot = 1, 16, 1 do
- local slotData = turtle.getItemDetail(slot)
- if slotData then
- if slotData.name == coalName then
- turtle.select(slot)
- turtle.transferTo(coalSlot)
- end
- end
- end
- turtle.select(1)
- end
- -- Dumps the local inventory into the starting chest
- local function dumpInv()
- local success, data = turtle.inspectUp()
- if data.name == chestName then
- for slot = 1, 16, 1 do
- turtle.select(slot)
- local slotData = turtle.getItemDetail(slot)
- if slotData then
- if slotData.name ~= chestName and slotData.name ~= coalName then
- if turtle.dropUp() then
- -- HIER: turtle.dropUp() only checks if dropUp() worked NOT if the chest is full.
- -- the chest is not full!
- -- else the chest is full.
- else
- print("Chest above is full!")
- os.shutdown()
- end
- end
- end
- end
- else
- local chestInInv = false
- local validSlot
- for slot = 1, 16, 1 do
- local slotData = turtle.getItemDetail(slot)
- if slotData then
- if slotData.name == chestName then
- chestInInv = true
- validSlot = slot
- end
- end
- end
- if chestInInv then
- turtle.select(validSlot)
- turtle.placeUp()
- dumpInv()
- else
- print("No Chest either above the turtle or in the turtle inventory. Aborted!")
- return
- end
- end
- turtle.select(1)
- end
- -- After an execution of the function 'excavate()' the turtle goes to the origin of the coordinates (aka where the chest is / supposed to be)
- local function goToOrigin()
- if size % 2 == 1 then
- turtle.turnLeft()
- for i = 1, size-1, 1 do
- turtle.forward()
- end
- turtle.turnLeft()
- for i = 1, size-1, 1 do
- turtle.forward()
- end
- turtle.turnLeft()
- turtle.turnLeft()
- else
- turtle.turnRight()
- for i = 1, size-1, 1 do
- turtle.forward()
- end
- turtle.turnRight()
- end
- end
- -- The turtle goes back up to the top, where (ideally) the chest is located.
- local function goBackUp()
- for i = 1, depth, 1 do
- turtle.up()
- end
- end
- -- The turtle goes bakc down, where (ideally) the turtle can resume excavating.
- local function goBackDown()
- for i = 1, depth, 1 do
- turtle.down()
- end
- end
- -- excavates an area the size of 'size' (default: 5) and immediately after runs goToOrigin() and goes 1 deeper
- local function excavate()
- for i = 1, size, 1 do
- for j = 1, size-1, 1 do
- turtle.digDown()
- turtle.forward()
- end
- turtle.digDown()
- if i ~= size then
- if i % 2 == 1 then
- turtle.turnRight()
- turtle.forward()
- turtle.turnRight()
- else
- turtle.turnLeft()
- turtle.forward()
- turtle.turnLeft()
- end
- end
- end
- goToOrigin()
- depth = depth + 1
- turtle.down()
- end
- -- This function is needed for the function 'searchForCoal()'.
- -- It either directly drops the items in slot 1 into a second chest placed under the turtle or finds a chest in inventory and places it and then drops the items.
- -- If no chest is found abort.
- local function chestUnderTurtle()
- local success, data = turtle.inspectDown()
- if data.name == chestName then
- turtle.dropDown()
- -- if the chest is not already placed, search for a chest in the inventory of the turtle
- -- if no chest is found abort.
- else
- for slot = 1, 16, 1 do
- local slotData1 = turtle.getItemDetail(slot)
- if slotData1 then
- if slotData1.name == chestName then
- chestInInv = true
- validSlot = slot
- end
- end
- end
- if chestInInv then
- turtle.select(validSlot)
- turtle.placeDown()
- turtle.select(1)
- turtle.dropDown()
- else
- print("No Chest either under the turtle or in the turtle inventory. Aborted!")
- return
- end
- end
- end
- -- Cycles all of the items of the chest under the turtle back up to the chest above the turtle.
- local function cycleItemsUp()
- while turtle.suckDown() do
- turtle.select(1)
- turtle.dropUp()
- end
- turtle.digDown()
- local chestInInv = false
- local validSlot
- for slot = 1, 16, 1 do
- local slotData = turtle.getItemDetail(slot)
- if slotData then
- if slotData.name == chestName then
- chestInInv = true
- validSlot = slot
- end
- end
- end
- if chestInInv then
- turtle.transferTo(validSlot)
- else
- turtle.transferTo(chestSlot)
- end
- end
- -- Searches for Coal in an above chest
- local function searchForCoal()
- local foundNoCoal = true
- while foundNoCoal and turtle.suckUp() do
- turtle.select(1)
- local slotData = turtle.getItemDetail()
- if slotData then
- if slotData.name == coalName then
- turtle.transferTo(14)
- if turtle.getItemCount(14) > 32 then
- foundNoCoal = false
- end
- -- else the slot does not contain coal
- else
- chestUnderTurtle()
- end
- end
- end
- coalSetup()
- cycleItemsUp()
- end
- -- Looks at the fuel level and decides wether to continue, just refuel or needs to gobackUp() to suck fuel out of the chest or (worst case) terminate if no fuel is available.
- local function refuel()
- local safetyLvl = 20
- -- if fuelLvl is in critical range
- if turtle.getFuelLevel() < size*size + depth + safetyLvl then
- coalSetup()
- turtle.select(coalSlot)
- turtle.refuel()
- turtle.select(1)
- -- if after setting up fuel the fuel is still critical gobackUp() and search for fuel in chest
- if turtle.getFuelLevel() < size*size + depth + safetyLvl then
- goBackUp()
- dumpInv()
- searchForCoal()
- turtle.select(coalSlot)
- turtle.refuel()
- turtle.select(1)
- -- if even after searching for fuel in the chest the fuel is still at critical Level, need to abort.
- if turtle.getFuelLevel() < size*size + depth + safetyLvl then
- print("FuelLevel: " .. turtle.getFuelLevel() .. " ist still critically low.\nNeed to abort!" )
- os.shutdown()
- -- else need to goBackDown() and resume mining
- else
- goBackDown()
- end
- end
- end
- end
- -- Detects bedrock under the turtle, meaning the turtle is finished.
- local function reachedBedrock()
- local success, data = turtle.inspectDown()
- if success then
- if data.name == bedrockName then
- return true
- end
- end
- return false
- end
- -- This function does all the work if the turtle is full.
- local function turtleIsFullHandler()
- local numbOfNonEmptySlots = 0
- for slot = 1, 16, 1 do
- if turtle.getItemCount(slot) ~= 0 then
- numbOfNonEmptySlots = numbOfNonEmptySlots + 1
- end
- end
- if numbOfNonEmptySlots > 14 then
- goBackUp()
- dumpInv()
- refuel()
- goBackDown()
- end
- end
- -- main program
- local function main()
- while reachedBedrock() == false do
- refuel()
- turtleIsFullHandler()
- excavate()
- end
- goBackUp()
- dumpInv()
- coalSetup()
- end
- -- testing area
- main()
- print("Finished!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement