bcash8

Storage System Server V1.0

Jan 4th, 2022 (edited)
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.27 KB | None | 0 0
  1. local OUTPUT = "minecraft:barrel_7"
  2. local INPUT = "minecraft:barrel_8"
  3. local playerInventories = {goblim = {adjacentInventory = "minecraft:barrel_9", inventoryManager = "inventoryManager_0"},
  4. WhiteCarsMatter = {adjacentInventory = "minecraft:barrel_10", inventoryManager = "inventoryManager_1"},
  5. beaudog = {adjacentInventory = "minecraft:barrel_11", inventoryManager = "inventoryManager_2"}
  6. }
  7. local chests = {peripheral.find("minecraft:chest")}
  8. local storage = {}  --"item" = {location = {"chestName" = {1,2,3}}, count = 0}
  9. local movingItems = false
  10. local checkInvList = {INPUT, "enderstorage:ender_chest_0"}
  11.  
  12. --modem setup
  13. local listenChannel = 68
  14. local channelPassword = "BigDonkeyButts"
  15. local modem = peripheral.wrap("back")
  16. if not modem.isOpen(listenChannel) then modem.open(listenChannel) end -- open our modem
  17. print(modem.isOpen(listenChannel))
  18. --local OUTPUT = "minecraft:chest_3"
  19.  
  20. function getStorage()
  21.     for _ , chest in pairs(chests) do
  22.         for slot, item in pairs(chest.list()) do
  23.             if not storage[item.name] then storage[item.name] = {count = 0, location = {}} end
  24.  
  25.             storage[item.name].count = storage[item.name].count + item.count
  26.             if storage[item.name].location[peripheral.getName(chest)] == nil then
  27.                 storage[item.name].location[peripheral.getName(chest)] = {}
  28.                 table.insert(storage[item.name].location[peripheral.getName(chest)], slot)
  29.             else
  30.                 table.insert(storage[item.name].location[peripheral.getName(chest)], slot)
  31.             end
  32.         end
  33.     end
  34. end
  35.  
  36. function getItem(name,amount,destInv)
  37.     if not storage[name] then return false end
  38.     destInv = destInv or OUTPUT
  39.     movingItems = true
  40.     drawScreen()
  41.     term.setCursorPos(1,2)
  42.     print("Retrieving " .. name)
  43.     term.setCursorPos(1,4)
  44.     for chestName, slots in pairs(storage[name].location) do
  45.         coroutine.yield()
  46.         local chest = peripheral.wrap(chestName)
  47.         for i, slot in pairs(slots) do
  48.             local movedItems = chest.pushItems(destInv,slot,amount)
  49.             storage[name].count = storage[name].count - movedItems
  50.             amount = amount - movedItems
  51.  
  52.             local details = chest.getItemDetail(slot)
  53.             if not details then
  54.                 table.remove(storage[name].location[chestName],i)
  55.             end
  56.             if storage[name].count <= 0 then
  57.                 storage[name] = nil
  58.             end
  59.             if amount <= 0 then movingItems = false return true end
  60.         end
  61.     end
  62.     movingItems = false
  63. end
  64.  
  65. function putInInventory(player, item, amount)
  66.     term.setCursorPos(1,10)
  67.     getItem(item,amount,playerInventories[player].adjacentInventory)
  68.     local manager = peripheral.wrap(playerInventories[player].inventoryManager)
  69.     manager.addItemToPlayer("UP",amount,-1,item)
  70. end
  71.  
  72. function moveToInv(destInv, destSlot, originInv, originSlot, amount)
  73.     local fromChest = peripheral.wrap(originInv)
  74.     local itemsMoved = fromChest.pushItems(destInv, originSlot, amount, destSlot)
  75.     return itemsMoved
  76. end
  77.  
  78. function findItemStackWithSpace(item)
  79.     for chestName, slots in pairs(storage[item].location) do -- check every known instance of that item in the storage
  80.         local testChest = peripheral.wrap(chestName)
  81.         for i, slot in pairs(slots) do
  82.             local space = 64 - testChest.getItemDetail(slot).count
  83.             if space > 0 then
  84.                 print("found part stack at: " .. chestName .. " " .. slot .. " " .. space)
  85.                 return true, chestName, slot, space
  86.             end
  87.         end
  88.     end
  89.     return false
  90. end
  91.  
  92. function findEmptySpace(lastOpenSlot)
  93.     local startInv = lastOpenSlot[1]
  94.     local startSlot = lastOpenSlot[2]
  95.     local startPos = 1
  96.     if startInv ~= "" then
  97.         for i, chest in pairs(chests) do
  98.             if peripheral.getName(chest) == startInv then
  99.                 startPos = i
  100.                 break
  101.             end
  102.         end
  103.     end
  104.  
  105.     for i=startPos,#chests do
  106.         local chest = chests[i]
  107.         for b=startSlot, chest.size() do
  108.             if not chest.getItemDetail(b) then
  109.                 return {peripheral.getName(chest), b}
  110.             end
  111.         end
  112.     end
  113.     return {"",1}
  114. end
  115.  
  116. function checkInputs()
  117.     while true do
  118.         sleep(1)
  119.         local lastOpenSlot = {"",1}
  120.         local testedItems = {}
  121.         for _, inputChest in pairs(checkInvList) do --check every input chest
  122.             local thisInv = peripheral.wrap(inputChest)
  123.  
  124.             for inputSlot, item in pairs(thisInv.list()) do -- chest every slot in that chest
  125.                 local numberOfItems = item.count
  126.  
  127.                 if storage[item.name] then
  128.                     if not testedItems[item.name] then
  129.                         testedItems[item.name] = true
  130.                         local worked, chestName, slot, space = findItemStackWithSpace(item.name)
  131.                         if worked then
  132.                             local movedItems = moveToInv(chestName,slot,inputChest,inputSlot,numberOfItems)
  133.                             storage[item.name].count = storage[item.name].count + movedItems
  134.                             numberOfItems = numberOfItems - movedItems
  135.                             if numberOfItems <= 0 then break end
  136.                         end
  137.                     end
  138.                 end
  139.  
  140.                 if numberOfItems > 0 then
  141.                     lastOpenSlot = findEmptySpace(lastOpenSlot)
  142.                     local movedItems = moveToInv(lastOpenSlot[1], lastOpenSlot[2], inputChest, inputSlot, numberOfItems)
  143.  
  144.                     if not storage[item.name] then storage[item.name] = {count = 0, location = {}} end
  145.                     if storage[item.name].location[lastOpenSlot[1]] == nil then
  146.                         storage[item.name].location[lastOpenSlot[1]] = {}
  147.                         table.insert(storage[item.name].location[lastOpenSlot[1]], lastOpenSlot[2])
  148.                     else
  149.                         table.insert(storage[item.name].location[lastOpenSlot[1]], lastOpenSlot[2])
  150.                     end
  151.                     storage[item.name].count = storage[item.name].count + movedItems
  152.                 end
  153.             end
  154.         end
  155.     end
  156. end
  157.  
  158. function commands()
  159.     local text = ""
  160.     local amount = "0"
  161.     local list = {}
  162.     local selectedItem = ""
  163.     local selector = 1
  164.     while true do
  165.         local event, key = os.pullEvent()
  166.         term.setCursorPos(1,2)
  167.         if event == "char" or event == "key" then
  168.             if event == "char" then
  169.                 if selectedItem == "" then
  170.                     text = text .. key
  171.                 elseif tonumber(key) then
  172.                     amount = amount .. key
  173.                 end
  174.             elseif event == "key" then
  175.                 if key == keys.backspace then
  176.                     text = string.sub(text, 1, -2)
  177.                     selectedItem = ""
  178.                     amount = "0"
  179.                     selector = 1
  180.                 elseif key == keys.enter then
  181.                     if selectedItem == "" then
  182.                         if #list > 0 then
  183.                             selectedItem = list[selector]
  184.                         end
  185.                     elseif tonumber(amount) > 0 then
  186.                         getItem(selectedItem, tonumber(amount))
  187.                         selectedItem = ""
  188.                         amount = "0"
  189.                         text = ""
  190.                         selector = 1
  191.                     end
  192.                 elseif key == keys.down then
  193.                     selector = selector + 1
  194.                     if selector > 10 then
  195.                         selector = 10
  196.                     end
  197.                 elseif key == keys.up then
  198.                     selector = selector - 1
  199.                     if selector < 1 then
  200.                         selector = 1
  201.                     end
  202.                 end
  203.             end
  204.  
  205.             drawScreen()
  206.             print(text)
  207.             if selectedItem ~= "" then
  208.                 term.setCursorPos(1,3)
  209.                 print(tonumber(amount))
  210.             end
  211.             local stringLength = string.len(text)
  212.             if stringLength > 1 then
  213.                 list = {}
  214.                 for name, _ in pairs(storage) do
  215.                     if string.find(name, text) then
  216.                         table.insert(list, name)
  217.                         if #list >= 10 then
  218.                             break
  219.                         end
  220.                     end
  221.                 end
  222.                 for i, name in pairs(list) do
  223.                     term.setCursorPos(18, i)
  224.                     if i == selector then
  225.                         term.setBackgroundColor(colors.blue)
  226.                     else
  227.                         term.setBackgroundColor(colors.gray)
  228.                     end
  229.                     local colon = string.find(name, ":")+1
  230.                     local DisplayName = string.sub(name, colon)
  231.                     print(DisplayName .. " " .. storage[name].count)
  232.                 end
  233.             end
  234.         end
  235.     end
  236. end
  237.  
  238. function drawScreen()
  239.     term.setBackgroundColor(colors.gray)
  240.     term.clear()
  241.     term.setCursorPos(1,1)
  242.     term.write("Type Item Name")
  243.     term.setCursorPos(20,1)
  244.     --term.setBackgroundColor(colors.white)
  245.     --term.write("Get")
  246.     --term.setBackgroundColor(colors.lightBlue)
  247.     --term.setCursorPos(25, 1)
  248.     --term.write("Craft")
  249.     --term.setBackgroundColor(colors.gray)
  250.     term.setCursorPos(1,2)
  251. end
  252.  
  253. function listenForMessages()
  254.     while true do
  255.         local event, side, channel, replyChannel, message = os.pullEvent("modem_message")
  256.         if channel == listenChannel then
  257.             if type(message) == "table" then
  258.                 if message[1] == channelPassword then
  259.                     if message[2] == "requestStorage" then
  260.                         modem.transmit(replyChannel, listenChannel, {"storage",storage})
  261.                     elseif message[2] == "getItem" then
  262.                         modem.transmit(replyChannel, listenChannel, message[4])
  263.                         putInInventory(message[3],message[4],message[5])
  264.                     end
  265.                 end
  266.             end
  267.         end
  268.     end
  269. end
  270.  
  271. getStorage()
  272. drawScreen()
  273.  
  274. parallel.waitForAll(checkInputs, commands, listenForMessages)
  275.  
Add Comment
Please, Sign In to add comment