AdditionalPylons

limapi_ui

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