Robear9992

storage

Aug 1st, 2022 (edited)
754
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.86 KB | None | 0 0
  1. -- sort
  2. --pastebin get T1p86Kg3 storage.lua
  3.  
  4. --Passthru: find, store, get, containers, etc
  5. --shell.run("storage "..arg[0].." "..table.concat(arg," "))
  6.  
  7.  
  8. local inventory = require("inventory")
  9. local sides = require("sides")
  10.  
  11. STORAGE = {
  12.     --inventory.wrap("left"),
  13.     --inventory.wrap("right")
  14. }
  15.  
  16. DROP = inventory.wrap("metalbarrels:silver_tile_6")
  17.  
  18. function isStorage(name)
  19.     local types = {peripheral.getType(name)}
  20.     for _,t in pairs(types) do
  21.         if t == "inventory" then return true end
  22.     end
  23.     return false
  24. end
  25.  
  26. function containers()
  27.     for _,v in pairs(STORAGE) do
  28.         print(v.name)
  29.     end
  30. end
  31.  
  32. function findStorage()
  33.     for _,storage in pairs(peripheral.getNames()) do
  34.         if storage ~= DROP.name then
  35.             if isStorage(storage) and not isSide(storage) then
  36.                 table.insert(STORAGE,inventory.wrap(storage))
  37.             end
  38.         end
  39.     end
  40. end
  41.  
  42. function store()
  43.     storeAll()
  44.     local size = 0
  45.     local count = 0
  46.     for _,storage in pairs(STORAGE) do
  47.         size = size + storage.size()
  48.         count = count + storage.count()
  49.     end
  50.     print(count.."/"..size.." slots full ("..string.format("%.2f", (count/size) * 100).. "%)")
  51. end
  52.  
  53. function storeAll()
  54.     local existing = {}
  55.     for _,storage in pairs(STORAGE) do
  56.         for item,_ in pairs(storage.counts()) do
  57.             if existing[item] == nil then
  58.                 existing[item] = { storage }
  59.             else
  60.                 table.insert(existing[item],storage)
  61.             end
  62.         end
  63.     end
  64.    
  65.     for slot, item in pairs(DROP.list()) do
  66.         if not storeItem(item, slot, existing) then return false end
  67.     end
  68.     return true
  69. end
  70.  
  71. function storeItem(item, slot, existing)
  72.  
  73.     local preferredStorage = existing[item]
  74.     if preferredStorage ~= nil then
  75.         for _,storage in pairs(preferredStorage) do
  76.             if DROP.push(storage, slot) then return true end
  77.         end
  78.     end
  79.  
  80.     for _,storage in pairs(STORAGE) do
  81.         if not storage.isFull() then
  82.             if DROP.push(storage, slot) then return true end
  83.         end
  84.     end
  85.     return false
  86. end
  87.  
  88. function findItem(search)
  89.     for _,storage in pairs(STORAGE) do
  90.         for item, count in pairs(storage.counts()) do
  91.             if string.find(item,search) then
  92.                 --print(count .. " " .. item .. " in " .. storage.name)
  93.                 print(count .. " " .. item)
  94.             end
  95.         end
  96.     end
  97. end
  98.  
  99. function getItems(search, count)
  100.     for _,storage in pairs(STORAGE) do
  101.         for item, _ in pairs(storage.counts()) do
  102.             if string.find(item,search) then
  103.                 local _, moved = storage.push(DROP,item,count)
  104.                 count = count - moved
  105.                 if count == 0 then return true, 0 end
  106.             end
  107.         end
  108.     end
  109.     return false, count
  110. end
  111.  
  112.  
  113. findStorage()
  114.  
  115. local tArgs = { ... }
  116.  
  117. if #tArgs == 1 then
  118.     if(tArgs[1] == "store") then store() end
  119.     if(tArgs[1] == "containers") then containers() end
  120. end
  121.  
  122. if #tArgs == 2 then
  123.     if(tArgs[1] == "find")  then findItem(tArgs[2]) end
  124.     if(tArgs[1] == "get") then getItems(tArgs[2], 64) end
  125. end
  126.  
  127. if #tArgs == 3 then
  128.     if(tArgs[1] == "get") then getItems(tArgs[3], tonumber(tArgs[2])) end
  129. end
  130.  
Add Comment
Please, Sign In to add comment