Advertisement
Chaos_Cash

storageManager.lua

Jan 4th, 2025 (edited)
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.02 KB | None | 0 0
  1. function startup()
  2.  
  3.     for k,v in pairs(peripheral.getNames()) do
  4.         if peripheral.getType(peripheral.getNames()[k]) == "modem" then
  5.         rednet.open(peripheral.getNames()[k])
  6.         end
  7.     end
  8.  
  9.  
  10. scrollmenuLen={}
  11. scrollmenuInfos={}
  12.  
  13.  
  14. whitelistedPocketComputers = {53}
  15.  
  16. nonStackableItems = {}
  17.  
  18. mainStorage = Storage:new({"create:item_vault"},"minecraft:barrel_1","minecraft:barrel_0")
  19. mainStorage:refreshStorageContent()
  20.  
  21. Chaos_Cash = Player:new("player_interface_3")
  22.  
  23.  
  24. end
  25.  
  26.  
  27.  
  28. function main()
  29.  
  30.     while true do
  31.  
  32.     info1,info2,info3,info4 = os.pullEvent()
  33.        
  34.    
  35.         if info1 == "rednet_message" then  
  36.             for k,v in pairs(whitelistedPocketComputers) do
  37.                 if v == info2 then
  38.  
  39.  
  40.                     if type(info3) == "table" then
  41.                    
  42.                         if info3["action"] == "inputToPlayer" then
  43.                             if nonStackableItems[info3["itemName"]] then
  44.                             Chaos_Cash:inputFromStorage(mainStorage,info3["itemName"],1)
  45.                             else
  46.                             Chaos_Cash:inputFromStorage(mainStorage,info3["itemName"],info3["amount"])
  47.                             end
  48.                         rednet.send(info2,{["action"]="sendingStorageContent",["storageContent"]=mainStorage:getStorageContent()})
  49.                         end
  50.                        
  51.                        
  52.                         if info3["action"] == "inputToStorage" then
  53.                         Chaos_Cash:inputToStorage(mainStorage,info3["slot"],info3["itemName"],info3["amount"])
  54.                         rednet.send(info2,{["action"]="sendingPlayerInventory",["inventory"]=Chaos_Cash:getInventory()})
  55.                         end
  56.                        
  57.                        
  58.                    
  59.                     else
  60.                    
  61.                         if info3 == "ping" then
  62.                         rednet.send(info2,"pong")
  63.                         end
  64.                        
  65.                        
  66.                         if info3 == "requestingStorageContent" then
  67.                         rednet.send(info2,{["action"]="sendingStorageContent",["storageContent"]=mainStorage:getStorageContent()})
  68.                         end
  69.                        
  70.                        
  71.                         if info3 == "requestingPlayerInventory" then
  72.                         rednet.send(info2,{["action"]="sendingPlayerInventory",["inventory"]=Chaos_Cash:getInventory()})
  73.                         end
  74.                        
  75.                     end
  76.  
  77.                 break
  78.                 end
  79.             end
  80.        
  81.         end
  82.        
  83.        
  84.         if info1 == "key" and info2 == keys.space then
  85.         mainStorage:input()
  86.         end
  87.        
  88.        
  89.         if curEnv == homeMenu then
  90.        
  91.         scrollOut = checkScrollmenu("storageItems")
  92.        
  93.             if scrollOut then
  94.             mainStorage:output(scrollmenuInfos["storageItems"]["content"][scrollOut],64)
  95.             scrollContent = getItemNames()
  96.             table.sort(scrollContent)
  97.             createScrollmenu("storageItems",1,1,30,19,scrollContent,scrollmenuInfos["storageItems"]["position"])
  98.             printScrollmenu("storageItems")
  99.             end
  100.        
  101.         end
  102.        
  103.    
  104.     end
  105.    
  106. end
  107.  
  108.  
  109.  
  110. function getItemNames()
  111. returnTable = {}
  112.     for k,v in pairs(mainStorage["storageContent"]) do
  113.     table.insert(returnTable,k)
  114.     end
  115.    
  116. return returnTable
  117. end
  118.  
  119.  
  120.  
  121. function homeMenu()
  122. curEnv = homeMenu
  123. term.clear()
  124.  
  125.  
  126. scrollContent = getItemNames()
  127. table.sort(scrollContent)
  128. createScrollmenu("storageItems",1,1,30,19,scrollContent,0)
  129. printScrollmenu("storageItems")
  130.  
  131. end
  132.  
  133.  
  134.  
  135. function printScrollmenu(name)
  136.  
  137.     for i=1,math.min(scrollmenuLen[name],scrollmenuInfos[name]["y2"]-scrollmenuInfos[name]["y1"]+1) do
  138.        
  139.         if type(scrollmenuInfos[name]["previewText"][i+scrollmenuInfos[name]["position"]]) == "string" then
  140.             if (i+scrollmenuInfos[name]["position"])%2==0 then
  141.             term.setBackgroundColor(colors.gray)
  142.             else
  143.             term.setBackgroundColor(colors.black)
  144.             end
  145.            
  146.         term.setCursorPos(scrollmenuInfos[name]["x1"],scrollmenuInfos[name]["y1"]+i-1)
  147.         term.write(scrollmenuInfos[name]["previewText"][i+scrollmenuInfos[name]["position"]])
  148.        
  149.             for i2=1,scrollmenuInfos[name]["x2"]-scrollmenuInfos[name]["x1"]+1-string.len(scrollmenuInfos[name]["content"][i+scrollmenuInfos[name]["position"]]) do
  150.             term.write(" ")
  151.             end
  152.         end
  153.  
  154.     end
  155.  
  156.  
  157.  
  158. end
  159.  
  160.  
  161.  
  162. function checkScrollmenu(name)
  163.  
  164.     if info1 == "mouse_click" and info3 > scrollmenuInfos[name]["x1"]-1 and info3 < scrollmenuInfos[name]["x2"]+1 and info4 > scrollmenuInfos[name]["y1"]-1 and info4 < scrollmenuInfos[name]["y2"]+1 then
  165.     resetEvent()
  166.     return info4-scrollmenuInfos[name]["y1"]+1+scrollmenuInfos[name]["position"]
  167.     end
  168.    
  169.     if info1 == "mouse_scroll" and not (scrollmenuInfos[name]["position"]+info2 > scrollmenuLen[name]-(scrollmenuInfos[name]["y2"]-scrollmenuInfos[name]["y1"]+1)) and not (scrollmenuInfos[name]["position"]+info2 < 0) then
  170.     scrollmenuInfos[name]["position"] = scrollmenuInfos[name]["position"] + info2
  171.     printScrollmenu(name)
  172.     end
  173.  
  174.  
  175. end
  176.  
  177.  
  178.  
  179.  
  180. function createScrollmenu(name,x1,y1,x2,y2,content,position)
  181. scrollmenuLen[name]=0
  182. scrollmenuInfos[name]={["x1"]=x1,["y1"]=y1,["x2"]=x2,["y2"]=y2,["content"]=content,["position"]=position,["previewText"]={}}
  183.  
  184.     for k,v in pairs(content) do
  185.     scrollmenuLen[name]=scrollmenuLen[name]+1
  186.     end
  187.    
  188.     for k,v in pairs(content) do
  189.         if string.len(v) > x2 - x1 + 1 then
  190.         scrollmenuInfos[name]["previewText"][k] = string.sub(v,1,x2-x1-2) .. "..."
  191.         else
  192.         scrollmenuInfos[name]["previewText"][k] = v
  193.         end
  194.     end
  195.  
  196. end
  197.  
  198.  
  199.  
  200. Storage = {
  201. new = function(self,storageNames,inputPeripheral,outputPeripheral)
  202.     if type(storageNames) ~= "table" then
  203.     error("bad argument #1 (storageNames is supposed to be a table)")
  204.     elseif type(inputPeripheral) ~= "string" then
  205.     error("bad argument #2 (inputPeripheral is supposed to be a string)")
  206.     elseif type(outputPeripheral) ~= "string" then
  207.     error("bad argument #3 (outputPeripheral is supposed to be a string)")
  208.     end
  209.  
  210. local returnTable = {["storageNames"]=storageNames,["inputPeripheralName"]=inputPeripheral,["inputPeripheral"]=peripheral.wrap(inputPeripheral),["outputPeripheralName"]=outputPeripheral,["outputPeripheral"]=peripheral.wrap(outputPeripheral),["storagePeripherals"] = {},["storageContent"]={},["storageInfo"]={}}
  211.  
  212.     for k,v in pairs(peripheral.getNames()) do
  213.         for k2,v2 in pairs(storageNames) do
  214.             if string.find(v,v2) then
  215.             returnTable["storagePeripherals"][v] = peripheral.wrap(v)
  216.             returnTable["storageInfo"][v] = {}
  217.             end
  218.         end
  219.     end
  220.  
  221. setmetatable(returnTable,self)
  222. self.__index = self
  223. return returnTable
  224. end
  225. ,
  226.  
  227.  
  228. output = function(self,itemName,amount)
  229.  
  230.     for k,v in pairs(self["storageContent"][itemName]) do
  231.  
  232.         for i = 1, self["outputPeripheral"].size() do
  233.             if self["outputPeripheral"].getItemDetail(i) == nil then
  234.             local outputtedAmount = v["storage"].pushItems(self["outputPeripheralName"],v["slot"],amount)
  235.                 if amount >= v["count"] then
  236.                 self["storageContent"][itemName][k] = nil
  237.                 end
  238.             amount = amount - outputtedAmount
  239.             end
  240.                        
  241.             if amount < 1 or self["storageContent"][itemName][k] == nil then
  242.             break
  243.             end
  244.         end
  245.  
  246.  
  247.         if amount < 1 then
  248.         break
  249.         end
  250.  
  251.     end
  252.  
  253. end
  254. ,
  255.  
  256.  
  257. input = function(self)
  258.  
  259.     for slot,item in pairs(self["inputPeripheral"].list()) do
  260.    
  261.         while item["count"] > 0 do
  262.        
  263.             if self["storageContent"][item["name"]] then
  264.                
  265.                 for k,v in pairs(self["storageContent"][item["name"]]) do
  266.                
  267.                     if v["storage"].getItemLimit(v["slot"])-v["count"] > 0 then
  268.                     local pushedItems = self["inputPeripheral"].pushItems(v["storageName"],slot,v["storage"].getItemLimit(v["slot"])-v["count"],v["slot"])
  269.                     item["count"] = item["count"] - pushedItems
  270.                     self["storageContent"][item["name"]][k]["count"] = self["storageContent"][item["name"]][k]["count"] + pushedItems
  271.                         if item["count"] < 1 then
  272.                         break
  273.                         end
  274.                     end
  275.                 end
  276.                
  277.                
  278.                 if item["count"] > 0 then
  279.                 local freeStorageName, freeSlot = self:getFreeSlot()
  280.                 local pushedItems = self["inputPeripheral"].pushItems(freeStorageName,slot,self["storagePeripherals"][freeStorageName].getItemLimit(freeSlot),freeSlot)
  281.                 table.insert(self["storageContent"][item["name"]],{["slot"]=freeSlot,["storageName"]=freeStorageName,["storage"]=self["storagePeripherals"][freeStorageName],["count"]=pushedItems})
  282.                 item["count"] = item["count"] - pushedItems
  283.                 table.remove(self["storageInfo"][freeStorageName]["freeSlots"],1)
  284.                 end
  285.                
  286.            
  287.             else
  288.            
  289.             local freeStorageName, freeSlot = self:getFreeSlot()
  290.             local pushedItems = self["inputPeripheral"].pushItems(freeStorageName,slot,self["storagePeripherals"][freeStorageName].getItemLimit(freeSlot),freeSlot)
  291.             self["storageContent"][item["name"]] = {}
  292.             table.insert(self["storageContent"][item["name"]],{["slot"]=freeSlot,["storageName"]=freeStorageName,["storage"]=self["storagePeripherals"][freeStorageName],["count"]=pushedItems})
  293.             item["count"] = item["count"] - pushedItems
  294.             table.remove(self["storageInfo"][freeStorageName]["freeSlots"],1)
  295.            
  296.             end
  297.         end
  298.        
  299.     end
  300.  
  301. end
  302. ,
  303.  
  304.  
  305. refreshStorageContent = function(self)
  306.  
  307. self["storageContent"] = {}
  308.  
  309.     for storageName,storage in pairs(self["storagePeripherals"]) do
  310.         for slot,item in pairs(storage.list()) do
  311.        
  312.             if item["count"] == 1 then
  313.                 if storage.getItemDetail(slot)["maxCount"] == 1 then
  314.                 stackable = false
  315.                 else
  316.                 stackable = true
  317.                 end
  318.             else
  319.             stackable = true
  320.             end
  321.                
  322.             if type(self["storageContent"][item["name"]]) == "table" then
  323.             table.insert(self["storageContent"][item["name"]],{["slot"]=slot,["storageName"]=storageName,["storage"]=storage,["count"]=item["count"],["stackable"]=stackable})
  324.             else
  325.             self["storageContent"][item["name"]] = {{["slot"]=slot,["storageName"]=storageName,["storage"]=storage,["count"]=item["count"],["stackable"]=stackable}}
  326.             end
  327.         end
  328.    
  329.     self["storageInfo"][storageName] = {["freeSlots"] = {}}
  330.    
  331.     local storageItems = storage.list()
  332.         for i = 1, storage.size() do
  333.             if not storageItems[i] then
  334.             table.insert(self["storageInfo"][storageName]["freeSlots"],i)
  335.             end
  336.         end
  337.    
  338.     end
  339.  
  340. end
  341. ,
  342.  
  343.  
  344. getFreeSlot = function(self)
  345.  
  346.     for storageName,storage in pairs(self["storagePeripherals"]) do
  347.         for k2,slot in pairs(self["storageInfo"][storageName]["freeSlots"]) do
  348.         return storageName, slot
  349.         end
  350.     end
  351.  
  352. error("storage is full")   
  353. end
  354. ,
  355.  
  356.  
  357. getStorageContent = function(self)
  358. return self["storageContent"]
  359. end
  360. }
  361.  
  362.  
  363.  
  364. Player = {
  365. new = function(self,peripheralName)
  366.     if type(peripheralName) ~= "string" then
  367.     error("bad argument #1 (peripheralName is supposed to be a string)")
  368.     end
  369.  
  370. local returnTable = {["peripheralName"]=peripheralName,["peripheral"]=peripheral.wrap(peripheralName),["inventory"]={}}
  371.  
  372.     for k,v in pairs(returnTable["peripheral"].list()) do
  373.         if k < 37 then
  374.         returnTable["inventory"][k] = v
  375.         end
  376.     end
  377.  
  378. setmetatable(returnTable,self)
  379. self.__index = self
  380. return returnTable
  381. end
  382. ,
  383.  
  384.  
  385. getInventory = function(self)
  386. self["inventory"] = {}
  387.     for k,v in pairs(self["peripheral"].list()) do
  388.         if k < 37 then
  389.         self["inventory"][k] = v
  390.         end
  391.     end
  392.    
  393. return self["inventory"]
  394. end
  395. ,
  396.  
  397.  
  398. inputFromStorage = function(self,storage,itemName,amount)
  399.  
  400.     if storage["storageContent"][itemName] then
  401.         if table.maxn(storage["storageContent"][itemName]) ~= 0 then
  402.             for k,item in pairs(storage["storageContent"][itemName]) do
  403.             pulledAmount = self["peripheral"].pullItems(item["storageName"],item["slot"],amount)
  404.                 if pulledAmount == item["count"] then
  405.                 table.insert(mainStorage["storageInfo"][item["storageName"]]["freeSlots"],item["slot"])
  406.                 storage["storageContent"][itemName][k] = nil
  407.                     if table.maxn(storage["storageContent"][itemName]) == 0 then
  408.                     storage["storageContent"][itemName] = nil
  409.                     end
  410.                 end
  411.             amount = amount - pulledAmount
  412.                 if amount < 1 then
  413.                 break
  414.                 end
  415.             end
  416.         else
  417.         error("Item wasnt found in storage.")
  418.         end
  419.     else
  420.     error("Item wasnt found in storage.")
  421.     end
  422.    
  423. end
  424. ,
  425.  
  426.  
  427. inputToStorage = function(self,storage,itemSlot,itemName,itemCount)
  428.  
  429.    
  430.     if storage["storageContent"][itemName] then
  431.        
  432.         for k,v in pairs(storage["storageContent"][itemName]) do
  433.        
  434.             if v["storage"].getItemLimit(v["slot"])-v["count"] > 0 then
  435.             local pushedItems = self["peripheral"].pushItems(v["storageName"],itemSlot,v["storage"].getItemLimit(v["slot"])-v["count"],v["slot"])
  436.                 if pushedItems then
  437.                 itemCount = itemCount - pushedItems
  438.                 storage["storageContent"][itemName][k]["count"] = storage["storageContent"][itemName][k]["count"] + pushedItems
  439.                     if itemCount < 1 then
  440.                     break
  441.                     end
  442.                 end
  443.             end
  444.         end
  445.        
  446.        
  447.         if itemCount > 0 then
  448.         local freeStorageName, freeSlot = storage:getFreeSlot()
  449.         local pushedItems = self["peripheral"].pushItems(freeStorageName,itemSlot,storage["storagePeripherals"][freeStorageName].getItemLimit(freeSlot),freeSlot)
  450.         table.insert(storage["storageContent"][itemName],{["slot"]=freeSlot,["storageName"]=freeStorageName,["storage"]=storage["storagePeripherals"][freeStorageName],["count"]=pushedItems})
  451.         itemCount = itemCount - pushedItems
  452.         table.remove(storage["storageInfo"][freeStorageName]["freeSlots"],1)
  453.         end
  454.        
  455.    
  456.     else
  457.    
  458.     local freeStorageName, freeSlot = storage:getFreeSlot()
  459.     local pushedItems = self["peripheral"].pushItems(freeStorageName,itemSlot,storage["storagePeripherals"][freeStorageName].getItemLimit(freeSlot),freeSlot)
  460.     storage["storageContent"][itemName] = {}
  461.     table.insert(storage["storageContent"][itemName],{["slot"]=freeSlot,["storageName"]=freeStorageName,["storage"]=storage["storagePeripherals"][freeStorageName],["count"]=pushedItems})
  462.     itemCount = itemCount - pushedItems
  463.     table.remove(storage["storageInfo"][freeStorageName]["freeSlots"],1)
  464.    
  465.     end
  466.  
  467. end
  468. }
  469.  
  470.  
  471.  
  472. function resetEvent()
  473. info1 = nil
  474. end
  475.  
  476.  
  477.  
  478.  
  479.  
  480.  
  481. startup()
  482. homeMenu()
  483. main()
  484.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement