Advertisement
joebodo

apps.Pim.lua

Sep 15th, 2016
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.14 KB | None | 0 0
  1. local Event = require('event')
  2. local UI = require('ui')
  3. local Config = require('config')
  4.  
  5. multishell.setTitle(multishell.getCurrent(), 'PIM')
  6.  
  7. local inventory = { }
  8. local mode = 'sync'
  9.  
  10. if not device.pim then
  11.   error('PIM not attached')
  12. end
  13.  
  14. local page = UI.Page({
  15.   menu = UI.Menu({
  16.     centered = true,
  17.     y = 2,
  18.     menuItems = {
  19.       { prompt = 'Learn', event = 'learn', help = '' },
  20.     },
  21.   }),
  22.   statusBar = UI.StatusBar({
  23.     columns = {
  24.       { 'Status', 'status', UI.term.width - 7 },
  25.       { 'Mode', 'mode', 7 }
  26.     }
  27.   }),
  28.   accelerators = {
  29.     q = 'quit',
  30.   },
  31. })
  32.  
  33. local function learn()
  34.   if device.pim.getInventorySize() > 0 then
  35.     local stacks = device.pim.getAllStacks(false)
  36.     Config.update('pim', stacks)
  37.     mode = 'sync'
  38.     page.statusBar:setValue('status', 'Learned inventory')
  39.   end
  40.   page.statusBar:setValue('mode', mode)
  41.   page.statusBar:draw()
  42. end
  43.  
  44. function page:eventHandler(event)
  45.  
  46.   if event.type == 'learn' then
  47.     mode = 'learn'
  48.     learn()
  49.   elseif event.type == 'quit' then
  50.     Event.exitPullEvents()
  51.   end
  52.  
  53.   return UI.Page.eventHandler(self, event)
  54. end
  55.  
  56. local function inInventory(s)
  57.   for _,i in pairs(inventory) do
  58.     if i.id == s.id then
  59.       return true
  60.     end
  61.   end
  62. end
  63.  
  64. local function pimWatcher()
  65.   local playerOn = false
  66.  
  67.   while true do
  68.     if device.pim.getInventorySize() > 0 and not playerOn then
  69.       playerOn = true
  70.      
  71.       if mode == 'learn' then
  72.         learn()
  73.  
  74.       else
  75.         local stacks = device.pim.getAllStacks(false)
  76.         for k,stack in pairs(stacks) do
  77.           if not inInventory(stack) then
  78.             device.pim.pushItem('down', k, stack.qty)
  79.           end
  80.         end
  81.         page.statusBar:setValue('status', 'Synchronized')
  82.         page.statusBar:draw()
  83.       end
  84.    
  85.     elseif device.pim.getInventorySize() == 0 and playerOn then
  86.       page.statusBar:setValue('status', 'No player')
  87.       page.statusBar:draw()
  88.       playerOn = false
  89.     end
  90.     os.sleep(1)
  91.   end
  92. end
  93.  
  94. Config.load('pim', inventory)
  95.  
  96. if Util.empty(inventory) then
  97.   mode = 'learn'
  98. end
  99. page.statusBar:setValue('mode', mode)
  100.  
  101. UI.pager:setPage(page)
  102.  
  103. Event.pullEvents(pimWatcher)
  104. UI.term:reset()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement