Advertisement
DabDaddy6223

single_miner

Apr 20th, 2025 (edited)
354
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.05 KB | None | 0 0
  1. ACCEPTABLE_FUEL = {"minecraft:coal"}
  2. FUEL_COUNT = -1
  3. FUEL_INDEX = -1
  4.  
  5. TORCHES_INDEX = -1
  6. TORCHES_COUNT = -1
  7. TORCHES_CURRENT = 0
  8. TORCHES_DISTANCE = 15
  9.  
  10. ACCEPTABLE_BLOCKS = {"minecraft:cobblestone", "minecraft:cobbled_deepslate", "minecraft:tuff", "minecraft:granite", "minecraft:andesite", "minecraft:diorite", "minecraft:dirt", "minecraft:sandstone"}
  11. DUMP_BLOCKS = {"minecraft:gravel", "minecraft:sand", "minecraft:flint"}
  12. BLOCK_INDEX = -1
  13. BLOCK_COUNT = 0
  14.  
  15. DUMP_TRASH = true
  16.  
  17. -- Utilities
  18. function arrHasValue(arr, val)
  19.     for index, value in ipairs(arr) do
  20.         if value == val then
  21.             return true
  22.         end
  23.     end
  24.  
  25.     return false
  26. end
  27.  
  28. -- Fuel
  29. function hasFuel()
  30.     return FUEL_COUNT > 0
  31. end
  32.  
  33. function shouldRefuel()
  34.     return turtle.getFuelLevel() <= 0
  35. end
  36.  
  37. function refuel()
  38.     if shouldRefuel() == true then
  39.         if hasFuel() ~= true then
  40.             return false
  41.         end
  42.  
  43.         turtle.select(1)
  44.         turtle.refuel(1)
  45.         FUEL_COUNT = FUEL_COUNT - 1
  46.     end
  47.  
  48.     return true
  49. end
  50.  
  51. -- Torches
  52. function canUseTorches()
  53.     return TORCHES_INDEX ~= -1
  54. end
  55.  
  56. function hasTorches()
  57.     return TORCHES_COUNT > 0
  58. end
  59.  
  60. function shouldPlaceTorch()
  61.     return TORCHES_CURRENT == TORCHES_DISTANCE
  62. end
  63.  
  64. function placeTorch()
  65.     turtle.select(TORCHES_INDEX)
  66.     turnAround()
  67.     turtle.place()
  68.     turnAround()
  69. end
  70.  
  71. function handleTorches()
  72.     if TORCHES_COUNT == 0 then
  73.         TORCHES_INDEX = -1
  74.     end
  75.  
  76.     if canUseTorches() == true then
  77.         if hasTorches() == true then
  78.             if shouldPlaceTorch() then
  79.                 placeTorch()
  80.                 TORCHES_CURRENT = 0
  81.             else
  82.                 TORCHES_CURRENT = TORCHES_CURRENT + 1
  83.             end
  84.             return true
  85.         else
  86.             return false
  87.         end
  88.     end
  89.  
  90.     return true
  91. end
  92.  
  93. -- Blocks
  94. function hasBlocks()
  95.     return BLOCK_COUNT > 0
  96. end
  97.  
  98. function findBlocks()
  99.     for i=1, 16 do
  100.         turtle.select(i)
  101.         local curr_slot_data = getSlotData(i)
  102.         if curr_slot_data ~= nil then
  103.             if arrHasValue(ACCEPTABLE_BLOCKS, curr_slot_data["name"]) then
  104.                 BLOCK_INDEX = i
  105.                 BLOCK_COUNT = curr_slot_data["count"]
  106.                 return true
  107.             end
  108.         end
  109.     end
  110.  
  111.     return false
  112. end
  113.  
  114. function handleBlocks()
  115.     if BLOCK_INDEX == -1 or BLOCK_COUNT == 0 then
  116.         if findBlocks() == false then
  117.             return
  118.         end
  119.     end
  120.  
  121.     local has_block, data = turtle.inspectDown()
  122.     if has_block == false then
  123.         turtle.select(BLOCK_INDEX)
  124.         turtle.placeDown()
  125.         BLOCK_COUNT = BLOCK_COUNT - 1
  126.     else
  127.         if data["name"] == "minecraft:lava" or data["name"] == "minecraft:water" then
  128.             turtle.select(BLOCK_INDEX)
  129.             turtle.placeDown()
  130.             BLOCK_COUNT = BLOCK_COUNT - 1
  131.         end
  132.     end
  133. end
  134.  
  135. -- Movement
  136. function turnAround()
  137.     turtle.turnLeft()
  138.     turtle.turnLeft()
  139. end
  140.  
  141. -- Inventory
  142. function getSlotData(index)
  143.     turtle.select(index)
  144.     local slotData = turtle.getItemDetail()
  145.     return slotData
  146. end
  147.  
  148. function getSlotName(index)
  149.     local data = getSlotData(index)
  150.     if data ~= nil then
  151.         return data["name"]
  152.     end
  153.     return nil
  154. end
  155.  
  156. function getSlotCount(index)
  157.     local data = getSlotData(index)
  158.     if data ~= nil then
  159.         return data["count"]
  160.     end
  161.     return -1
  162. end
  163.  
  164. function slotHasItem(index)
  165.     local data = getSlotData(index)
  166.     return data ~= nil
  167. end
  168.  
  169. function inventoryFull()
  170.     for i=1, 16 do
  171.         turtle.select(i)
  172.         local curr_slot_data = getSlotData(i)
  173.         if curr_slot_data == nil then
  174.             return false
  175.         end
  176.     end
  177.     return true
  178. end
  179.  
  180.  
  181.  
  182. function main()
  183.     -- Setup
  184.     for i=1, 16 do
  185.         local curr_slot_data = getSlotData(i)
  186.         if curr_slot_data ~= nil then
  187.             if FUEL_INDEX == -1 then
  188.                 if arrHasValue(ACCEPTABLE_FUEL, curr_slot_data["name"]) then
  189.                     FUEL_INDEX = i
  190.                     FUEL_COUNT = curr_slot_data["count"]
  191.                 end
  192.             end
  193.  
  194.             if TORCHES_INDEX == -1 then
  195.                 if curr_slot_data["name"] == "minecraft:torch" then
  196.                     TORCHES_INDEX = i
  197.                     TORCHES_COUNT = curr_slot_data["count"]
  198.                 end
  199.             end
  200.         end
  201.     end
  202.  
  203.     if FUEL_INDEX == -1 then
  204.         print("No fuel!")
  205.         return
  206.     end
  207.  
  208.     print("Fuel Index: " .. FUEL_INDEX)
  209.     print("Torch Index: " .. TORCHES_INDEX)
  210.  
  211.     -- Loop
  212.     while true do
  213.         if refuel() == false then
  214.             print("Ran out of fuel!")
  215.             return
  216.         end
  217.  
  218.         if inventoryFull() == true then
  219.             print("Inventory full!")
  220.  
  221.             if DUMP_TRASH == true then
  222.                 local items_dumped = false
  223.  
  224.                 turnAround()
  225.                 for i=1,16 do repeat
  226.                     if i == BLOCK_INDEX or i == TORCHES_DISTANCE or i == FUEL_INDEX then
  227.                         do break end
  228.                     end
  229.        
  230.                     local data = getSlotData(i)
  231.                     if data ~= nil then
  232.                         if arrHasValue(ACCEPTABLE_BLOCKS, data["name"]) == true then
  233.                             turtle.select(i)
  234.                             turtle.drop()
  235.                             items_dumped = true
  236.                         elseif arrHasValue(DUMP_BLOCKS, data["name"]) == true then
  237.                             turtle.select(i)
  238.                             turtle.drop()
  239.                             items_dumped = true
  240.                         end
  241.                     end
  242.                 until true end
  243.                 turnAround()
  244.  
  245.                 if items_dumped == false then
  246.                     return
  247.                 end
  248.             else
  249.                 return
  250.             end
  251.         end
  252.  
  253.         turtle.select(1)
  254.  
  255.         turtle.dig()
  256.         handleTorches()
  257.         turtle.forward()
  258.         handleBlocks()
  259.         turtle.digUp()
  260.     end
  261. end
  262.  
  263. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement