Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local items = {
- ["all"] = {
- ["minecraft:dirt"] = "Dirt",
- ["minecraft:stone"] = "Stone",
- ["minecraft:cobblestone"] = "Cobblestone",
- },
- ["recents"] = {
- },
- ["favorites"] = {
- "minecraft:cobblestone"
- }
- }
- local results = {}
- local query = ""
- local selectedIndex = 1
- local screen = "search"--"favorites"
- local storageComputerID = 18
- local enderChest = "enderstorage:ender_chest_6"
- rednet.open("back")
- function Get(itemName, itemCount, chest, slot)
- while true do
- print("Getting " .. tostring(itemCount) .. " " .. chest .. " slot " .. tostring(slot))
- rednet.send(storageComputerID, {
- ["action"] = "get",
- ["instructionRef"] = "outChestTopup",
- ["chest"] = chest,
- ["slot"] = slot,
- ["name"] = itemName,
- ["count"] = itemCount
- }, "inv")
- while true do
- local id, msg = rednet.receive("invResp", 2)
- if not id then
- print("Timed out")
- break
- end
- if id == storageComputerID then
- if msg["status"] == "success" then
- print("Got " .. tostring(msg["moved"]) .. " " .. chest .. " slot " .. tostring(slot))
- return msg["moved"]
- else
- print("NOT Got " .. chest .. " slot " .. tostring(slot) .. " - " .. msg["message"])
- return 0
- end
- end
- sleep(0)
- end
- end
- end
- function Store(chest, slot, count)
- while true do
- print("Storing " .. tostring(count) .. " " .. chest .. " slot " .. tostring(slot))
- rednet.send(storageComputerID, {
- ["action"] = "store",
- ["instructionRef"] = "BillPhone",
- ["chest"] = chest,
- ["slot"] = slot,
- ["count"] = count
- }, "inv")
- while true do
- local id, msg = rednet.receive("invResp", 2)
- if not id then
- print("Timed out")
- break
- end
- if id == storageComputerID then
- if msg["status"] == "success" then
- print("Stored " .. tostring(msg["moved"]) .. " " .. chest .. " slot " .. tostring(slot))
- return msg["moved"]
- else
- print("NOT Stored " .. chest .. " slot " .. tostring(slot) .. " - " .. msg["message"])
- return 0
- end
- end
- sleep(0)
- end
- sleep(1)
- end
- end
- function Count(itemName)
- while true do
- print("Getting count of: " .. itemName)
- rednet.send(storageComputerID, {
- ["action"] = "count",
- ["instructionRef"] = "BillPhone",
- ["name"] = itemName
- }, "inv")
- while true do
- local id, msg = rednet.receive("invResp", 2)
- if not id then
- print("Timed out")
- break
- end
- if id == storageComputerID then
- if msg["status"] == "success" then
- print("Count: " .. tostring(msg["count"]))
- return msg["count"]
- else
- print("Get count failed: " .. msg["message"])
- return 0
- end
- end
- sleep(0)
- end
- sleep(1)
- end
- end
- function TableSize(tab)
- if tab == nil then
- return 0
- end
- local tabSize = 0
- for aTab, bTab in pairs(tab) do
- tabSize = tabSize + 1
- end
- return tabSize
- end
- function Save()
- local itemsFile = fs.open("items", "w")
- itemsFile.write(textutils.serialise(items))
- itemsFile.close()
- end
- function Load()
- local itemsFile = fs.open("items", "r")
- if itemsFile == nil then
- return
- end
- items = textutils.unserialise(itemsFile.readAll())
- itemsFile.close()
- end
- function UpdateSearchResults()
- results = {}
- for k, v in pairs(items.all) do
- if string.find(string.lower(v), string.lower(query)) ~= nil then
- table.insert(results, k)
- end
- end
- if selectedIndex > TableSize(results) then
- selectedIndex = TableSize(results)
- end
- SortResults()
- end
- function SortResults()
- local results2 = {}
- while TableSize(results) > 0 do
- local shortestKey = nil
- local shortest = nil
- for k, v in pairs(results) do
- if shortest == nil or string.len(v) < string.len(shortest) then
- shortest = v
- shortestKey = k
- end
- end
- if shortest == nil then
- break
- end
- table.insert(results2, shortest)
- results[shortestKey] = nil
- end
- results = results2
- end
- function RenderResults()
- local width, height = term.getSize()
- term.clear()
- term.setCursorPos(1, 1)
- term.setTextColour(colors.orange)
- if screen == "search" then
- term.write("search: " .. query .. "_")
- else
- term.write(screen)
- end
- local resCount = TableSize(results)
- term.setCursorPos(width - string.len(tostring(resCount)) + 1, 1)
- term.write(resCount)
- local line = 1
- for k, v in pairs(results) do
- line = line + 1
- term.setCursorPos(1, line)
- if selectedIndex + 1 == line then
- term.setTextColour(colors.lightBlue)
- else
- term.setTextColour(colors.blue)
- end
- term.write(items.all[v])
- end
- end
- local up = 265
- local down = 264
- local left = 263
- local right = 262
- local delKey = 261
- local endKey = 269
- local pgDwnKey = 267
- local ctrlKey = 345
- local numInsKey = 320
- local insKey = 260
- local backspace = 259
- local enter = 257
- local tab = 258
- local homeKey = 268
- function MenuKeys()
- while true do
- local event, key, is_held = os.pullEvent("key")
- local resCount = TableSize(results)
- if key == up then
- selectedIndex = selectedIndex - 1
- if selectedIndex < 1 then
- selectedIndex = 1
- end
- RenderResults()
- elseif key == down then
- selectedIndex = selectedIndex + 1
- if selectedIndex > resCount then
- selectedIndex = resCount
- end
- if selectedIndex < 1 then
- selectedIndex = 1
- end
- RenderResults()
- elseif key == left then
- if screen == "favorites" then
- screen = "search"
- UpdateSearchResults()
- end
- selectedIndex = 1
- RenderResults()
- elseif key == right then
- if screen == "search" then
- screen = "favorites"
- results = items.favorites
- end
- selectedIndex = 1
- RenderResults()
- elseif key == delKey then
- if screen == "search" then
- query = ""
- UpdateSearchResults()
- RenderResults()
- end
- elseif key == backspace then
- if screen == "search" then
- query = query:sub(1, -2)
- UpdateSearchResults()
- RenderResults()
- end
- elseif key == endKey then
- if results[selectedIndex] ~= nil then
- term.clear()
- term.setCursorPos(1, 1)
- term.setTextColour(colors.green)
- Count(results[selectedIndex])
- term.setTextColour(colors.blue)
- print("")
- print("Press enter to close")
- read()
- RenderResults()
- end
- elseif key == enter then
- if results[selectedIndex] ~= nil then
- term.clear()
- term.setCursorPos(1, 1)
- term.setTextColour(colors.green)
- Store(enderChest, 1, 64)
- Get(results[selectedIndex], 64, enderChest, 1)
- RenderResults()
- end
- elseif key == insKey then
- term.clear()
- term.setCursorPos(1, 1)
- term.setTextColour(colors.green)
- Store(enderChest, 1, 64)
- RenderResults()
- end
- end
- end
- function QueryInput()
- while true do
- local event, character = os.pullEvent("char")
- if screen == "search" then
- query = query .. character
- UpdateSearchResults()
- RenderResults()
- end
- end
- end
- Load()
- UpdateSearchResults()
- RenderResults()
- parallel.waitForAny(MenuKeys, QueryInput)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement