Advertisement
hornedcommando

Minecraft Turtle stairUp

May 2nd, 2024
795
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.18 KB | Gaming | 0 0
  1. --Desc: A turtle program which attempts to stair up through blocks, number of stairs defined by the user
  2. -- Useful for making exits to mineshafts or traversing the nether
  3. --By: hornedcommando and BadPunBananas
  4.  
  5. --TODO: currently it is hardcoded to only place dirtblocks, should be updated to a dict with at least cobble and netherack
  6.  
  7. local function input()
  8.     while true do
  9.         write("How Many Stairs?\n")
  10.         size = tonumber(read())
  11.         break
  12.     end
  13. end
  14.  
  15. local function isForbiddenBlock(blockName)
  16.     local forbiddenBlocks = {
  17.         "forbidden_arcanus:stella_arcanum",
  18.         "minecraft:bedrock"
  19.     }
  20.     for _, forbiddenBlock in ipairs(forbiddenBlocks) do
  21.         if blockName == forbiddenBlock then
  22.             return true
  23.         end
  24.     end
  25.     return false
  26. end
  27.  
  28. function searchInventory(name)
  29.     for slot = 1, 16 do
  30.         turtle.select(slot)
  31.         local slotDetail = turtle.getItemDetail()
  32.         if slotDetail and slotDetail.name:find(name) then
  33.             return slot -- Return the slot number if item found
  34.         end
  35.     end
  36.     return nil -- Return nil if item not found
  37. end
  38.  
  39. function fill(name)
  40.     local found, slot = searchInventory(name)
  41.     if found then
  42.         turtle.placeDown()  -- Place the block
  43.         print("Placed block:", name)
  44.     else
  45.         print("Block not found:", name)
  46.     end
  47. end
  48.  
  49. --Function to refuel the turtle, attempts to maintain a specified fuel level, this could be adjusted based on abundance of fuel or time between fuel calls
  50. local function smartRefuel()
  51.     print("Checking Fuel")
  52.     -- Check if fuel level is below 1000
  53.     while turtle.getFuelLevel() < 1000 do
  54.         print("Refueling")
  55.         for slot = 1, 16 do
  56.             turtle.select(slot) -- Select the slot
  57.             if turtle.refuel(0) then
  58.                 -- Check if the selected item is a fuel
  59.                 turtle.refuel(1) -- Refuel with the selected item
  60.                 break -- Stop searching for fuel after refueling
  61.             end
  62.         end
  63.     end
  64. end
  65.  
  66. function gu()
  67.     print("Going up")
  68.     local success, block = turtle.inspectDown()
  69.     if isForbiddenBlock(block.name) then
  70.         print("Detected block is forbidden")
  71.         return false
  72.     else
  73.         local success, block = turtle.inspectUp()
  74.         if isForbiddenBlock(block.name) then
  75.             print("Detected block is forbidden")
  76.             return false
  77.         else
  78.             turtle.digUp()
  79.             while not turtle.up() do
  80.                 print("Digging up")
  81.                 turtle.digUp()
  82.             end
  83.             turtle.digUp()
  84.             return true
  85.         end
  86.     end
  87.  
  88. end
  89.  
  90. function gf()
  91.     print("Going forward")
  92.     local success, block = turtle.inspect()
  93.     if isForbiddenBlock(block.name) then
  94.         print("Detected block is forbidden")
  95.         return false
  96.     else
  97.         while not turtle.forward() do
  98.             print("Digging forward")
  99.             turtle.dig()
  100.         end
  101.         if not turtle.inspectDown() then
  102.             fill("minecraft:dirt")
  103.         end
  104.     end
  105. end
  106.  
  107. function stair()
  108.     for i = 1, size do
  109.         gu()
  110.         gf()
  111.     end
  112. end
  113.  
  114. input()
  115. smartRefuel()
  116. stair()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement