Advertisement
joebodo

organizer.lua

Apr 30th, 2014
385
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.86 KB | None | 0 0
  1. vos.loadApi('apis.lua')
  2.  
  3. local chest = Peripheral.wrap({ alias = 'chest' })
  4.  
  5. Logger.disable()
  6.  
  7. function learn()
  8.   listPage.grid.t = { }
  9.   local t = chest.getAllStacks()
  10.   for k,slot in pairs(t) do
  11.     table.insert(listPage.grid.t, {
  12.       name = slot.name,
  13.       slotNo = k
  14.     })
  15.   end
  16.   Util.writeTable('inventory.table', listPage.grid.t)
  17. end
  18.  
  19. --[[-- listPage --]]--
  20. listPage = UI.Page({
  21.   titleBar = UI.TitleBar({
  22.     title = 'Organizer'
  23.   }),
  24.   grid = UI.ScrollingGrid({
  25.     columns = {
  26.       { 'Name', 'name', UI.term.width-7 },
  27.       { 'Slot', 'slotNo', 4 }
  28.     },  
  29.     sortColumn = 'name',
  30.     pageSize = UI.term.height-3,
  31.     y = 2
  32.   }),
  33.   statusBar = UI.StatusBar({
  34.     status = 'l to learn'
  35.   }),
  36.   accelerators = {
  37.     l = 'learn',
  38.     q = 'quit'
  39.   }
  40. })
  41.  
  42. function listPage:eventHandler(event)
  43.   if event.type == 'quit' then
  44.     Event.exitPullEvents()
  45.   elseif event.type == 'learn' then
  46.     learn()
  47.     self.grid:draw()
  48.   end
  49.   return UI.Page.eventHandler(self, event)
  50. end
  51.  
  52. Event.addHandler('turtle_inventory', function()
  53.   local tslots = TL2.getFilledSlots()
  54.  
  55.   local function getOpenSlot()
  56.     for i = 1, chest.getInventorySize() do
  57.       if not chest.getStackInSlot(i) then
  58.         return i
  59.       end
  60.     end
  61.   end
  62.  
  63.   for _,tslot in pairs(tslots) do
  64.     local chestSlot = getOpenSlot()
  65.     chest.pullItemIntoSlot('down', tslot.slotNo, 1, chestSlot)
  66.     local slot = chest.getStackInSlot(chestSlot)
  67.     for k,s in pairs(listPage.grid.t) do
  68.       if s.name == slot.name then
  69.         if not chest.getStackInSlot(s.slotNo) then
  70.           chest.swapStacks(chestSlot, s.slotNo)
  71.           break
  72.         end
  73.       end
  74.     end
  75.   end
  76. end)
  77.  
  78. listPage.grid.t = Util.readTable('inventory.table', slots)
  79. if Util.empty(listPage.grid.t) then
  80.   learn()
  81. end
  82.  
  83. UI.pager:setPage(listPage)
  84.  
  85. Event.pullEvents()
  86. UI.term:clear()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement