Bmorr

inventory_util.lua

Apr 26th, 2023 (edited)
994
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.27 KB | None | 0 0
  1. function checkIfSlotIsItem(slot, name)
  2.     local item = turtle.getItemDetail(slot)
  3.     turtle.select(1)
  4.     if item ~= nil then
  5.         return item["name"] == name
  6.     end
  7.     return false
  8. end
  9.  
  10. function findItem(name)
  11.     for slot = 1, 16 do
  12.         if checkIfSlotIsItem(slot, name) then
  13.             return slot
  14.         end
  15.     end
  16.     return -1
  17. end
  18.  
  19. function checkIfHaveItem(name)
  20.     return findItem(name) ~= -1
  21. end
  22.  
  23. function findEmpty()
  24.     for index = 1, 16 do
  25.         if turtle.getItemCount(index) == 0 then
  26.             return index
  27.         end
  28.     end
  29.     return -1
  30. end
  31.  
  32. function countItems()
  33.     local total = 0
  34.     for index = 1, 16 do
  35.         total = turtle.getItemCount(index) + total
  36.     end
  37.     return total
  38. end
  39.  
  40. function checkIfInventoryFull()
  41.     return findEmpty() == -1
  42. end
  43.  
  44. drop_directions = {
  45.     ["down"] = turtle.dropDown,
  46.     ["up"] = turtle.dropUp,
  47.     ["forward"] = turtle.drop
  48. }
  49.  
  50. function emptyInventory(direction)
  51.     direction = direction or "forward"
  52.     direction = drop_directions[direction]
  53.     print("Dropping:")
  54.     for i = 1, 16 do
  55.         turtle.select(i)
  56.         local data = turtle.getItemDetail()
  57.         if data then
  58.             print(tostring(turtle.getItemCount()).."x "..data["name"])
  59.         end
  60.  
  61.         direction()
  62.     end
  63.     turtle.select(1)
  64. end
  65.  
  66. function refuel()
  67.     local fuelLevel = turtle.getFuelLevel()
  68.     if fuelLevel == "unlimited" or fuelLevel > 0 then
  69.         return
  70.     end
  71.  
  72.     function tryRefuel()
  73.         for n = 1, 16 do
  74.             if turtle.getItemCount(n) > 0 then
  75.                 turtle.select(n)
  76.                 if turtle.refuel(1) then
  77.                     turtle.select(1)
  78.                     return true
  79.                 end
  80.             end
  81.         end
  82.         turtle.select(1)
  83.         return false
  84.     end
  85.  
  86.     if not tryRefuel() then
  87.         print("Add more fuel to continue.")
  88.         while not tryRefuel() do
  89.             os.pullEvent("turtle_inventory")
  90.         end
  91.         print("Resuming Tunnel.")
  92.     end
  93. end
  94.  
  95. return { checkIfSlotIsItem = checkIfSlotIsItem, findItem = findItem, checkIfHaveItem = checkIfHaveItem, findEmpty = findEmpty, countItems = countItems, checkIfInventoryFull = checkIfInventoryFull, emptyInventory = emptyInventory, refuel = refuel,}
Advertisement
Add Comment
Please, Sign In to add comment