Advertisement
Progig01

Storage

Sep 28th, 2020 (edited)
967
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.95 KB | None | 0 0
  1. local m = {}
  2.  
  3.     --List valid inventory types
  4.     m.validStorageInventories = {
  5.     "actuallyadditions:giantchestlarge",
  6.     "actuallyadditions:giantchestmedium",
  7.     "actuallyadditions:giantchest",
  8.     "yabba:item_barrel",
  9.     "yabba:antibarrel"
  10.     }
  11.  
  12.     --Create a new storage database object
  13.     function m:newStorage(network)
  14.         if network == nil then error("Network provided is invalid or does not exist") end
  15.         local storage = {}
  16.         storage.network = network
  17.         storage.inventories = {}
  18.         storage.inventoryNames = {}
  19.         storage.masterTable = {}
  20.  
  21.         --Do all the startup stuff for making a new storage DB
  22.         function storage:init()
  23.             --Init the table of connected inventoy names
  24.             self.inventoryNames = self:indexStorageInventories()
  25.  
  26.             --Setup inventories as tables so we can actually use them
  27.             for i=1, #self.inventoryNames do
  28.                 self.inventories[i] = {}
  29.                 self.inventories[i].items = self:generateItemStacks(self.inventoryNames[i])
  30.             end
  31.  
  32.             --Generate the storage master table
  33.             self.masterTable = self:generateMasterTable()
  34.         end
  35.  
  36.         --Test function
  37.         function storage:test()
  38.             for i=1, #self.inventories do
  39.                
  40.             end
  41.         end
  42.  
  43.         --Move an item from storage to a specified inventory
  44.         function storage:moveItemTo(target, item, quantity)
  45.             if target == nil then error("no inventory provided") end
  46.             local currentItem = nil
  47.             local quantityRetrieved = 0
  48.  
  49.             for i=1, #self.inventories do
  50.  
  51.                 for k,v in pairs(self.inventories[i].items) do
  52.                     currentItem = self.inventories[i].items[k]
  53.  
  54.                     if currentItem.name == item then
  55.                         if self.masterTable[currentItem.name].count < quantity then
  56.                             print("Insufficient quantity, only " .. self.masterTable[currentItem.name].count .. " " .. currentItem.name .. "(s) in storage.")
  57.                             sleep(1.1)
  58.                         elseif currentItem.count >= quantity then
  59.                             self.network.callRemote(target, "pullItems", currentItem.parentInventory,k, quantity)
  60.                             currentItem.count = currentItem.count - quantity
  61.                             self.masterTable[currentItem.name].count = self.masterTable[currentItem.name].count - quantity
  62.                             quantityRetrieved = quantityRetrieved + quantity
  63.                             if quantityRetrieved >= quantity then break end
  64.                             print("Retrieved")
  65.                             sleep(0.75)
  66.                         end
  67.                     end
  68.                 end
  69.  
  70.             end
  71.         end
  72.  
  73.         --Get the amount of open slots in a given inventory
  74.         function storage:getAvailableSpace(inv)
  75.             if inv ~= nil then
  76.                 local invSize = self.network.callRemote(inv, "size")
  77.                 local slotsOccupied = 0
  78.  
  79.                 if invSize > 0 then
  80.                     local invList = self.network.callRemote(inv, "list")
  81.  
  82.                     for k,v in pairs(invList) do
  83.                         slotsOccupied = slotsOccupied + 1
  84.                     end
  85.                 else
  86.                     return 0
  87.                 end
  88.                 return invSize - slotsOccupied
  89.             else
  90.                 error("expected inventory, got nil")
  91.             end
  92.         end
  93.  
  94.         --Test a given inventory against the list of valid types of storage inventories
  95.         function storage:isStorageInventory(inv)
  96.             local invType = network.callRemote(inv , "getTypeRemote")
  97.  
  98.             for i=1, #m.validStorageInventories do
  99.                 if invType == m.validStorageInventories[i] then
  100.                     return true
  101.                 end
  102.             end
  103.             return false
  104.         end
  105.  
  106.         --Get a list of valid attached storage inventories
  107.         function storage:indexStorageInventories()
  108.             local allPeripherals = network.getNamesRemote()
  109.             local storageInventories = {}
  110.  
  111.             for i=1, #allPeripherals do
  112.                 for j=1, #m.validStorageInventories do
  113.                     if network.getTypeRemote(allPeripherals[i]) == m.validStorageInventories[j] then
  114.                         table.insert(storageInventories, allPeripherals[i])
  115.                     end
  116.                 end
  117.             end
  118.             return storageInventories
  119.         end
  120.  
  121.         --Create a set of itemStacks for a given inventory
  122.         function storage:generateItemStacks(inv)
  123.             if inv == nil then error("Expected inventory") end
  124.             local invList = self.network.callRemote(inv, "list")
  125.             local invItemStacks = {}
  126.  
  127.             for k,v in pairs(invList) do
  128.                 local currentItem = self.network.callRemote(inv, "getItemMeta", k)
  129.                 local itemStack = {}
  130.  
  131.                 itemStack.name = currentItem.displayName
  132.                 itemStack.count = currentItem.count
  133.                 itemStack.rawName = currentItem.rawName
  134.                 itemStack.id = currentItem.name
  135.                 itemStack.oredic = currentItem.ores
  136.                 itemStack.parentInventory = inv
  137.  
  138.                 invItemStacks[k] = itemStack
  139.             end
  140.  
  141.             return invItemStacks
  142.         end
  143.  
  144.         --Create a master storage table of contents of all inventories for fast checking
  145.         function storage:generateMasterTable()
  146.             local masterTable = {}
  147.  
  148.             for i=1, #self.inventories do
  149.  
  150.                 for k,v in pairs(self.inventories[i].items) do
  151.                     if masterTable[v.name] == nil then
  152.                         masterTable[v.name] = {}
  153.                         masterTable[v.name].name = v.displayName
  154.                         masterTable[v.name].count = v.count
  155.                         masterTable[v.name].rawName = v.rawName
  156.                         masterTable[v.name].id = v.name
  157.                         masterTable[v.name].oredic = v.ores
  158.                     else
  159.                         masterTable[v.name].count = masterTable[v.name].count + v.count
  160.                     end
  161.                 end
  162.  
  163.             end
  164.  
  165.             return masterTable
  166.         end
  167.  
  168.         return storage
  169.     end
  170.  
  171. return m
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement