SuperMcBrent

app_inventory

Aug 18th, 2025 (edited)
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.04 KB | None | 0 0
  1. -- Inventory app
  2.  
  3. local invName = "create:item_vault_0" -- static inventory name for now
  4.  
  5. -- IDs for buttons
  6. local scanBtnId = "scan_inventory"
  7.  
  8. -- padding for top-left placement
  9. local PADDING_X, PADDING_Y = 3, 4
  10.  
  11. -- state variables
  12. local scanResults = nil
  13. local scanRunning = false
  14.  
  15. -- views
  16. local function rootView(ctx)
  17.     return {
  18.         draw = function(ctx, mon)
  19.             local btnW, btnH = 12, 3
  20.             local x, y = PADDING_X, PADDING_Y
  21.  
  22.             -- update label/color if scanning
  23.             local label = scanRunning and "Scanning..." or "Scan"
  24.             local color = scanRunning and colors.yellow or colors.lime
  25.  
  26.             ctx.libs().button.addButton(x, y, btnW, btnH, scanBtnId, color, colors.gray, true, label, "", x + 2, y + 1,
  27.                 "")
  28.             ctx.libs().button.drawButton(scanBtnId, mon)
  29.         end,
  30.         touch = function(ctx, x, y)
  31.             if ctx.libs().button.isWithinBoundingBox(x, y, scanBtnId) and not scanRunning then
  32.                 scanRunning = true
  33.                 scanResults = nil
  34.  
  35.                 -- define the job
  36.                 local job = function(done_callback)
  37.                     local inv = peripheral.wrap(invName)
  38.                     if not inv or not inv.list then
  39.                         print("Could not access inventory: " .. invName)
  40.                         done_callback()
  41.                         return
  42.                     end
  43.  
  44.                     local totals = {}
  45.                     for slot, item in pairs(inv.list()) do
  46.                         totals[item.name] = (totals[item.name] or 0) + item.count
  47.                     end
  48.  
  49.                     -- call the callback when finished
  50.                     done_callback(totals)
  51.                 end
  52.  
  53.                 -- submit the job to the system via ctx
  54.                 if ctx.os.submitJob then
  55.                     ctx.os.submitJob(job, function(results)
  56.                         scanResults = results
  57.                         scanRunning = false
  58.  
  59.                         print("\nScan complete:")
  60.                         for name, count in pairs(scanResults) do
  61.                             print((" - %s: %d"):format(name, count))
  62.                         end
  63.                     end)
  64.                 else
  65.                     print("System does not expose submitJob")
  66.                     scanRunning = false
  67.                 end
  68.             end
  69.         end
  70.     }
  71. end
  72.  
  73. local views = {
  74.     root = rootView
  75. }
  76.  
  77. return {
  78.     id = "inventory",
  79.     name = "Inventory",
  80.     protocol = "inventory",
  81.     receive = function(ctx, sender, message) end,
  82.     create = function(ctx) end,
  83.     destroy = function(ctx) end,
  84.     resume = function(ctx) end,
  85.     suspend = function(ctx) end,
  86.     update = function(ctx, dt) end,
  87.     draw = function(ctx, mon, viewId)
  88.         local v = views[viewId]
  89.         if v and v(ctx).draw then v(ctx).draw(ctx, mon) end
  90.     end,
  91.     touch = function(ctx, x, y, viewId)
  92.         local v = views[viewId]
  93.         if v and v(ctx).touch then v(ctx).touch(ctx, x, y) end
  94.     end
  95. }
  96.  
Advertisement
Add Comment
Please, Sign In to add comment