Advertisement
BillBodkin

InventoryClient

Oct 10th, 2021 (edited)
1,015
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.71 KB | None | 0 0
  1. local items = {
  2.     ["all"] = {
  3.         ["minecraft:dirt"] = "Dirt",
  4.         ["minecraft:stone"] = "Stone",
  5.         ["minecraft:cobblestone"] = "Cobblestone",
  6.     },
  7.     ["recents"] = {
  8.     },
  9.     ["favorites"] = {
  10.         "minecraft:cobblestone"
  11.     }
  12. }
  13. local results = {}
  14. local query = ""
  15. local selectedIndex = 1
  16. local screen = "search"--"favorites"
  17.  
  18. local storageComputerID = 18
  19. local enderChest = "enderstorage:ender_chest_6"
  20.  
  21. rednet.open("back")
  22.  
  23. function Get(itemName, itemCount, chest, slot)
  24.     while true do
  25.         print("Getting " .. tostring(itemCount) .. " " .. chest .. " slot " .. tostring(slot))
  26.         rednet.send(storageComputerID, {
  27.             ["action"] = "get",
  28.             ["instructionRef"] = "outChestTopup",
  29.             ["chest"] = chest,
  30.             ["slot"] = slot,
  31.             ["name"] = itemName,
  32.             ["count"] = itemCount
  33.         }, "inv")
  34.         while true do
  35.             local id, msg = rednet.receive("invResp", 2)
  36.             if not id then
  37.                 print("Timed out")
  38.                 break
  39.             end
  40.             if id == storageComputerID then
  41.                 if msg["status"] == "success" then
  42.                     print("Got " .. tostring(msg["moved"]) .. " " .. chest .. " slot " .. tostring(slot))
  43.                     return msg["moved"]
  44.                 else
  45.                     print("NOT Got " .. chest .. " slot " .. tostring(slot) .. " - " .. msg["message"])
  46.                     return 0
  47.                 end
  48.             end
  49.             sleep(0)
  50.         end
  51.     end
  52. end
  53.  
  54. function Store(chest, slot, count)
  55.     while true do
  56.         print("Storing " .. tostring(count) .. " " .. chest .. " slot " .. tostring(slot))
  57.         rednet.send(storageComputerID, {
  58.             ["action"] = "store",
  59.             ["instructionRef"] = "BillPhone",
  60.             ["chest"] = chest,
  61.             ["slot"] = slot,
  62.             ["count"] = count
  63.         }, "inv")
  64.         while true do
  65.             local id, msg = rednet.receive("invResp", 2)
  66.             if not id then
  67.                 print("Timed out")
  68.                 break
  69.             end
  70.             if id == storageComputerID then
  71.                 if msg["status"] == "success" then
  72.                     print("Stored " .. tostring(msg["moved"]) .. " " .. chest .. " slot " .. tostring(slot))
  73.                     return msg["moved"]
  74.                 else
  75.                     print("NOT Stored " .. chest .. " slot " .. tostring(slot) .. " - " .. msg["message"])
  76.                     return 0
  77.                 end
  78.             end
  79.             sleep(0)
  80.         end
  81.         sleep(1)
  82.     end
  83. end
  84.  
  85. function Count(itemName)
  86.     while true do
  87.         print("Getting count of: " .. itemName)
  88.         rednet.send(storageComputerID, {
  89.             ["action"] = "count",
  90.             ["instructionRef"] = "BillPhone",
  91.             ["name"] = itemName
  92.         }, "inv")
  93.         while true do
  94.             local id, msg = rednet.receive("invResp", 2)
  95.             if not id then
  96.                 print("Timed out")
  97.                 break
  98.             end
  99.             if id == storageComputerID then
  100.                 if msg["status"] == "success" then
  101.                     print("Count: " .. tostring(msg["count"]))
  102.                     return msg["count"]
  103.                 else
  104.                     print("Get count failed: " .. msg["message"])
  105.                     return 0
  106.                 end
  107.             end
  108.             sleep(0)
  109.         end
  110.         sleep(1)
  111.     end
  112. end
  113.  
  114. function TableSize(tab)
  115.     if tab == nil then
  116.         return 0
  117.     end
  118.     local tabSize = 0
  119.     for aTab, bTab in pairs(tab) do
  120.         tabSize = tabSize + 1
  121.     end
  122.     return tabSize
  123. end
  124.  
  125. function Save()
  126.     local itemsFile = fs.open("items", "w")
  127.     itemsFile.write(textutils.serialise(items))
  128.     itemsFile.close()
  129. end
  130.  
  131. function Load()
  132.     local itemsFile = fs.open("items", "r")
  133.     if itemsFile == nil then
  134.         return
  135.     end
  136.     items = textutils.unserialise(itemsFile.readAll())
  137.     itemsFile.close()
  138. end
  139.  
  140. function UpdateSearchResults()
  141.     results = {}
  142.     for k, v in pairs(items.all) do
  143.         if string.find(string.lower(v), string.lower(query)) ~= nil then
  144.             table.insert(results, k)
  145.         end
  146.     end
  147.     if selectedIndex > TableSize(results) then
  148.         selectedIndex = TableSize(results)
  149.     end
  150.     SortResults()
  151. end
  152.  
  153. function SortResults()
  154.     local results2 = {}
  155.    
  156.     while TableSize(results) > 0 do
  157.         local shortestKey = nil
  158.         local shortest = nil
  159.         for k, v in pairs(results) do
  160.             if shortest == nil or string.len(v) < string.len(shortest) then
  161.                 shortest = v
  162.                 shortestKey = k
  163.             end
  164.         end
  165.         if shortest == nil then
  166.             break
  167.         end
  168.         table.insert(results2, shortest)
  169.         results[shortestKey] = nil
  170.     end
  171.    
  172.     results = results2
  173. end
  174.  
  175. function RenderResults()
  176.     local width, height = term.getSize()
  177.     term.clear()
  178.     term.setCursorPos(1, 1)
  179.     term.setTextColour(colors.orange)
  180.     if screen == "search" then
  181.         term.write("search: " .. query .. "_")
  182.     else
  183.         term.write(screen)
  184.     end
  185.     local resCount = TableSize(results)
  186.     term.setCursorPos(width - string.len(tostring(resCount)) + 1, 1)
  187.     term.write(resCount)
  188.     local line = 1
  189.     for k, v in pairs(results) do
  190.         line = line + 1
  191.         term.setCursorPos(1, line)
  192.         if selectedIndex + 1 == line then
  193.             term.setTextColour(colors.lightBlue)
  194.         else
  195.             term.setTextColour(colors.blue)
  196.         end
  197.         term.write(items.all[v])
  198.     end
  199. end
  200.  
  201. local up = 265
  202. local down = 264
  203. local left = 263
  204. local right = 262
  205. local delKey = 261
  206. local endKey = 269
  207. local pgDwnKey = 267
  208. local ctrlKey = 345
  209. local numInsKey = 320
  210. local insKey = 260
  211. local backspace = 259
  212. local enter = 257
  213. local tab = 258
  214. local homeKey = 268
  215.  
  216. function MenuKeys()
  217.     while true do
  218.         local event, key, is_held = os.pullEvent("key")
  219.         local resCount = TableSize(results)
  220.         if key == up then
  221.             selectedIndex = selectedIndex - 1
  222.             if selectedIndex < 1 then
  223.                 selectedIndex = 1
  224.             end
  225.             RenderResults()
  226.         elseif key == down then
  227.             selectedIndex = selectedIndex + 1
  228.             if selectedIndex > resCount then
  229.                 selectedIndex = resCount
  230.             end
  231.             if selectedIndex < 1 then
  232.                 selectedIndex = 1
  233.             end
  234.             RenderResults()
  235.         elseif key == left then
  236.             if screen == "favorites" then
  237.                 screen = "search"
  238.                 UpdateSearchResults()
  239.             end
  240.             selectedIndex = 1
  241.             RenderResults()
  242.         elseif key == right then
  243.             if screen == "search" then
  244.                 screen = "favorites"
  245.                 results = items.favorites
  246.             end
  247.             selectedIndex = 1
  248.             RenderResults()
  249.         elseif key == delKey then
  250.             if screen == "search" then
  251.                 query = ""
  252.                 UpdateSearchResults()
  253.                 RenderResults()
  254.             end
  255.         elseif key == backspace then
  256.             if screen == "search" then
  257.                 query = query:sub(1, -2)
  258.                 UpdateSearchResults()
  259.                 RenderResults()
  260.             end
  261.         elseif key == endKey then
  262.             if results[selectedIndex] ~= nil then
  263.                 term.clear()
  264.                 term.setCursorPos(1, 1)
  265.                 term.setTextColour(colors.green)
  266.                 Count(results[selectedIndex])
  267.                 term.setTextColour(colors.blue)
  268.                 print("")
  269.                 print("Press enter to close")
  270.                 read()
  271.                 RenderResults()
  272.             end
  273.         elseif key == enter then
  274.             if results[selectedIndex] ~= nil then
  275.                 term.clear()
  276.                 term.setCursorPos(1, 1)
  277.                 term.setTextColour(colors.green)
  278.                 Store(enderChest, 1, 64)
  279.                 Get(results[selectedIndex], 64, enderChest, 1)
  280.                 RenderResults()
  281.             end
  282.         elseif key == insKey then
  283.             term.clear()
  284.             term.setCursorPos(1, 1)
  285.             term.setTextColour(colors.green)
  286.             Store(enderChest, 1, 64)
  287.             RenderResults()
  288.         end
  289.     end
  290. end
  291.  
  292. function QueryInput()
  293.     while true do
  294.         local event, character = os.pullEvent("char")
  295.         if screen == "search" then
  296.             query = query .. character
  297.             UpdateSearchResults()
  298.             RenderResults()
  299.         end
  300.     end
  301. end
  302.  
  303. Load()
  304.  
  305. UpdateSearchResults()
  306. RenderResults()
  307.  
  308. parallel.waitForAny(MenuKeys, QueryInput)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement