Advertisement
skypop

CC turtle compactInventory

Jul 31st, 2018
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.12 KB | None | 0 0
  1. --Turtle compactInventory
  2. --by SukaiPoppuGo
  3. --Stack items in first slots of an inventory
  4. --Example #1:
  5. -- os.loadAPI("APIFileName.lua")
  6. -- APIFileName.compactInventory()
  7. --
  8. --Example #2: (compact stacks, but do not moving items near the first slot)
  9. -- os.loadAPI("APIFileName.lua")
  10. -- APIFileName.compactInventory(true)
  11. --
  12.  
  13. local function _select(slot)
  14.     if slot ~= turtle.getSelectedSlot() then
  15.         turtle.select(slot)
  16.     end
  17. end
  18.  
  19. function compactInventory(finish)
  20.     finish = finish or false
  21.     for slot=16,2,-1 do
  22.         if turtle.getItemCount(slot) > 0 then
  23.             for toSlot=1,16 do
  24.                 --print(slot, "->", toSlot)
  25.                 if slot > toSlot
  26.                 and turtle.getItemSpace(toSlot) > 0
  27.                 and (_select(slot) or true)
  28.                 and (turtle.compareTo(toSlot) or turtle.getItemCount(toSlot) == 0)
  29.                 and turtle.transferTo(toSlot)
  30.                 and turtle.getItemCount(slot) == 0 then
  31.                     break
  32.                 end
  33.             end
  34.         end
  35.     end
  36.     if not finish then
  37.         compactInventory(true)
  38.     end
  39. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement