Advertisement
Voxel3042

simple storage network

Apr 2nd, 2024 (edited)
1,302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.52 KB | None | 0 0
  1. local chest_list = { peripheral.find("minecraft:chest") }
  2. local chest_main = ""
  3. local item_list = {}
  4. local ssn = {}
  5.  
  6. local function index_item(item, slot, chest)
  7.     if item_list[item.name] == nil then item_list[item.name] = {} end
  8.     local list_ = item_list[item.name]
  9.     list_[#list_+1] = {item=item, slot=slot, chest=chest}
  10. end
  11.  
  12. function ssn.init(chest_name)
  13.     chest_main = chest_name
  14.     for index, chest in pairs(chest_list) do
  15.         if peripheral.getName(chest) == chest_main then
  16.             table.remove(chest_list, index)
  17.             chest_main = chest
  18.         else
  19.             for slot, item in pairs(chest.list()) do
  20.                 index_item(item, slot, chest)
  21.             end
  22.         end
  23.     end
  24. end
  25.  
  26. function ssn.sum(item_id)
  27.     if item_list[item_id] == nil then return 0 end
  28.     local sum = 0
  29.    
  30.     for index, info in pairs(item_list[item_id]) do
  31.         sum = sum + info.item.count
  32.     end
  33.     return sum
  34. end
  35.  
  36. function ssn.pull(item_id)
  37.     if item_list[item_id] == nil then return false end
  38.     if #item_list[item_id] == 0 then return false end
  39.     local item = item_list[item_id]
  40.     item[#item].chest.pushItems( peripheral.getName(chest_main), item[#item].slot)
  41.     table.remove(item #item)
  42. end
  43.  
  44. function ssn.free_capacity(chest)
  45.     local cap = 0
  46.     for i = 1, chest.size() do
  47.         if chest.getItemDetail(i) == nil then
  48.             cap = cap + 1
  49.         end
  50.     end
  51.     return cap
  52. end
  53.  
  54. function ssn.exist(item_id)
  55.     return item_list[item_id] ~= nil
  56. end
  57.  
  58. return ssn
Tags: ssn
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement