Advertisement
Zeehao

InventoryPull

May 25th, 2024 (edited)
609
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 15.46 KB | Gaming | 0 0
  1. local component = require("component")
  2. local event = require("event")
  3. local math = require("math")
  4. local sides = require("sides")
  5. local serialization = require("serialization")
  6.  
  7. local CircleQueue = require("circulateQueue")
  8. local Screen = require("gui").Screen
  9. local Section = require("gui").Section
  10. local ScrollBar = require("gui").ScrollBar
  11. local COLORS = require("gui").COLORS
  12. local TextArea = require("gui").TextArea
  13. local List = require("gui").List
  14. local Label = require("gui").Label
  15. local Button = require("gui").Button
  16.  
  17. local loggers = require("logger")
  18. local logger = loggers.getLogger("InventoryPull")
  19.  
  20. local transposer = component.transposer
  21.  
  22. -- local transposer = {
  23. --     getInventorySize = function(side)
  24. --         return 21
  25. --     end,
  26. --     getStackInSlot = function(side, slot)
  27. --         return { name = "minecraft:stone_" .. slot % 10, label = "Stone_" .. slot % 10, size = 64 }
  28. --     end,
  29. --     transferItem = function(sideA, sideB, size, slotA, slotB)
  30. --         return 1
  31. --     end,
  32. --     getAllStacks = function(side)
  33. --         return function()
  34. --             return nil
  35. --         end
  36. --     end
  37. -- }
  38.  
  39. local inventorySide = sides.north
  40. local pullside = sides.top
  41. local fullOxygenTankChestSide = sides.south
  42. local oxygenTankInfo = transposer.getStackInSlot(fullOxygenTankChestSide, 1)
  43.  
  44. local function saveData(path, data)
  45.     local file = io.open(path, "w")
  46.     if file then
  47.         file:write(data)
  48.         file:close()
  49.     end
  50. end
  51.  
  52. local function readData(path)
  53.     local file = io.open(path, "r")
  54.     if file then
  55.         local str = file:read("*all")
  56.         file:close()
  57.         return str
  58.     end
  59. end
  60.  
  61. local moveItem = function(sideA, slotA, sideB, size)
  62.     local invSize = transposer.getInventorySize(sideB);
  63.     for slotB = 1, invSize do
  64.         local transAmt = transposer.transferItem(sideA, sideB, size, slotA, slotB)
  65.         if size - transAmt == 0 then
  66.             return true
  67.         end
  68.     end
  69.     return false
  70. end
  71.  
  72. local findFullOxygenTankSlot = function()
  73.     local slotInfo = nil
  74.     local count = 0
  75.     local chestItem = transposer.getAllStacks(fullOxygenTankChestSide)
  76.     repeat
  77.         count = count + 1
  78.         slotInfo = chestItem()
  79.         if slotInfo ~= nil and slotInfo.name == oxygenTankInfo.name and slotInfo.damage == 0 then
  80.             return count
  81.         end
  82.     until slotInfo == nil
  83. end
  84.  
  85. local data = {}
  86.  
  87. data.pullItems = {
  88.     items = {},
  89.     sortedItems = {},
  90.     displayItems = {},
  91.     selectedLabel = nil,
  92.     currentPage = 1,
  93.     maxPage = 1,
  94.     maxDisplayItems = 22,
  95.     setMaxPage = function(self)
  96.         self.maxPage = math.max(math.ceil(#self.sortedItems / self.maxDisplayItems), 1)
  97.     end,
  98.     add = function(self, item)
  99.         self.sortedItems = {}
  100.         if self.items[item.name] ~= nil then
  101.             return
  102.         end
  103.         self.items[item.name] = item
  104.         for _, value in pairs(self.items) do
  105.             table.insert(self.sortedItems, value)
  106.         end
  107.         self:setMaxPage()
  108.         table.sort(self.sortedItems, function(a, b) return a.label < b.label end)
  109.         self:setDisplayItems()
  110.     end,
  111.     remove = function(self)
  112.         self.sortedItems = {}
  113.         if self.selectedLabel == nil then
  114.             return
  115.         end
  116.         self.items[self.displayItems[self.selectedLabel.idx].name] = nil
  117.         for _, value in pairs(self.items) do
  118.             table.insert(self.sortedItems, value)
  119.         end
  120.         self:setMaxPage()
  121.         table.sort(self.sortedItems, function(a, b) return a.label < b.label end)
  122.         self:unselected()
  123.         self:setDisplayItems()
  124.     end,
  125.     setDisplayItems = function(self)
  126.         self.displayItems = {}
  127.         if self.currentPage > self.maxPage then
  128.             self.currentPage = self.maxPage
  129.         end
  130.         for i = (self.currentPage - 1) * self.maxDisplayItems + 1, self.currentPage * self.maxDisplayItems do
  131.             if self.sortedItems[i] ~= nil then
  132.                 table.insert(self.displayItems, self.sortedItems[i])
  133.             end
  134.         end
  135.         if self.selectedLabel ~= nil and self.selectedLabel ~= nil then
  136.             self.selectedLabel:setBackgroundColor(COLORS.grey)
  137.             self.selectedLabel = nil
  138.         end
  139.     end,
  140.     setPage = function(self, page)
  141.         if page == self.currentPage then
  142.             return
  143.         end
  144.         self.currentPage = page
  145.         self.displayItems = {}
  146.         self:setDisplayItems();
  147.     end,
  148.     unselected = function(self)
  149.         if self.selectedLabel ~= nil then
  150.             self.selectedLabel:setBackgroundColor(COLORS.grey)
  151.             self.selectedLabel = nil
  152.         end
  153.     end,
  154. }
  155.  
  156. data.inventoryItems = {
  157.     items = {},
  158.     sortedItems = {},
  159.     displayItems = {},
  160.     selectedLabel = nil,
  161.     currentPage = 1,
  162.     maxPage = 1,
  163.     maxDisplayItems = 22,
  164.     removeItemForInventoryIfInPullList = function(self, item, slot)
  165.         if item == nil then
  166.             return nil
  167.         end
  168.         if data.pullItems.items[item.name] ~= nil then
  169.             if moveItem(inventorySide, slot, pullside, 64) then
  170.                 logger:print("move item: " .. item.name .. " to pull side")
  171.                 return nil
  172.             else
  173.                 return item
  174.             end
  175.         end
  176.         return item
  177.     end,
  178.     replaceOxygenTank = function(self, item, slot)
  179.         if item == nil then
  180.             return nil
  181.         end
  182.         if item.name == oxygenTankInfo.name and item.damage ~= 0 then
  183.             local chestSlot = findFullOxygenTankSlot()
  184.             if chestSlot == nil then
  185.                 return nil
  186.             end
  187.             moveItem(inventorySide, slot, fullOxygenTankChestSide, 1)
  188.             moveItem(fullOxygenTankChestSide, chestSlot, inventorySide, 1)
  189.             logger:print("move oxygen tank from player inventory: " .. slot .. " to refill-Chest: " .. chestSlot)
  190.             return item
  191.         end
  192.         return item
  193.     end,
  194.     refresh = function(self)
  195.         local size, err = transposer.getInventorySize(inventorySide);
  196.         self.items = {}
  197.         self.sortedItems = {}
  198.         for i = 1, size do
  199.             local item = transposer.getStackInSlot(inventorySide, i)
  200.             item = self:replaceOxygenTank(item, i)
  201.             item = self:removeItemForInventoryIfInPullList(item, i)
  202.             if item ~= nil and self.items[item.name] == nil then
  203.                 self.items[item.name] = item
  204.                 table.insert(self.sortedItems, item)
  205.             end
  206.         end
  207.         table.sort(self.sortedItems, function(a, b) return a.label < b.label end)
  208.         self.maxPage = math.ceil(#self.sortedItems / self.maxDisplayItems)
  209.         self:setDisplayItems()
  210.     end,
  211.     unselected = function(self)
  212.         if self.selectedLabel ~= nil then
  213.             self.selectedLabel:setBackgroundColor(COLORS.grey)
  214.             self.selectedLabel = nil
  215.         end
  216.     end,
  217.     setDisplayItems = function(self)
  218.         self.displayItems = {}
  219.         if self.currentPage > self.maxPage then
  220.             self.currentPage = self.maxPage
  221.         end
  222.         for i = (self.currentPage - 1) * self.maxDisplayItems + 1, self.currentPage * self.maxDisplayItems do
  223.             if self.sortedItems[i] ~= nil then
  224.                 table.insert(self.displayItems, self.sortedItems[i])
  225.             end
  226.         end
  227.         if self.selectedLabel ~= nil and self.selectedLabel.text == "" then
  228.             self.selectedLabel:setBackgroundColor(COLORS.grey)
  229.         end
  230.     end,
  231.     setPage = function(self, page)
  232.         if page == self.currentPage then
  233.             return
  234.         end
  235.         self.currentPage = page
  236.         self:setDisplayItems();
  237.     end,
  238.     reset = function (self)
  239.         self.items = {}
  240.         self.sortedItems = {}
  241.         self.displayItems = {}
  242.         self.selectedLabel = nil
  243.         self.currentPage = 1
  244.         self.maxPage = 1
  245.     end
  246. }
  247.  
  248. data.log = {
  249.     data = CircleQueue:new(44 * 7),
  250.     display = {},
  251.     currPos = 1,
  252.     maxPos = 1,
  253.     textArea = nil,
  254.     logScrollBar = nil,
  255.     add = function(self, ...)
  256.         -- concat all the arguments into a single string, make sure boolean, number and nil are converted to strings
  257.         local raw = table.concat({ ... }, "\t")
  258.         local lines = self.logTextArea:wrapText(raw)
  259.         for i = 1, #lines do
  260.             self.data:enqueue(lines[i])
  261.         end
  262.         self.maxPos = math.ceil(self.data:size() / self.logTextArea.height)
  263.         self:resetDisplay(self.currPos)
  264.         self.logTextArea:setText(self:getDisplayTextList(function(log) return log end))
  265.         self.logScrollBar:setMaxPos(self.maxPos)
  266.     end,
  267.     resetDisplay = function(self, pos)
  268.         local display = {}
  269.         local dataSize = self.data:size()
  270.         for i = 1, self.logTextArea.height do
  271.             local index = (pos - 1) * self.logTextArea.height + i
  272.             if index > dataSize then
  273.                 break
  274.             end
  275.             table.insert(display, self.data:get(index))
  276.         end
  277.         self.currPos = pos
  278.         self.display = display
  279.     end,
  280.     next = function(self)
  281.         if self.currPos < self.maxPos then
  282.             self:resetDisplay(self.currPos + 1)
  283.         end
  284.     end,
  285.     prev = function(self)
  286.         if self.currPos > 1 then
  287.             self:resetDisplay(self.currPos - 1)
  288.         end
  289.     end,
  290.     getDisplayTextList = function(self, extractor)
  291.         local list = {}
  292.         for i = 1, #self.display do
  293.             table.insert(list, extractor(self.display[i]))
  294.         end
  295.         return list
  296.     end
  297. }
  298.  
  299. local screen = Screen:new(160, 50, COLORS.black)
  300.  
  301. -- Inventory section
  302. local inventoryItemsSection = Section:new("item_list_section", 2, 1, 40, 48, "INVETORY", COLORS.lightGreen, COLORS.white,
  303.     COLORS.black)
  304. local iinventoryItemsListScrollBar = ScrollBar:new("item_list_scroll_bar", 36, 2, 44, COLORS.black, COLORS.lightGrey)
  305. local inventoryItemsList = List:new("item_list", 4, 2, 30, 44, 22, COLORS.white, COLORS.grey)
  306.  
  307. -- left right buttons
  308. local leftButton = Button:new("left_button", 44, 18, 5, 5, "<", COLORS.black, COLORS.lightGreen)
  309. local rightButton = Button:new("right_button", 44, 24, 5, 5, ">", COLORS.black, COLORS.pink)
  310.  
  311. -- Pull section
  312. local pullItemListSection = Section:new("pull_item_list_section", 51, 1, 40, 48, "PULL", COLORS.pink, COLORS.white,
  313.     COLORS.black)
  314. local pullItemListScrollBar = ScrollBar:new("pull_item_list_scroll_bar", 36, 2, 44, COLORS.black, COLORS.lightGrey)
  315. local pullList = List:new("pull_item_list", 4, 2, 30, 44, 22, COLORS.white, COLORS.grey)
  316.  
  317. -- log section
  318. local logSection = Section:new("log_section", 94, 1, 62, 48, "LOG", COLORS.lightGrey, COLORS.white, COLORS.black)
  319. local logScrollBar = ScrollBar:new("log_scroll_bar", 58, 2, 44, COLORS.black, COLORS.lightGrey)
  320. local logTextArea = TextArea:new("log_text_area", 4, 2, 54, 44, COLORS.white, COLORS.grey)
  321.  
  322. -- add components to screen
  323. screen:addComponent(inventoryItemsSection, 0)
  324. screen:addComponent(pullItemListSection, 0)
  325. screen:addComponent(leftButton, 0)
  326. leftButton:setOnTouch(function(btn, x, y)
  327.     local textList = {}
  328.     local pullItems = data.pullItems
  329.     if pullItems.selectedLabel == nil then
  330.         return
  331.     end
  332.     pullItems:remove()
  333.     for i = 1, #pullItems.displayItems do
  334.         table.insert(textList, pullItems.displayItems[i].label)
  335.     end
  336.     pullList:setTextList(textList)
  337. end)
  338. screen:addComponent(rightButton, 0)
  339. rightButton:setOnTouch(function(btn, x, y)
  340.     local textList = {}
  341.     local inventoryItems = data.inventoryItems
  342.     local pullItems = data.pullItems
  343.     if inventoryItems.selectedLabel == nil then
  344.         return
  345.     end
  346.     pullItems:add(inventoryItems.displayItems[inventoryItems.selectedLabel.idx])
  347.     for i = 1, #pullItems.displayItems do
  348.         table.insert(textList, pullItems.displayItems[i].label)
  349.     end
  350.     pullList:setTextList(textList)
  351.     data.inventoryItems:unselected();
  352. end)
  353. screen:addComponent(logSection, 0)
  354.  
  355. -- add components to itemlist section
  356. inventoryItemsSection:addComponent(inventoryItemsList, 0)
  357. inventoryItemsSection:addComponent(iinventoryItemsListScrollBar, 0)
  358. inventoryItemsList:setOnTouch(function(label, x, y)
  359.     if data.inventoryItems.selectedLabel ~= nil then
  360.         data.inventoryItems.selectedLabel:setBackgroundColor(COLORS.grey)
  361.     end
  362.     data.inventoryItems.selectedLabel = label
  363.     label:setBackgroundColor(COLORS.lightGreen)
  364. end)
  365.  
  366. -- add components to pull itemlist section
  367. pullItemListSection:addComponent(pullList, 0)
  368. pullItemListSection:addComponent(pullItemListScrollBar, 0)
  369. pullList:setOnTouch(function(label, x, y)
  370.     if data.pullItems.selectedLabel ~= nil then
  371.         data.pullItems.selectedLabel:setBackgroundColor(COLORS.grey)
  372.     end
  373.     data.pullItems.selectedLabel = label
  374.     label:setBackgroundColor(COLORS.pink)
  375. end)
  376.  
  377. -- add components to log section
  378. logSection:addComponent(logTextArea, 0)
  379. logSection:addComponent(logScrollBar, 0)
  380. data.log.logTextArea = logTextArea
  381. data.log.logScrollBar = logScrollBar
  382. logScrollBar:setOnTouch(function(self, x, y)
  383.     local status, err = pcall(function()
  384.         local adsParentX, absParentY = self.parent:getAbsXY()
  385.         if self:isScrollUpTouched(x, y, adsParentX, absParentY) then
  386.             data.log:prev()
  387.             logTextArea:setText(data.log:getDisplayTextList(function(log) return log end))
  388.             self:scrollUp()
  389.             logScrollBar:setMaxPos(data.log.maxPos)
  390.         elseif self:isScrollDownTouched(x, y, adsParentX, absParentY) then
  391.             data.log:next()
  392.             logTextArea:setText(data.log:getDisplayTextList(function(log) return log end))
  393.             self:scrollDown()
  394.             logScrollBar:setMaxPos(data.log.maxPos)
  395.         elseif self:isBarTouched(x, y, adsParentX, absParentY) then
  396.             local pos = self:yToBarPos(y, absParentY)
  397.             data.log:resetDisplay(pos)
  398.             logTextArea:setText(data.log:getDisplayTextList(function(log) return log end))
  399.             self:setCurrentPos(pos)
  400.             logScrollBar:setMaxPos(data.log.maxPos)
  401.         end
  402.     end)
  403.     if not status then
  404.         logger:print(err)
  405.     end
  406. end)
  407.  
  408. screen:init()
  409. screen:render()
  410. loggers.setPrintFunction(function(...)
  411.     data.log:add(...)
  412. end)
  413.  
  414. -- load data
  415. local save = readData("/save")
  416. if save ~= nil then
  417.     save = serialization.unserialize(save)
  418.     data.pullItems.items = save.items
  419.     data.pullItems.sortedItems = save.sortedItems
  420.     data.pullItems.displayItems = save.displayItems
  421.     local textList = {}
  422.     for i = 1, #data.pullItems.displayItems do
  423.         table.insert(textList, data.pullItems.displayItems[i].label)
  424.     end
  425.     pullList:setTextList(textList)
  426. end
  427.  
  428. event.timer(0.5, function()
  429.     local status, err = pcall(function()
  430.         data.inventoryItems:refresh()
  431.         local labelList = {}
  432.         for i = 1, #data.inventoryItems.displayItems do
  433.             table.insert(labelList, data.inventoryItems.displayItems[i].label)
  434.         end
  435.         inventoryItemsList:setTextList(labelList)
  436.     end)
  437.     if not status then
  438.         logger:print(err)
  439.         local save = {}
  440.         save.items = data.pullItems.items
  441.         save.sortedItems = data.pullItems.sortedItems
  442.         save.displayItems = data.pullItems.displayItems
  443.         saveData("/save", serialization.serialize(save))
  444.         logger:print("reboot in 10 seconds")
  445.         os.sleep(10)
  446.         os.execute("reboot")
  447.     end
  448. end, math.huge)
  449.  
  450. event.timer(0.1, function()
  451.     screen:render()
  452. end, math.huge)
  453.  
  454. while event.pull(1, "interrupted") == nil do
  455.  
  456. end
  457.  
Tags: lua Oc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement