Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local MAIN_CHEST = "another_furniture:drawer_4"
- local CURR_DIR = fs.getDir(shell.getRunningProgram())
- local RECENTS_FILE = CURR_DIR.."/caches/recent_items.lua"
- local AMOUNTS_FILE = CURR_DIR.."/caches/amounts.lua"
- local CACHE_FILE = CURR_DIR.."/caches/cache.lua"
- local inv = require("limapi")
- local completion = require("cc.completion")
- inv.setMainChest(MAIN_CHEST)
- inv.registerChests()
- 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)
- local amounts_file = fs.open(file, "r")
- if amounts_file == nil then return end
- amounts_cache = textutils.unserialise(amounts_file.readAll())
- amounts_file.close()
- end
- local function saveAmounts(file)
- local amounts_file = fs.open(file,"w")
- amounts_file.write(textutils.serialise(amounts_cache))
- amounts_file.close()
- end
- local function updateRecents(item)
- if item == "" or item=="minecraft:" 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 pushItem(item,count)
- amounts_cache[item] = amounts_cache[item] or 0
- amounts_cache[item]=amounts_cache[item]+inv.pushItem(item,count)
- saveAmounts(AMOUNTS_FILE)
- inv.saveCache(CACHE_FILE)
- end
- local function pullItem(item,count)
- local n = inv.pullItem(item,count)
- if amounts_cache[item]==nil then
- if n>0 then
- local m = inv.countItem(item)
- if m>0 then
- amounts_cache[item]=m
- end
- end
- else
- amounts_cache[item]=amounts_cache[item]-n
- if amounts_cache[item]==0 then
- amounts_cache[item]=nil
- end
- end
- saveAmounts(AMOUNTS_FILE)
- inv.saveCache(CACHE_FILE)
- end
- local function returnItems()
- local r = inv.returnItems()
- for item,count in pairs(r) do
- if amounts_cache[item]==nil then
- amounts_cache[item]=0
- end
- amounts_cache[item]=amounts_cache[item]+count
- end
- saveAmounts(AMOUNTS_FILE)
- inv.saveCache(CACHE_FILE)
- end
- local function requestItemAmount(name)
- if name=="" or name=="minecraft:" then return end
- local item = name
- if not string.find(item, ":", 1, true) then
- item = "minecraft:"..item
- end
- if amounts_cache[item]~=nil then
- write("Amount: (default: 1, stored: "..amounts_cache[item]..") ")
- else
- local n = inv.countItem(item)
- if n>0 then
- amounts_cache[item] = n
- end
- if amounts_cache[item] == nil then return end
- write("Amount: (default: 1, stored: "..amounts_cache[item]..") ")
- end
- local am = tonumber(read()) or 1
- pullItem(item,am)
- updateRecents(item)
- end
- local function uiPullItem()
- print("Item ID:")
- local name = read()
- requestItemAmount(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
- requestItemAmount(name)
- end
- local function convertAmountsTable()
- amounts_cache = {}
- known_amount_items = {}
- local n = 1
- for item,count in pairs(inv.listItemsWithCount()) do
- amounts_cache[item] = count
- known_amount_items[n] = item
- n=n+1
- end
- return known_amount_items
- end
- local function uiListAll()
- local known_amount_items = {}
- for key in pairs(amounts_cache) do
- known_amount_items[#known_amount_items] = key
- end
- table.sort(known_amount_items)
- 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
- local name = known_amount_items[i]
- if name==nil then break end
- print(amounts_cache[name].." x "..name)
- 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...")
- known_amount_items = convertAmountsTable()
- table.sort(known_amount_items)
- saveAmounts(AMOUNTS_FILE)
- elseif ev[4]+scroll~=1 and ev[4]~=h then
- name = known_amount_items[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
- requestItemAmount(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:")
- requestItemAmount(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 function uiSortAllChests()
- print("Sit back and relax, this'll take a while")
- inv.sortAllChests()
- inv.createCache()
- inv.saveCache(CACHE_FILE)
- 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", returnItems},
- {"Restock",inv.restockMainChest},
- {"Refresh cache (debug)", inv.createCache},
- {"Sort all chests", uiSortAllChests},
- {"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)
- saveAmounts(AMOUNTS_FILE)
Advertisement
Add Comment
Please, Sign In to add comment