lucifersamfr

UniversalPackager

Sep 12th, 2014
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --nb of item per packet (1 for simple conversion, 4 for 2*2 grid crafting, 9 for 3*3 grid crafting)
  2. psize=9
  3. --packager side
  4. side="bottom"
  5.  
  6. function compactInv()
  7.   for i = 1,16 do
  8.     if turtle.getItemCount(i) > 0 and turtle.getItemCount(i) < 64 then
  9.       turtle.select(i)
  10.       for j = i+1,16 do
  11.         if turtle.compareTo(j) then
  12.           turtle.select(j)
  13.           turtle.transferTo(i)
  14.           turtle.select(i)
  15.         end
  16.       end
  17.     end
  18.   end
  19.      
  20.   for i = 1,16 do
  21.     if turtle.getItemCount(i) > 0 then
  22.       for j = 1,i do
  23.         if turtle.getItemCount(j) == 0 then
  24.           turtle.select(i)
  25.           turtle.transferTo(j)
  26.           break
  27.         end
  28.       end
  29.     end
  30.   end
  31. end
  32.  
  33. function nextSlot(slot)
  34.   if slot == 16 then
  35.     return 1
  36.   else
  37.     return slot + 1
  38.   end
  39. end
  40.  
  41. function sendStackToPackager(packager,slot,packetSize)
  42.   print("Sending slot "..slot.. " to packager in packets of "..packetSize.." items.")
  43.   waitBeforeNextSlot = not turtle.compareTo(nextSlot(slot))
  44.      
  45.   while turtle.getItemCount(slot) >= packetSize do
  46.     --print("  enough items left to package ("..turtle.getItemCount(slot)..").")
  47.     buffer = packager.getStackInSlot(1)
  48.     while buffer ~= null and buffer["qty"] >= (64-packetSize) do
  49.       print("--Packager Overflow : Waiting 3s ("..buffer["qty"].." items left).")
  50.       sleep(3)
  51.       buffer = packager.getStackInSlot(1)
  52.     end
  53.     print("--Packager OK : dropping down "..packetSize.." items from slot "..slot..".")
  54.     turtle.dropDown(packetSize)
  55.     sleep(0.2)
  56.   end
  57.  
  58.   if waitBeforeNextSlot then
  59.     buffer = packager.getStackInSlot(1)
  60.     while buffer ~= null and buffer["qty"] > 0 do
  61.       if bak == buffer["qty"] then
  62.         print("There seems to be a problem with the packager : buffer size("..bak..") doesn't change.")
  63.       end
  64.       bak = buffer["qty"]
  65.      
  66.       print("--Slot Transiton : waiting ...")
  67.       sleep(1.2)
  68.  
  69.       buffer = packager.getStackInSlot(1)
  70.     end
  71.   end
  72. end
  73.  
  74.  
  75. local packager = peripheral.wrap(side)
  76. while true do
  77.   print("Compacting ...")
  78.   compactInv()
  79.   for i=1,16 do
  80.     turtle.select(i)
  81.     sendStackToPackager(packager,i,psize)
  82.     sleep(0.2)
  83.   end
  84.   sleep(2)
  85. end
Add Comment
Please, Sign In to add comment