Neon1432

inventory

Jan 20th, 2022 (edited)
374
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.49 KB | None | 0 0
  1. -- matrix
  2.  
  3.  
  4. local matrixObj = {matrix = {}, name = "", size = 0}
  5.  
  6. function matrixObj:refresh()
  7.     self.matrix = peripheral.wrap(self.name).list()
  8. end
  9.  
  10. function matrixObj:getFirstIndex(id)
  11.     if id == nil then
  12.         for I = 1, (self.size - 1) do
  13.             if self.matrix[I] == nil then
  14.                 return I
  15.             end
  16.         end
  17.         return nil
  18.     else
  19.         for I = 1, self.size do
  20.             if self.matrix[I] ~= nil then
  21.                 if self.matrix[I].name == id then
  22.                     return I
  23.                 end
  24.             end
  25.         end
  26.     end
  27.     return nil
  28. end
  29.  
  30. function matrixObj:getEveryIndex(id)
  31.     local returntable = {}
  32.     if id == nil then
  33.         for I = 1, self.size - 1 do
  34.             if self.matrix[I] == nil then
  35.                 table.insert(returntable, I)
  36.             end
  37.         end
  38.     else
  39.         for I = 1, self.size do
  40.             if self.matrix[I] ~= nil then
  41.                 if self.matrix[I].name == id then
  42.                     table.insert(returntable, I)
  43.                 end
  44.             end
  45.         end
  46.     end
  47.     if returntable[1] == nil then
  48.         return nil
  49.     else
  50.         return returntable
  51.     end
  52. end
  53.  
  54. function matrixObj:getFirstFree(id)
  55.     if id == nil then
  56.         for I = 1, (self.size - 1) do
  57.             if self.matrix[I] == nil then
  58.                 return I
  59.             end
  60.         end
  61.         return nil
  62.     else
  63.         local maxCount = peripheral.wrap(self.name).getItemDetail(self:getFirstIndex(id)).maxCount
  64.         if maxCount == nil then return false end
  65.  
  66.         for I = 1, (self.size - 1) do
  67.             if self.matrix[I] ~= nil then
  68.                 if self.matrix[I].name == id and self.matrix[I].count ~= maxCount then
  69.                     return I
  70.                 end
  71.             end
  72.         end
  73.         for I = 1, (self.size - 1) do
  74.             if self.matrix[I] == nil then
  75.                 return I
  76.             end
  77.         end
  78.         return nil
  79.     end
  80. end
  81.  
  82. function matrixObj:move(pullIndex, pushIndex, amount)
  83.     local chest = peripheral.wrap(self.name)
  84.     self:refresh()
  85.  
  86.     if pullIndex == nil then return false end
  87.     if self.matrix[pullIndex] == nil then printError("pullIndex ("..pullIndex..") is nil") return false end
  88.     if pullIndex > self.size then return false end
  89.  
  90.     if pushIndex == nil then
  91.         pushIndex = self:getFirstFree(self.matrix[pullIndex].name)
  92.         if pushIndex == nil then
  93.             return false
  94.         elseif pushIndex == pullIndex then
  95.             pushIndex = self:getFirstFree()
  96.         end
  97.     end
  98.     if pushIndex > self.size then return false end
  99.  
  100.     if self.matrix[pushIndex] ~= nil then
  101.         local pushDetail = chest.getItemDetail(pushIndex)
  102.         if self.matrix[pushIndex].count == pushDetail.maxCount or self.matrix[pushIndex].name ~= self.matrix[pullIndex].name then
  103.             return 0
  104.         end
  105.     end
  106.  
  107.     if amount == nil then amount = self.matrix[pullIndex].count end
  108.     if amount > self.matrix[pullIndex].count then amount = self.matrix[pullIndex].count end
  109.  
  110.     local pushamount = chest.pushItems(self.name, pullIndex, amount, pushIndex)
  111.    
  112.     self:refresh()
  113.    
  114.     if pushamount < amount then
  115.         return pushamount
  116.     else
  117.         return true
  118.     end
  119. end
  120.  
  121. function matrixObj:swap(pullIndex, pushIndex)
  122.     self:refresh()
  123.  
  124.     if pullIndex == nil then return false end
  125.     if pullIndex > (self.size - 1) then return false end
  126.     if pushIndex == nil then return false end
  127.     if pushIndex > (self.size - 1) then return false end
  128.    
  129.     chest = peripheral.wrap(self.name)
  130.    
  131.     if self.matrix[self.size] ~= nil then printError("Last slot of the chest needs to be free") return false end
  132.  
  133.     if self.matrix[pullIndex] ~= nil and self.matrix[pushIndex] == nil then
  134.         chest.pushItems(self.name, pullIndex, nil, pushIndex)
  135.     elseif self.matrix[pullIndex] == nil and self.matrix[pushIndex] ~= nil then
  136.         chest.pushItems(self.name, pushIndex, nil, pullIndex)  
  137.     elseif self.matrix[pullIndex] ~= nil and self.matrix[pushIndex] ~= nil then
  138.         chest.pushItems(self.name, pullIndex, nil, self.size)
  139.         chest.pushItems(self.name, pushIndex, nil, pullIndex)
  140.         chest.pushItems(self.name, self.size, nil, pushIndex)
  141.     end
  142.  
  143.     self:refresh()
  144.    
  145.     return true
  146. end
  147.  
  148. function matrixObj:sort()
  149.     self:refresh()
  150.     local ids = {}
  151.     for I = 1, self.size do
  152.         if self.matrix[I] ~= nil then
  153.             local alreadyIn = false
  154.             for J = 1, #ids do
  155.                 if ids[J] == self.matrix[I].name then
  156.                     alreadyIn = true
  157.                 end
  158.             end
  159.             if alreadyIn == false then
  160.                 table.insert(ids, self.matrix[I].name)
  161.             end
  162.         end
  163.     end
  164.    
  165.     local nextIndex = 1
  166.     for I = 1, #ids do
  167.         local entry = {name = ids[I], indexList = self:getEveryIndex(ids[I])}
  168.  
  169.         while #entry.indexList > 0 do
  170.             local removecount = 0
  171.             if self.matrix[nextIndex] == nil then
  172.                 self:swap(entry.indexList[1], nextIndex)
  173.             else
  174.                 if self.matrix[nextIndex].name ~= entry.name then
  175.                     self:swap(entry.indexList[1], nextIndex)   
  176.                 end
  177.             end
  178.             table.remove(entry.indexList, 1)
  179.             for J = 1, #entry.indexList do
  180.                 local res = self:move(entry.indexList[J], nextIndex)
  181.                 if res == true then
  182.                     removecount = removecount + 1
  183.                 elseif res == false then
  184.                     return false
  185.                 else
  186.                     break
  187.                 end
  188.             end
  189.             for J = 1, removecount do
  190.                 table.remove(entry.indexList, 1)
  191.             end
  192.             nextIndex = nextIndex + 1
  193.         end
  194.     end
  195. end
  196.  
  197. function matrixObj:new(o)
  198.     o = o or {}
  199.     setmetatable(o, self)
  200.     self.__index = self
  201.     return o
  202. end
  203.  
  204.  
  205.  
  206. local function createMatrix(chestName)
  207.     if chestName == nil then return false end
  208.     local chest = peripheral.wrap(chestName)
  209.     if chest == nil then return false end
  210.  
  211.     local returntable = matrixObj:new()
  212.  
  213.     returntable.name = chestName
  214.     returntable.size = chest.size()
  215.     returntable.matrix = chest.list()
  216.  
  217.     if returntable.matrix[returntable.size] ~= nil then
  218.         printError("The last slot of a chest should always be empty")
  219.     end
  220.  
  221.     return returntable
  222. end
  223.  
  224.  
  225.  
  226. -- matrixTable
  227.  
  228.  
  229.  
  230. local matrixTableObj = {list = {}}
  231.  
  232. function matrixTableObj:add(cname)
  233.     table.insert(self.list, createMatrix(cname))
  234. end
  235.  
  236. function matrixTableObj:remove(cname)
  237.     local index = 0
  238.     for I = 1, #self.list do
  239.         if self.list[I].name == cname then
  240.             index = I
  241.             break
  242.         end
  243.     end
  244.     if index == 0 then
  245.         return false
  246.     else
  247.         table.remove(self.list, index)
  248.         return true
  249.     end
  250. end
  251.  
  252. function matrixTableObj:getIndex(cname)
  253.     for I = 1, #self.list do
  254.         if self.list[I].name == cname then
  255.             return I
  256.         end
  257.     end
  258.     return nil
  259. end
  260.  
  261. function matrixTableObj:move(pullChestName, pullIndex, pushChestName, pushIndex, amount)
  262.  
  263.     --*error detection and setting constants*
  264.  
  265.     --chests
  266.     if pullChestName == nil or pushChestName == nil or pullIndex == nil then return false end
  267.     local pullc = peripheral.wrap(pullChestName)
  268.     local pushc = peripheral.wrap(pushChestName)
  269.     if pullc == nil or pushc == nil then return false end
  270.  
  271.     --constants
  272.     local pullcIndex = self:getIndex(pullChestName)
  273.     if pullcIndex == nil then printError("pullChestName not found in matrixTable: \""..pullChestName.."\"") return false end
  274.     local pushcIndex = self:getIndex(pushChestName)
  275.     if pushcIndex == nil then printError("pullChestName not foundin matrixTable: \""..pushChestName.."\"") return false end
  276.  
  277.     self.list[pullcIndex]:refresh()
  278.     self.list[pushcIndex]:refresh()
  279.  
  280.     local pullDetail = pullc.getItemDetail(pullIndex)
  281.  
  282.     --Slots
  283.     if pullDetail == nil then printError("pullIndex ("..pullIndex..") is nil") return false end
  284.  
  285.     if pushIndex ~= nil then
  286.         if pushc.getItemDetail(pushIndex) ~= nil then
  287.             if pushc.getItemDetail(pushIndex).name ~= pullDetail.name then
  288.                 printError("pushIndex ("..pushIndex..") is occupied")
  289.                 return false
  290.             end
  291.             if pushc.getItemDetail(pushIndex).count == pullDetail.maxCount then
  292.                 return 0
  293.             end
  294.         end
  295.     else
  296.         pushIndex = self.list[pushcIndex]:getFirstFree(pullDetail.name)
  297.         if pushIndex == nil then return false end
  298.     end
  299.     if pushIndex > self.list[pushcIndex].size then return false end
  300.  
  301.     if amount == nil then amount = pullDetail.count end
  302.     if amount > pullDetail.count then amount = pullDetail.count end
  303.  
  304.     --*function start*
  305.  
  306.     local pushamount = pullc.pushItems(pushChestName, pullIndex, amount, pushIndex)
  307.    
  308.     self.list[pullcIndex]:refresh()
  309.     self.list[pushcIndex]:refresh()
  310.  
  311.     if pushamount ~= amount then
  312.         return pushamount
  313.     else
  314.         return true
  315.     end
  316. end
  317.  
  318. function matrixTableObj:swap(pullChestName, pullIndex, pushChestName, pushIndex)
  319.    
  320.     --*error detection and setting constants*
  321.  
  322.     --chests
  323.     if pullChestName == nil or pushChestName == nil or pullIndex == nil or pushIndex == nil then return false end
  324.     local pullc = peripheral.wrap(pullChestName)
  325.     local pushc = peripheral.wrap(pushChestName)
  326.     if pullc == nil or pushc == nil then return false end
  327.  
  328.     local pullcIndex = self:getIndex(pullChestName)
  329.     if pullcIndex == nil then printError("pullChestName not found in matrixTable: \""..pullChestName.."\"") return false end
  330.     local pushcIndex = self:getIndex(pushChestName)
  331.     if pushcIndex == nil then printError("pullChestName not foundin matrixTable: \""..pushChestName.."\"") return false end
  332.  
  333.     self.list[pullcIndex]:refresh()
  334.     self.list[pushcIndex]:refresh()
  335.  
  336.     --Slots
  337.     if pullIndex >= self.list[pullcIndex].size then return false end
  338.     if pushIndex >= self.list[pushcIndex].size then return false end
  339.  
  340.     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
  341.     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
  342.  
  343.     --*function start*
  344.  
  345.     if self.list[pullcIndex].matrix[pullIndex] ~= nil and self.list[pushcIndex].matrix[pushIndex] == nil then
  346.         pullc.pushItems(pushChestName, pullIndex, nil, pushIndex)
  347.     elseif self.list[pullcIndex].matrix[pullIndex] == nil and self.list[pushcIndex].matrix[pushIndex] ~= nil then
  348.         pushc.pushItems(pullChestName, pushIndex, nil, pullIndex)  
  349.     elseif self.list[pullcIndex].matrix[pullIndex] ~= nil and self.list[pushcIndex].matrix[pushIndex] ~= nil then
  350.         pullc.pushItems(pushChestName, pullIndex, nil, self.list[pushcIndex].size)
  351.         pushc.pushItems(pullChestName, pushIndex, nil, pullIndex)
  352.         pushc.pushItems(pushChestName, self.list[pushcIndex].size, nil, pushIndex)
  353.     end
  354.  
  355.     self.list[pullcIndex]:refresh()
  356.     self.list[pushcIndex]:refresh()
  357.  
  358.     return true
  359. end
  360.  
  361. function matrixTableObj:new(o)
  362.     o = o or {}
  363.     setmetatable(o, self)
  364.     self.__index = self
  365.     return o
  366. end
  367.  
  368.  
  369. local function createMatrixTable(chestNames)
  370.     if chestNames == nil then return false end
  371.     if type(chestNames) == "table" then
  372.         if chestNames[1] == nil then return false end
  373.     end
  374.  
  375.     local returnobj = matrixTableObj:new()
  376.     if type(chestNames) == "string" then
  377.         returnobj.list[1] = createMatrix(chestNames)
  378.     elseif type(chestNames) == "table" then
  379.         for I = 1, #chestNames do
  380.             table.insert(returnobj.list, createMatrix(chestNames[I]))
  381.         end
  382.     else
  383.         return false
  384.     end
  385.  
  386.     return returnobj
  387. end
  388.  
  389. matrix = {create = createMatrix, createTable = createMatrixTable}
Add Comment
Please, Sign In to add comment