Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local component = require("component")
- local event = require("event")
- local math = require("math")
- local sides = require("sides")
- local serialization = require("serialization")
- local CircleQueue = require("circulateQueue")
- local Screen = require("gui").Screen
- local Section = require("gui").Section
- local ScrollBar = require("gui").ScrollBar
- local COLORS = require("gui").COLORS
- local TextArea = require("gui").TextArea
- local List = require("gui").List
- local Label = require("gui").Label
- local Button = require("gui").Button
- local loggers = require("logger")
- local logger = loggers.getLogger("InventoryPull")
- local transposer = component.transposer
- -- local transposer = {
- -- getInventorySize = function(side)
- -- return 21
- -- end,
- -- getStackInSlot = function(side, slot)
- -- return { name = "minecraft:stone_" .. slot % 10, label = "Stone_" .. slot % 10, size = 64 }
- -- end,
- -- transferItem = function(sideA, sideB, size, slotA, slotB)
- -- return 1
- -- end,
- -- getAllStacks = function(side)
- -- return function()
- -- return nil
- -- end
- -- end
- -- }
- local inventorySide = sides.north
- local pullside = sides.top
- local fullOxygenTankChestSide = sides.south
- local oxygenTankInfo = transposer.getStackInSlot(fullOxygenTankChestSide, 1)
- local function saveData(path, data)
- local file = io.open(path, "w")
- if file then
- file:write(data)
- file:close()
- end
- end
- local function readData(path)
- local file = io.open(path, "r")
- if file then
- local str = file:read("*all")
- file:close()
- return str
- end
- end
- local moveItem = function(sideA, slotA, sideB, size)
- local invSize = transposer.getInventorySize(sideB);
- for slotB = 1, invSize do
- local transAmt = transposer.transferItem(sideA, sideB, size, slotA, slotB)
- if size - transAmt == 0 then
- return true
- end
- end
- return false
- end
- local findFullOxygenTankSlot = function()
- local slotInfo = nil
- local count = 0
- local chestItem = transposer.getAllStacks(fullOxygenTankChestSide)
- repeat
- count = count + 1
- slotInfo = chestItem()
- if slotInfo ~= nil and slotInfo.name == oxygenTankInfo.name and slotInfo.damage == 0 then
- return count
- end
- until slotInfo == nil
- end
- local data = {}
- data.pullItems = {
- items = {},
- sortedItems = {},
- displayItems = {},
- selectedLabel = nil,
- currentPage = 1,
- maxPage = 1,
- maxDisplayItems = 22,
- setMaxPage = function(self)
- self.maxPage = math.max(math.ceil(#self.sortedItems / self.maxDisplayItems), 1)
- end,
- add = function(self, item)
- self.sortedItems = {}
- if self.items[item.name] ~= nil then
- return
- end
- self.items[item.name] = item
- for _, value in pairs(self.items) do
- table.insert(self.sortedItems, value)
- end
- self:setMaxPage()
- table.sort(self.sortedItems, function(a, b) return a.label < b.label end)
- self:setDisplayItems()
- end,
- remove = function(self)
- self.sortedItems = {}
- if self.selectedLabel == nil then
- return
- end
- self.items[self.displayItems[self.selectedLabel.idx].name] = nil
- for _, value in pairs(self.items) do
- table.insert(self.sortedItems, value)
- end
- self:setMaxPage()
- table.sort(self.sortedItems, function(a, b) return a.label < b.label end)
- self:unselected()
- self:setDisplayItems()
- end,
- setDisplayItems = function(self)
- self.displayItems = {}
- if self.currentPage > self.maxPage then
- self.currentPage = self.maxPage
- end
- for i = (self.currentPage - 1) * self.maxDisplayItems + 1, self.currentPage * self.maxDisplayItems do
- if self.sortedItems[i] ~= nil then
- table.insert(self.displayItems, self.sortedItems[i])
- end
- end
- if self.selectedLabel ~= nil and self.selectedLabel ~= nil then
- self.selectedLabel:setBackgroundColor(COLORS.grey)
- self.selectedLabel = nil
- end
- end,
- setPage = function(self, page)
- if page == self.currentPage then
- return
- end
- self.currentPage = page
- self.displayItems = {}
- self:setDisplayItems();
- end,
- unselected = function(self)
- if self.selectedLabel ~= nil then
- self.selectedLabel:setBackgroundColor(COLORS.grey)
- self.selectedLabel = nil
- end
- end,
- }
- data.inventoryItems = {
- items = {},
- sortedItems = {},
- displayItems = {},
- selectedLabel = nil,
- currentPage = 1,
- maxPage = 1,
- maxDisplayItems = 22,
- removeItemForInventoryIfInPullList = function(self, item, slot)
- if item == nil then
- return nil
- end
- if data.pullItems.items[item.name] ~= nil then
- if moveItem(inventorySide, slot, pullside, 64) then
- logger:print("move item: " .. item.name .. " to pull side")
- return nil
- else
- return item
- end
- end
- return item
- end,
- replaceOxygenTank = function(self, item, slot)
- if item == nil then
- return nil
- end
- if item.name == oxygenTankInfo.name and item.damage ~= 0 then
- local chestSlot = findFullOxygenTankSlot()
- if chestSlot == nil then
- return nil
- end
- moveItem(inventorySide, slot, fullOxygenTankChestSide, 1)
- moveItem(fullOxygenTankChestSide, chestSlot, inventorySide, 1)
- logger:print("move oxygen tank from player inventory: " .. slot .. " to refill-Chest: " .. chestSlot)
- return item
- end
- return item
- end,
- refresh = function(self)
- local size, err = transposer.getInventorySize(inventorySide);
- self.items = {}
- self.sortedItems = {}
- for i = 1, size do
- local item = transposer.getStackInSlot(inventorySide, i)
- item = self:replaceOxygenTank(item, i)
- item = self:removeItemForInventoryIfInPullList(item, i)
- if item ~= nil and self.items[item.name] == nil then
- self.items[item.name] = item
- table.insert(self.sortedItems, item)
- end
- end
- table.sort(self.sortedItems, function(a, b) return a.label < b.label end)
- self.maxPage = math.ceil(#self.sortedItems / self.maxDisplayItems)
- self:setDisplayItems()
- end,
- unselected = function(self)
- if self.selectedLabel ~= nil then
- self.selectedLabel:setBackgroundColor(COLORS.grey)
- self.selectedLabel = nil
- end
- end,
- setDisplayItems = function(self)
- self.displayItems = {}
- if self.currentPage > self.maxPage then
- self.currentPage = self.maxPage
- end
- for i = (self.currentPage - 1) * self.maxDisplayItems + 1, self.currentPage * self.maxDisplayItems do
- if self.sortedItems[i] ~= nil then
- table.insert(self.displayItems, self.sortedItems[i])
- end
- end
- if self.selectedLabel ~= nil and self.selectedLabel.text == "" then
- self.selectedLabel:setBackgroundColor(COLORS.grey)
- end
- end,
- setPage = function(self, page)
- if page == self.currentPage then
- return
- end
- self.currentPage = page
- self:setDisplayItems();
- end,
- reset = function (self)
- self.items = {}
- self.sortedItems = {}
- self.displayItems = {}
- self.selectedLabel = nil
- self.currentPage = 1
- self.maxPage = 1
- end
- }
- data.log = {
- data = CircleQueue:new(44 * 7),
- display = {},
- currPos = 1,
- maxPos = 1,
- textArea = nil,
- logScrollBar = nil,
- add = function(self, ...)
- -- concat all the arguments into a single string, make sure boolean, number and nil are converted to strings
- local raw = table.concat({ ... }, "\t")
- local lines = self.logTextArea:wrapText(raw)
- for i = 1, #lines do
- self.data:enqueue(lines[i])
- end
- self.maxPos = math.ceil(self.data:size() / self.logTextArea.height)
- self:resetDisplay(self.currPos)
- self.logTextArea:setText(self:getDisplayTextList(function(log) return log end))
- self.logScrollBar:setMaxPos(self.maxPos)
- end,
- resetDisplay = function(self, pos)
- local display = {}
- local dataSize = self.data:size()
- for i = 1, self.logTextArea.height do
- local index = (pos - 1) * self.logTextArea.height + i
- if index > dataSize then
- break
- end
- table.insert(display, self.data:get(index))
- end
- self.currPos = pos
- self.display = display
- end,
- next = function(self)
- if self.currPos < self.maxPos then
- self:resetDisplay(self.currPos + 1)
- end
- end,
- prev = function(self)
- if self.currPos > 1 then
- self:resetDisplay(self.currPos - 1)
- end
- end,
- getDisplayTextList = function(self, extractor)
- local list = {}
- for i = 1, #self.display do
- table.insert(list, extractor(self.display[i]))
- end
- return list
- end
- }
- local screen = Screen:new(160, 50, COLORS.black)
- -- Inventory section
- local inventoryItemsSection = Section:new("item_list_section", 2, 1, 40, 48, "INVETORY", COLORS.lightGreen, COLORS.white,
- COLORS.black)
- local iinventoryItemsListScrollBar = ScrollBar:new("item_list_scroll_bar", 36, 2, 44, COLORS.black, COLORS.lightGrey)
- local inventoryItemsList = List:new("item_list", 4, 2, 30, 44, 22, COLORS.white, COLORS.grey)
- -- left right buttons
- local leftButton = Button:new("left_button", 44, 18, 5, 5, "<", COLORS.black, COLORS.lightGreen)
- local rightButton = Button:new("right_button", 44, 24, 5, 5, ">", COLORS.black, COLORS.pink)
- -- Pull section
- local pullItemListSection = Section:new("pull_item_list_section", 51, 1, 40, 48, "PULL", COLORS.pink, COLORS.white,
- COLORS.black)
- local pullItemListScrollBar = ScrollBar:new("pull_item_list_scroll_bar", 36, 2, 44, COLORS.black, COLORS.lightGrey)
- local pullList = List:new("pull_item_list", 4, 2, 30, 44, 22, COLORS.white, COLORS.grey)
- -- log section
- local logSection = Section:new("log_section", 94, 1, 62, 48, "LOG", COLORS.lightGrey, COLORS.white, COLORS.black)
- local logScrollBar = ScrollBar:new("log_scroll_bar", 58, 2, 44, COLORS.black, COLORS.lightGrey)
- local logTextArea = TextArea:new("log_text_area", 4, 2, 54, 44, COLORS.white, COLORS.grey)
- -- add components to screen
- screen:addComponent(inventoryItemsSection, 0)
- screen:addComponent(pullItemListSection, 0)
- screen:addComponent(leftButton, 0)
- leftButton:setOnTouch(function(btn, x, y)
- local textList = {}
- local pullItems = data.pullItems
- if pullItems.selectedLabel == nil then
- return
- end
- pullItems:remove()
- for i = 1, #pullItems.displayItems do
- table.insert(textList, pullItems.displayItems[i].label)
- end
- pullList:setTextList(textList)
- end)
- screen:addComponent(rightButton, 0)
- rightButton:setOnTouch(function(btn, x, y)
- local textList = {}
- local inventoryItems = data.inventoryItems
- local pullItems = data.pullItems
- if inventoryItems.selectedLabel == nil then
- return
- end
- pullItems:add(inventoryItems.displayItems[inventoryItems.selectedLabel.idx])
- for i = 1, #pullItems.displayItems do
- table.insert(textList, pullItems.displayItems[i].label)
- end
- pullList:setTextList(textList)
- data.inventoryItems:unselected();
- end)
- screen:addComponent(logSection, 0)
- -- add components to itemlist section
- inventoryItemsSection:addComponent(inventoryItemsList, 0)
- inventoryItemsSection:addComponent(iinventoryItemsListScrollBar, 0)
- inventoryItemsList:setOnTouch(function(label, x, y)
- if data.inventoryItems.selectedLabel ~= nil then
- data.inventoryItems.selectedLabel:setBackgroundColor(COLORS.grey)
- end
- data.inventoryItems.selectedLabel = label
- label:setBackgroundColor(COLORS.lightGreen)
- end)
- -- add components to pull itemlist section
- pullItemListSection:addComponent(pullList, 0)
- pullItemListSection:addComponent(pullItemListScrollBar, 0)
- pullList:setOnTouch(function(label, x, y)
- if data.pullItems.selectedLabel ~= nil then
- data.pullItems.selectedLabel:setBackgroundColor(COLORS.grey)
- end
- data.pullItems.selectedLabel = label
- label:setBackgroundColor(COLORS.pink)
- end)
- -- add components to log section
- logSection:addComponent(logTextArea, 0)
- logSection:addComponent(logScrollBar, 0)
- data.log.logTextArea = logTextArea
- data.log.logScrollBar = logScrollBar
- logScrollBar:setOnTouch(function(self, x, y)
- local status, err = pcall(function()
- local adsParentX, absParentY = self.parent:getAbsXY()
- if self:isScrollUpTouched(x, y, adsParentX, absParentY) then
- data.log:prev()
- logTextArea:setText(data.log:getDisplayTextList(function(log) return log end))
- self:scrollUp()
- logScrollBar:setMaxPos(data.log.maxPos)
- elseif self:isScrollDownTouched(x, y, adsParentX, absParentY) then
- data.log:next()
- logTextArea:setText(data.log:getDisplayTextList(function(log) return log end))
- self:scrollDown()
- logScrollBar:setMaxPos(data.log.maxPos)
- elseif self:isBarTouched(x, y, adsParentX, absParentY) then
- local pos = self:yToBarPos(y, absParentY)
- data.log:resetDisplay(pos)
- logTextArea:setText(data.log:getDisplayTextList(function(log) return log end))
- self:setCurrentPos(pos)
- logScrollBar:setMaxPos(data.log.maxPos)
- end
- end)
- if not status then
- logger:print(err)
- end
- end)
- screen:init()
- screen:render()
- loggers.setPrintFunction(function(...)
- data.log:add(...)
- end)
- -- load data
- local save = readData("/save")
- if save ~= nil then
- save = serialization.unserialize(save)
- data.pullItems.items = save.items
- data.pullItems.sortedItems = save.sortedItems
- data.pullItems.displayItems = save.displayItems
- local textList = {}
- for i = 1, #data.pullItems.displayItems do
- table.insert(textList, data.pullItems.displayItems[i].label)
- end
- pullList:setTextList(textList)
- end
- event.timer(0.5, function()
- local status, err = pcall(function()
- data.inventoryItems:refresh()
- local labelList = {}
- for i = 1, #data.inventoryItems.displayItems do
- table.insert(labelList, data.inventoryItems.displayItems[i].label)
- end
- inventoryItemsList:setTextList(labelList)
- end)
- if not status then
- logger:print(err)
- local save = {}
- save.items = data.pullItems.items
- save.sortedItems = data.pullItems.sortedItems
- save.displayItems = data.pullItems.displayItems
- saveData("/save", serialization.serialize(save))
- logger:print("reboot in 10 seconds")
- os.sleep(10)
- os.execute("reboot")
- end
- end, math.huge)
- event.timer(0.1, function()
- screen:render()
- end, math.huge)
- while event.pull(1, "interrupted") == nil do
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement