Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local MAIN_CHEST = "another_furniture:drawer_6"
- local CURR_DIR = fs.getDir(shell.getRunningProgram())
- local RECENTS_FILE = CURR_DIR.."/caches/recent_items.lua"
- local AMOUNTS_FILE = CURR_DIR.."/caches/amounts.txt"
- local CACHE_FILE = CURR_DIR.."/caches/cache.lua"
- local inv = require("limapi")
- local completion = require("cc.completion")
- inv.registerChests()
- inv.setMainChest(MAIN_CHEST)
- local w,h = term.getSize()
- inv.loadCache(CACHE_FILE)
- local recently_pulled = {}
- local amounts_cache = {}
- local function getRecents(file)
- local recents_file = fs.open(file,"r")
- if recents_file == nil then return end
- recently_pulled = textutils.unserialise(recents_file.readAll())
- recents_file.close()
- end
- local function saveRecents(file)
- local recents_file = fs.open(file,"w")
- recents_file.write(textutils.serialise(recently_pulled))
- recents_file.close()
- end
- local function getAmounts(file)
- amounts_cache = {}
- local amounts_file = fs.open(file, "r")
- if amounts_file == nil then return end
- local l = 0
- while true do
- local line = amounts_file.readLine()
- if not line then break end
- l = l + 1
- amounts_cache[l] = line
- end
- amounts_file.close()
- end
- local function saveAmounts(file)
- local amounts_file = fs.open(file,"w")
- for i=1,#amounts_cache do
- amounts_file.write(amounts_cache[i].."\n")
- end
- amounts_file.close()
- end
- local function updateRecents(item)
- if item == "" then return end
- local found = false
- for i=1,#recently_pulled do
- if item == recently_pulled[i] then
- found = true
- for j=i-1,1,-1 do
- recently_pulled[j+1]=recently_pulled[j]
- end
- break
- end
- end
- if not found then
- for i=#recently_pulled,1,-1 do
- recently_pulled[i+1]=recently_pulled[i]
- end
- end
- recently_pulled[1] = item
- recently_pulled[40] = nil
- saveRecents(RECENTS_FILE)
- end
- getRecents(RECENTS_FILE)
- getAmounts(AMOUNTS_FILE)
- local function uiPullItem()
- print("Item ID:")
- local name = read()
- write("Amount: (default: 1) ")
- local am = tonumber(read()) or 1
- if not string.find(name, ":", 1, true) then
- name = "minecraft:"..name
- end
- inv.pullItem(name,am)
- updateRecents(name)
- end
- local function uiRecentItem()
- local scroll=0
- local name
- term.setBackgroundColour(colours.black)
- while true do
- term.setCursorPos(1,1)
- term.clear()
- for i=1+scroll,scroll+h-1 do
- if recently_pulled[i]==nil then break end
- print("\x07 "..recently_pulled[i])
- end
- term.setCursorPos(w,1)
- write("x")
- local ev = {os.pullEvent()}
- if ev[1]=="mouse_scroll" then
- scroll=scroll+ev[2]
- if scroll<0 then scroll=0 end
- elseif ev[1]=="mouse_click" or ev[1]=="monitor_touch" then
- if ev[3]==w and ev[4]==1 then
- return
- end
- if ev[3]==1 then
- table.remove(recently_pulled,ev[4]+scroll)
- else
- name = recently_pulled[ev[4]+scroll]
- if name~=nil then
- term.setBackgroundColour(colours.blue)
- paintutils.drawFilledBox(1,1,w,ev[4]-1)
- paintutils.drawFilledBox(1,ev[4]+1,w,h)
- term.setCursorPos(1,ev[4]+1)
- break
- end
- end
- end
- end
- write("Amount: (default: 1) ")
- local am = tonumber(read()) or 1
- inv.pullItem(name,am)
- updateRecents(name)
- end
- local function convertAmountsTable()
- amounts_cache = {"Last updated at "..os.date('%Y-%m-%d %H:%M:%S')}
- local n = 1
- for item,count in pairs(inv.listItemsWithCount()) do
- n = n+1
- amounts_cache[n] = count.." x "..item
- end
- end
- local function uiListAll()
- local scroll=0
- local name
- term.setBackgroundColour(colours.black)
- while true do
- term.setCursorPos(1,1)
- term.clear()
- for i=1+scroll,scroll+h-1 do
- if amounts_cache[i]==nil then break end
- print(amounts_cache[i])
- end
- term.setCursorPos(w-2,1)
- write("@ x")
- local ev = {os.pullEvent()}
- if ev[1]=="mouse_scroll" then
- scroll=scroll+ev[2]
- if scroll<0 then scroll=0 end
- elseif ev[1]=="mouse_click" or ev[1]=="monitor_touch" then
- if ev[3]==w and ev[4]==1 then
- return
- end
- if ev[3]==w-2 and ev[4]==1 then
- term.setCursorPos(1,h)
- print("Recaching all stored values, this may take a while...")
- convertAmountsTable()
- saveAmounts(AMOUNTS_FILE)
- elseif ev[4]+scroll~=1 and ev[4]~=h then
- name = amounts_cache[ev[4]+scroll]
- if name~=nil then
- name = name:match("%d+ x (.+)")
- term.setBackgroundColour(colours.blue)
- paintutils.drawFilledBox(1,1,w,ev[4]-1)
- paintutils.drawFilledBox(1,ev[4]+1,w,h)
- term.setCursorPos(1,ev[4]+1)
- break
- end
- end
- end
- end
- write("Amount: (default: 1) ")
- local am = tonumber(read()) or 1
- inv.pullItem(name,am)
- updateRecents(name)
- end
- local function uiSearch()
- local items = inv.listItems()
- print("Search an item")
- local name = read(nil,nil,function(p) return completion.choice(p,items) end, "minecraft:")
- write("\nAmount: (default: 1) ")
- local am = tonumber(read()) or 1
- inv.pullItem(name,am)
- updateRecents(name)
- end
- local function uiHelp()
- term.setBackgroundColour(colours.black)
- term.clear()
- term.setCursorPos(1,1)
- print("Register new chests - add new chests to the system.")
- print("Pull an item - pull item by id. \"minecraft:\" is automatically added.")
- print("Pull a recent item - show a list of your recent lookups. Tip: press the \x07 to remove entry.")
- print("See the full list - see all the items in the system with their amounts. Since it's a costly operation, you must refresh it manually by pressing @.")
- print("Search - same as \"pull an item\", except with autocompletion.")
- print("Return all items - all the items in the main chest are pushed into the system.")
- print("Restock - all the items in the main chest are filled to the full stack.")
- print("Refresh cache - debug action, use when manually tampering with the system.")
- write("To select the main chest, edit the first line of this file. DO NOT PLACE IT ADJACENTLY TO THE COMPUTER.")
- sleep(1)
- os.pullEvent()
- end
- local shutdown = false
- local buttons = {
- {"Register new chests", inv.registerChests},
- {"Pull an item", uiPullItem},
- {"Pull a recent item", uiRecentItem},
- {"See the full list", uiListAll},
- {"Search",uiSearch},
- {"Return all items", inv.returnItems},
- {"Restock",inv.restockMainChest},
- {"Refresh cache (debug)", inv.createCache},
- {"Help",uiHelp},
- {"Exit", function() shutdown = true end}
- }
- while true do
- if shutdown then break end
- term.setBackgroundColour(colours.grey)
- term.clear()
- term.setBackgroundColour(colours.blue)
- term.setCursorPos(1,1)
- for i=1,#buttons do
- print("\x07 "..buttons[i][1])
- end
- local ev = {os.pullEvent()}
- if ev[1]=="monitor_touch" or ev[1]=="mouse_click" then
- local c = buttons[ev[4]]
- if c~=nil then
- c[2]()
- term.setCursorPos(w,h)
- end
- end
- end
- term.setBackgroundColour(colours.black)
- term.clear()
- term.setCursorPos(1,1)
- inv.saveCache(CACHE_FILE)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement