Advertisement
Guest User

sort.lua

a guest
May 3rd, 2015
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.21 KB | None | 0 0
  1. local drop_chest = peripheral.wrap( "left" )
  2. local output_chest = peripheral.wrap( "right" )
  3. local trash = peripheral.wrap( "front" )
  4. local filename = "pages"
  5.  
  6. local file = fs.open(filename, "r")
  7. local data = nil
  8.  
  9. if file ~= nil then
  10.     local raw_data = file.readAll( )
  11.     data = string.len(raw_data) == 0 and {} or textutils.unserialize(raw_data)
  12.     file.close( )
  13. else
  14.     data = {}
  15. end
  16.  
  17. local new_count = 0
  18. local repeat_count = 0
  19.  
  20. local drop_chest_size = drop_chest.getInventorySize( )
  21. for s=1, drop_chest_size do
  22.     local item = drop_chest.getStackInSlot(s)
  23.    
  24.     if item ~= nil then
  25.         local name = item.display_name
  26.         local amount = item.qty
  27.        
  28.         print( name .. " : " .. amount )
  29.         drop_chest.pushItem("west", s, 64)
  30.        
  31.         for i = 1, amount do
  32.             if data[name] == nil then
  33.                 --  New Page
  34.                 data[name] = 1
  35.                 output_chest.pullItem( "east", 1, 1 )
  36.                
  37.                 new_count = new_count + 1
  38.             else
  39.                 --  Old Page
  40.                 trash.pullItem( "north", 1, 1 )
  41.                 repeat_count = repeat_count + 1
  42.             end
  43.         end
  44.     end
  45. end
  46.  
  47. print("New Pages Found: " .. tostring(new_count))
  48. print("Repeat Pages Discarded: " .. tostring(repeat_count))
  49.  
  50. file = fs.open(filename, "w")
  51. file.write(textutils.serialize(data))
  52. file.close( )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement