Bmorr

chest_cleanup.lua

Apr 28th, 2023 (edited)
1,020
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.28 KB | None | 0 0
  1. inv = require("inventory_util")
  2.  
  3. --[[
  4.     pastebin get YrQEHQdQ chest_cleaner.lua
  5.     Start on chest to deposit in, facing AWAY from the chests you wish to unload.
  6. ]]
  7.  
  8. requirements = {
  9.     ["inventory_util.lua"] = "4bUbgNP7"
  10. }
  11.  
  12. for file, paste in pairs(requirements) do
  13.     print("Grabbing \""..file.."\"...")
  14.     if fs.exists(file) then
  15.         fs.delete(file)
  16.     end
  17.     shell.run("pastebin get "..paste.." "..file)
  18. end
  19.  
  20. function main(count)
  21.     count = count or 0
  22.  
  23.     print("Beginning loop #"..tostring(count))
  24.  
  25.     turtle.turnLeft()
  26.     turtle.turnLeft()
  27.     turtle.forward()
  28.     distance = 1
  29.  
  30.     -- This loop goes until it reaches the first chest.
  31.     exists, data = turtle.inspectDown()
  32.     while not exists or data.name ~= "minecraft:chest" do
  33.         turtle.forward()
  34.         exists, data = turtle.inspectDown()
  35.         distance = distance + 1
  36.     end
  37.     closest_chest = distance
  38.     print("Found closest chest!")
  39.  
  40.     -- Now every two blocks there better be another chest.
  41.     turtle.forward()
  42.     turtle.forward()
  43.     turtle.forward()
  44.     distance = distance + 3
  45.  
  46.     exists, data = turtle.inspectDown()
  47.     while exists and data.name == "minecraft:chest" do
  48.         turtle.forward()
  49.         turtle.forward()
  50.         turtle.forward()
  51.         distance = distance + 3
  52.  
  53.         exists, data = turtle.inspectDown()
  54.     end
  55.  
  56.     turtle.turnLeft()
  57.     turtle.turnLeft()
  58.  
  59.     distance = distance - 3
  60.     turtle.forward()
  61.     turtle.forward()
  62.     turtle.forward()
  63.  
  64.     closest_looted = -1
  65.  
  66.     while not inv.checkIfInventoryFull() and distance > 5 do
  67.         closest_looted = distance
  68.         if not turtle.suckDown() and not inv.checkIfInventoryFull() then
  69.             turtle.digDown()
  70.  
  71.             turtle.forward()
  72.             turtle.forward()
  73.             turtle.forward()
  74.             distance = distance - 3
  75.         end
  76.     end
  77.  
  78.     print("Inventory Full! Returning to drop-off...")
  79.  
  80.     turtle.up()
  81.  
  82.     for i = 1, distance do
  83.         turtle.forward()
  84.     end
  85.  
  86.     turtle.down()
  87.     exists, data = turtle.inspectDown()
  88.     if exists and data.name == "minecraft:chest" then
  89.         inv.emptyInventory("down")
  90.     end
  91.  
  92.     if closest_looted > 0 and closest_looted > closest_chest then
  93.         main(count + 1)
  94.     end
  95. end
  96.  
  97. main()
  98.  
  99.  
Advertisement
Add Comment
Please, Sign In to add comment