Advertisement
AdditionalPylons

limapi_ui

Mar 29th, 2025 (edited)
404
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.78 KB | None | 0 0
  1. local MAIN_CHEST = "another_furniture:drawer_6"
  2. local CURR_DIR = fs.getDir(shell.getRunningProgram())
  3. local RECENTS_FILE = CURR_DIR.."/caches/recent_items.lua"
  4. local AMOUNTS_FILE = CURR_DIR.."/caches/amounts.txt"
  5. local CACHE_FILE = CURR_DIR.."/caches/cache.lua"
  6.  
  7. local inv = require("limapi")
  8. local completion = require("cc.completion")
  9. inv.registerChests()
  10. inv.setMainChest(MAIN_CHEST)
  11.  
  12. local w,h = term.getSize()
  13.  
  14. inv.loadCache(CACHE_FILE)
  15. local recently_pulled = {}
  16. local amounts_cache = {}
  17. local function getRecents(file)
  18.     local recents_file = fs.open(file,"r")
  19.     if recents_file == nil then return end
  20.     recently_pulled = textutils.unserialise(recents_file.readAll())
  21.     recents_file.close()
  22. end
  23. local function saveRecents(file)
  24.     local recents_file = fs.open(file,"w")
  25.     recents_file.write(textutils.serialise(recently_pulled))
  26.     recents_file.close()
  27. end
  28. local function getAmounts(file)
  29.     amounts_cache = {}
  30.     local amounts_file = fs.open(file, "r")
  31.     if amounts_file == nil then return end
  32.     local l = 0
  33.     while true do
  34.         local line = amounts_file.readLine()
  35.         if not line then break end
  36.         l = l + 1
  37.         amounts_cache[l] = line
  38.     end
  39.     amounts_file.close()
  40. end
  41. local function saveAmounts(file)
  42.     local amounts_file = fs.open(file,"w")
  43.     for i=1,#amounts_cache do
  44.         amounts_file.write(amounts_cache[i].."\n")
  45.     end
  46.     amounts_file.close()
  47. end
  48. local function updateRecents(item)
  49.     if item == "" then return end
  50.     local found = false
  51.     for i=1,#recently_pulled do
  52.         if item == recently_pulled[i] then
  53.             found = true
  54.             for j=i-1,1,-1 do
  55.                 recently_pulled[j+1]=recently_pulled[j]
  56.             end
  57.             break
  58.         end
  59.     end
  60.     if not found then
  61.         for i=#recently_pulled,1,-1 do
  62.             recently_pulled[i+1]=recently_pulled[i]
  63.         end
  64.     end
  65.     recently_pulled[1] = item
  66.     recently_pulled[40] = nil
  67.     saveRecents(RECENTS_FILE)
  68. end
  69. getRecents(RECENTS_FILE)
  70. getAmounts(AMOUNTS_FILE)
  71.  
  72. local function uiPullItem()
  73.     print("Item ID:")
  74.     local name = read()
  75.     write("Amount: (default: 1) ")
  76.     local am = tonumber(read()) or 1
  77.    
  78.     if not string.find(name, ":", 1, true) then
  79.         name = "minecraft:"..name
  80.     end
  81.     inv.pullItem(name,am)
  82.     updateRecents(name)
  83. end
  84. local function uiRecentItem()
  85.     local scroll=0
  86.     local name
  87.     term.setBackgroundColour(colours.black)
  88.     while true do
  89.         term.setCursorPos(1,1)
  90.         term.clear()
  91.         for i=1+scroll,scroll+h-1 do
  92.             if recently_pulled[i]==nil then break end
  93.             print("\x07 "..recently_pulled[i])
  94.         end
  95.         term.setCursorPos(w,1)
  96.         write("x")
  97.         local ev = {os.pullEvent()}
  98.         if ev[1]=="mouse_scroll" then
  99.             scroll=scroll+ev[2]
  100.             if scroll<0 then scroll=0 end
  101.         elseif ev[1]=="mouse_click" or ev[1]=="monitor_touch" then
  102.             if ev[3]==w and ev[4]==1 then
  103.                 return
  104.             end
  105.             if ev[3]==1 then
  106.                 table.remove(recently_pulled,ev[4]+scroll)
  107.             else
  108.                 name = recently_pulled[ev[4]+scroll]
  109.                 if name~=nil then
  110.                     term.setBackgroundColour(colours.blue)
  111.                     paintutils.drawFilledBox(1,1,w,ev[4]-1)
  112.                     paintutils.drawFilledBox(1,ev[4]+1,w,h)
  113.                     term.setCursorPos(1,ev[4]+1)
  114.                     break
  115.                 end
  116.             end
  117.         end
  118.     end
  119.     write("Amount: (default: 1) ")
  120.     local am = tonumber(read()) or 1
  121.     inv.pullItem(name,am)
  122.     updateRecents(name)
  123. end
  124. local function convertAmountsTable()
  125.     amounts_cache = {"Last updated at "..os.date('%Y-%m-%d %H:%M:%S')}
  126.     local n = 1
  127.     for item,count in pairs(inv.listItemsWithCount()) do
  128.         n = n+1
  129.         amounts_cache[n] = count.." x "..item
  130.     end
  131. end
  132. local function uiListAll()
  133.     local scroll=0
  134.     local name
  135.     term.setBackgroundColour(colours.black)
  136.     while true do
  137.         term.setCursorPos(1,1)
  138.         term.clear()
  139.         for i=1+scroll,scroll+h-1 do
  140.             if amounts_cache[i]==nil then break end
  141.             print(amounts_cache[i])
  142.         end
  143.         term.setCursorPos(w-2,1)
  144.         write("@ x")
  145.         local ev = {os.pullEvent()}
  146.         if ev[1]=="mouse_scroll" then
  147.             scroll=scroll+ev[2]
  148.             if scroll<0 then scroll=0 end
  149.         elseif ev[1]=="mouse_click" or ev[1]=="monitor_touch" then
  150.             if ev[3]==w and ev[4]==1 then
  151.                 return
  152.             end
  153.             if ev[3]==w-2 and ev[4]==1 then
  154.                 term.setCursorPos(1,h)
  155.                 print("Recaching all stored values, this may take a while...")
  156.                 convertAmountsTable()
  157.                 saveAmounts(AMOUNTS_FILE)
  158.             elseif ev[4]+scroll~=1 and ev[4]~=h then
  159.                 name = amounts_cache[ev[4]+scroll]
  160.                 if name~=nil then
  161.                     name = name:match("%d+ x (.+)")
  162.                     term.setBackgroundColour(colours.blue)
  163.                     paintutils.drawFilledBox(1,1,w,ev[4]-1)
  164.                     paintutils.drawFilledBox(1,ev[4]+1,w,h)
  165.                     term.setCursorPos(1,ev[4]+1)
  166.                     break
  167.                 end
  168.             end
  169.         end
  170.     end
  171.     write("Amount: (default: 1) ")
  172.     local am = tonumber(read()) or 1
  173.     inv.pullItem(name,am)
  174.     updateRecents(name)
  175. end
  176. local function uiSearch()
  177.     local items = inv.listItems()
  178.     print("Search an item")
  179.     local name = read(nil,nil,function(p) return completion.choice(p,items) end, "minecraft:")
  180.     write("\nAmount: (default: 1) ")
  181.     local am = tonumber(read()) or 1
  182.     inv.pullItem(name,am)
  183.     updateRecents(name)
  184. end
  185. local function uiHelp()
  186.     term.setBackgroundColour(colours.black)
  187.     term.clear()
  188.     term.setCursorPos(1,1)
  189.     print("Register new chests - add new chests to the system.")
  190.     print("Pull an item - pull item by id. \"minecraft:\" is automatically added.")
  191.     print("Pull a recent item - show a list of your recent lookups. Tip: press the \x07 to remove entry.")
  192.     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 @.")
  193.     print("Search - same as \"pull an item\", except with autocompletion.")
  194.     print("Return all items - all the items in the main chest are pushed into the system.")
  195.     print("Restock - all the items in the main chest are filled to the full stack.")
  196.     print("Refresh cache - debug action, use when manually tampering with the system.")
  197.     write("To select the main chest, edit the first line of this file. DO NOT PLACE IT ADJACENTLY TO THE COMPUTER.")
  198.     sleep(1)
  199.     os.pullEvent()    
  200. end
  201. local shutdown = false
  202. local buttons = {
  203.     {"Register new chests", inv.registerChests},
  204.     {"Pull an item", uiPullItem},
  205.     {"Pull a recent item", uiRecentItem},
  206.     {"See the full list", uiListAll},
  207.     {"Search",uiSearch},
  208.     {"Return all items", inv.returnItems},
  209.     {"Restock",inv.restockMainChest},
  210.     {"Refresh cache (debug)", inv.createCache},
  211.     {"Help",uiHelp},
  212.     {"Exit", function() shutdown = true end}
  213. }
  214. while true do
  215.     if shutdown then break end
  216.     term.setBackgroundColour(colours.grey)
  217.     term.clear()
  218.     term.setBackgroundColour(colours.blue)
  219.     term.setCursorPos(1,1)
  220.     for i=1,#buttons do
  221.         print("\x07 "..buttons[i][1])
  222.     end
  223.     local ev = {os.pullEvent()}
  224.     if ev[1]=="monitor_touch" or ev[1]=="mouse_click" then
  225.         local c = buttons[ev[4]]
  226.         if c~=nil then
  227.             c[2]()
  228.             term.setCursorPos(w,h)
  229.         end
  230.     end
  231. end
  232. term.setBackgroundColour(colours.black)
  233. term.clear()
  234. term.setCursorPos(1,1)
  235. inv.saveCache(CACHE_FILE)
  236.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement