Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- matrix
- local matrixObj = {matrix = {}, name = "", size = 0}
- function matrixObj:refresh()
- self.matrix = peripheral.wrap(self.name).list()
- end
- function matrixObj:getFirstIndex(id)
- if id == nil then
- for I = 1, (self.size - 1) do
- if self.matrix[I] == nil then
- return I
- end
- end
- return nil
- else
- for I = 1, self.size do
- if self.matrix[I] ~= nil then
- if self.matrix[I].name == id then
- return I
- end
- end
- end
- end
- return nil
- end
- function matrixObj:getEveryIndex(id)
- local returntable = {}
- if id == nil then
- for I = 1, self.size - 1 do
- if self.matrix[I] == nil then
- table.insert(returntable, I)
- end
- end
- else
- for I = 1, self.size do
- if self.matrix[I] ~= nil then
- if self.matrix[I].name == id then
- table.insert(returntable, I)
- end
- end
- end
- end
- if returntable[1] == nil then
- return nil
- else
- return returntable
- end
- end
- function matrixObj:getFirstFree(id)
- if id == nil then
- for I = 1, (self.size - 1) do
- if self.matrix[I] == nil then
- return I
- end
- end
- return nil
- else
- local maxCount = peripheral.wrap(self.name).getItemDetail(self:getFirstIndex(id)).maxCount
- if maxCount == nil then return false end
- for I = 1, (self.size - 1) do
- if self.matrix[I] ~= nil then
- if self.matrix[I].name == id and self.matrix[I].count ~= maxCount then
- return I
- end
- end
- end
- for I = 1, (self.size - 1) do
- if self.matrix[I] == nil then
- return I
- end
- end
- return nil
- end
- end
- function matrixObj:move(pullIndex, pushIndex, amount)
- local chest = peripheral.wrap(self.name)
- self:refresh()
- if pullIndex == nil then return false end
- if self.matrix[pullIndex] == nil then printError("pullIndex ("..pullIndex..") is nil") return false end
- if pullIndex > self.size then return false end
- if pushIndex == nil then
- pushIndex = self:getFirstFree(self.matrix[pullIndex].name)
- if pushIndex == nil then
- return false
- elseif pushIndex == pullIndex then
- pushIndex = self:getFirstFree()
- end
- end
- if pushIndex > self.size then return false end
- if self.matrix[pushIndex] ~= nil then
- local pushDetail = chest.getItemDetail(pushIndex)
- if self.matrix[pushIndex].count == pushDetail.maxCount or self.matrix[pushIndex].name ~= self.matrix[pullIndex].name then
- return 0
- end
- end
- if amount == nil then amount = self.matrix[pullIndex].count end
- if amount > self.matrix[pullIndex].count then amount = self.matrix[pullIndex].count end
- local pushamount = chest.pushItems(self.name, pullIndex, amount, pushIndex)
- self:refresh()
- if pushamount < amount then
- return pushamount
- else
- return true
- end
- end
- function matrixObj:swap(pullIndex, pushIndex)
- self:refresh()
- if pullIndex == nil then return false end
- if pullIndex > (self.size - 1) then return false end
- if pushIndex == nil then return false end
- if pushIndex > (self.size - 1) then return false end
- chest = peripheral.wrap(self.name)
- if self.matrix[self.size] ~= nil then printError("Last slot of the chest needs to be free") return false end
- if self.matrix[pullIndex] ~= nil and self.matrix[pushIndex] == nil then
- chest.pushItems(self.name, pullIndex, nil, pushIndex)
- elseif self.matrix[pullIndex] == nil and self.matrix[pushIndex] ~= nil then
- chest.pushItems(self.name, pushIndex, nil, pullIndex)
- elseif self.matrix[pullIndex] ~= nil and self.matrix[pushIndex] ~= nil then
- chest.pushItems(self.name, pullIndex, nil, self.size)
- chest.pushItems(self.name, pushIndex, nil, pullIndex)
- chest.pushItems(self.name, self.size, nil, pushIndex)
- end
- self:refresh()
- return true
- end
- function matrixObj:sort()
- self:refresh()
- local ids = {}
- for I = 1, self.size do
- if self.matrix[I] ~= nil then
- local alreadyIn = false
- for J = 1, #ids do
- if ids[J] == self.matrix[I].name then
- alreadyIn = true
- end
- end
- if alreadyIn == false then
- table.insert(ids, self.matrix[I].name)
- end
- end
- end
- local nextIndex = 1
- for I = 1, #ids do
- local entry = {name = ids[I], indexList = self:getEveryIndex(ids[I])}
- while #entry.indexList > 0 do
- local removecount = 0
- if self.matrix[nextIndex] == nil then
- self:swap(entry.indexList[1], nextIndex)
- else
- if self.matrix[nextIndex].name ~= entry.name then
- self:swap(entry.indexList[1], nextIndex)
- end
- end
- table.remove(entry.indexList, 1)
- for J = 1, #entry.indexList do
- local res = self:move(entry.indexList[J], nextIndex)
- if res == true then
- removecount = removecount + 1
- elseif res == false then
- return false
- else
- break
- end
- end
- for J = 1, removecount do
- table.remove(entry.indexList, 1)
- end
- nextIndex = nextIndex + 1
- end
- end
- end
- function matrixObj:new(o)
- o = o or {}
- setmetatable(o, self)
- self.__index = self
- return o
- end
- local function createMatrix(chestName)
- if chestName == nil then return false end
- local chest = peripheral.wrap(chestName)
- if chest == nil then return false end
- local returntable = matrixObj:new()
- returntable.name = chestName
- returntable.size = chest.size()
- returntable.matrix = chest.list()
- if returntable.matrix[returntable.size] ~= nil then
- printError("The last slot of a chest should always be empty")
- end
- return returntable
- end
- -- matrixTable
- local matrixTableObj = {list = {}}
- function matrixTableObj:add(cname)
- table.insert(self.list, createMatrix(cname))
- end
- function matrixTableObj:remove(cname)
- local index = 0
- for I = 1, #self.list do
- if self.list[I].name == cname then
- index = I
- break
- end
- end
- if index == 0 then
- return false
- else
- table.remove(self.list, index)
- return true
- end
- end
- function matrixTableObj:getIndex(cname)
- for I = 1, #self.list do
- if self.list[I].name == cname then
- return I
- end
- end
- return nil
- end
- function matrixTableObj:move(pullChestName, pullIndex, pushChestName, pushIndex, amount)
- --*error detection and setting constants*
- --chests
- if pullChestName == nil or pushChestName == nil or pullIndex == nil then return false end
- local pullc = peripheral.wrap(pullChestName)
- local pushc = peripheral.wrap(pushChestName)
- if pullc == nil or pushc == nil then return false end
- --constants
- local pullcIndex = self:getIndex(pullChestName)
- if pullcIndex == nil then printError("pullChestName not found in matrixTable: \""..pullChestName.."\"") return false end
- local pushcIndex = self:getIndex(pushChestName)
- if pushcIndex == nil then printError("pullChestName not foundin matrixTable: \""..pushChestName.."\"") return false end
- self.list[pullcIndex]:refresh()
- self.list[pushcIndex]:refresh()
- local pullDetail = pullc.getItemDetail(pullIndex)
- --Slots
- if pullDetail == nil then printError("pullIndex ("..pullIndex..") is nil") return false end
- if pushIndex ~= nil then
- if pushc.getItemDetail(pushIndex) ~= nil then
- if pushc.getItemDetail(pushIndex).name ~= pullDetail.name then
- printError("pushIndex ("..pushIndex..") is occupied")
- return false
- end
- if pushc.getItemDetail(pushIndex).count == pullDetail.maxCount then
- return 0
- end
- end
- else
- pushIndex = self.list[pushcIndex]:getFirstFree(pullDetail.name)
- if pushIndex == nil then return false end
- end
- if pushIndex > self.list[pushcIndex].size then return false end
- if amount == nil then amount = pullDetail.count end
- if amount > pullDetail.count then amount = pullDetail.count end
- --*function start*
- local pushamount = pullc.pushItems(pushChestName, pullIndex, amount, pushIndex)
- self.list[pullcIndex]:refresh()
- self.list[pushcIndex]:refresh()
- if pushamount ~= amount then
- return pushamount
- else
- return true
- end
- end
- function matrixTableObj:swap(pullChestName, pullIndex, pushChestName, pushIndex)
- --*error detection and setting constants*
- --chests
- if pullChestName == nil or pushChestName == nil or pullIndex == nil or pushIndex == nil then return false end
- local pullc = peripheral.wrap(pullChestName)
- local pushc = peripheral.wrap(pushChestName)
- if pullc == nil or pushc == nil then return false end
- local pullcIndex = self:getIndex(pullChestName)
- if pullcIndex == nil then printError("pullChestName not found in matrixTable: \""..pullChestName.."\"") return false end
- local pushcIndex = self:getIndex(pushChestName)
- if pushcIndex == nil then printError("pullChestName not foundin matrixTable: \""..pushChestName.."\"") return false end
- self.list[pullcIndex]:refresh()
- self.list[pushcIndex]:refresh()
- --Slots
- if pullIndex >= self.list[pullcIndex].size then return false end
- if pushIndex >= self.list[pushcIndex].size then return false end
- if self.list[pullcIndex].matrix[self.list[pullcIndex].size] ~= nil then printError("Last slot of the chest needs to be free ("..pullChestName..")") return false end
- if self.list[pushcIndex].matrix[self.list[pushcIndex].size] ~= nil then printError("Last slot of the chest needs to be free ("..pushChestName..")") return false end
- --*function start*
- if self.list[pullcIndex].matrix[pullIndex] ~= nil and self.list[pushcIndex].matrix[pushIndex] == nil then
- pullc.pushItems(pushChestName, pullIndex, nil, pushIndex)
- elseif self.list[pullcIndex].matrix[pullIndex] == nil and self.list[pushcIndex].matrix[pushIndex] ~= nil then
- pushc.pushItems(pullChestName, pushIndex, nil, pullIndex)
- elseif self.list[pullcIndex].matrix[pullIndex] ~= nil and self.list[pushcIndex].matrix[pushIndex] ~= nil then
- pullc.pushItems(pushChestName, pullIndex, nil, self.list[pushcIndex].size)
- pushc.pushItems(pullChestName, pushIndex, nil, pullIndex)
- pushc.pushItems(pushChestName, self.list[pushcIndex].size, nil, pushIndex)
- end
- self.list[pullcIndex]:refresh()
- self.list[pushcIndex]:refresh()
- return true
- end
- function matrixTableObj:new(o)
- o = o or {}
- setmetatable(o, self)
- self.__index = self
- return o
- end
- local function createMatrixTable(chestNames)
- if chestNames == nil then return false end
- if type(chestNames) == "table" then
- if chestNames[1] == nil then return false end
- end
- local returnobj = matrixTableObj:new()
- if type(chestNames) == "string" then
- returnobj.list[1] = createMatrix(chestNames)
- elseif type(chestNames) == "table" then
- for I = 1, #chestNames do
- table.insert(returnobj.list, createMatrix(chestNames[I]))
- end
- else
- return false
- end
- return returnobj
- end
- matrix = {create = createMatrix, createTable = createMatrixTable}
Add Comment
Please, Sign In to add comment