QuicksilverBoy

main.lua

Oct 17th, 2021 (edited)
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --Storage and Auto Craft System for ComputerCraft Mod
  2. local arg = {...}
  3.  
  4. local crafterBot = 19
  5. local receptBookPath = "recept.book"
  6. local glasses = "left"
  7.  
  8. if not fs.exists(receptBookPath) then
  9.     file = fs.open(receptBookPath, "w")
  10.     file.close()
  11. end
  12.  
  13. storage = {
  14.     main = "minecraft:chest_9",
  15.     furnaces = {
  16.        
  17.     },
  18.     getAllChestName = function(self)
  19.         local pList = peripheral.getNames()
  20.         for i, cn in ipairs(pList) do
  21.             local isChest = type(peripheral.wrap(cn).pushItems) ~= "function"
  22.             if isChest or cn == self.main then
  23.                 pList[i] = nil
  24.             end
  25.         end
  26.         --companing
  27.         CList = {}
  28.         local i = 1
  29.         for _, line in pairs(pList) do
  30.             table.insert(CList, line)
  31.         end
  32.         return CList
  33.     end,
  34.     clearMain = function(self)
  35.         local mainChest = peripheral.wrap(self.main)
  36.         local stor = self:getAllChestName()
  37.         local openMain = mainChest.list()
  38.         local n = 1
  39.         for i, slot in pairs(openMain) do
  40.             local _chest, _, _ = self:findItemByName(slot.name)
  41.             local num = 0
  42.             if _chest then
  43.                 local num = mainChest.pushItems(_chest, i, slot.count)
  44.             end
  45.             if num ~= slot.count or not _chest then
  46.                 for j, chest in pairs(stor) do
  47.                     num = num + mainChest.pushItems(chest, i, slot.count - num)
  48.                 end
  49.             end
  50.         end
  51.     end,
  52.     findItemByName = function(self, name)
  53.         local storList = self:getAllChestName()
  54.         for i, stor in ipairs(storList) do
  55.             local openChest = peripheral.wrap(stor).list()
  56.             for i, slot in pairs(openChest) do
  57.                 if slot.name == name then
  58.                     return stor, i, slot.count
  59.                 end
  60.             end
  61.         end
  62.         return false
  63.     end,
  64.     callItemByName = function(self, name, count)
  65.         local chest, slotNum, itemCount = self:findItemByName(name)
  66.         if not chest then
  67.             return false
  68.         end
  69.         local ch = peripheral.wrap(chest)
  70.         ch.pushItems(self.main, slotNum, count)
  71.         if itemCount < count then
  72.             self:callItemByName(name, count - itemCount)
  73.         end
  74.     end,
  75.     getMain = function(self)
  76.         local mainChest = peripheral.wrap(self.main)
  77.        
  78.         local list = {}
  79.         for i, item in pairs(mainChest.list()) do
  80.             if list[item.name] then
  81.                 list[item.name] = list[item.name] + item.count
  82.             else
  83.                 list[item.name] = item.count
  84.             end
  85.         end
  86.         return list
  87.     end,
  88.     getBalance = function(self)
  89.         local list = {}
  90.        
  91.         local chests = self:getAllChestName()
  92.         for i, chest in pairs(chests) do
  93.             local openStorege = peripheral.wrap(chest)
  94.             for j, item in pairs(openStorege.list()) do
  95.                 if list[item.name] then
  96.                     list[item.name] = list[item.name] + item.count
  97.                 else
  98.                     list[item.name] = item.count
  99.                 end
  100.             end
  101.         end
  102.         return list
  103.     end,
  104. }
  105.  
  106. function findSimilar(name)
  107.     local list = storage:getBalance()
  108.     local sim = {}
  109.     for n, c in pairs(list) do
  110.         if string.find(n, name) then
  111.             table.insert(sim, n)
  112.         end
  113.     end
  114.     return sim
  115. end
  116.  
  117. function write2File(table)
  118.     local lineOfTab = string.gsub(textutils.serialize(table), "\n", "")
  119.     local file = fs.open(receptBookPath, "a")
  120.     file.writeLine(lineOfTab)
  121.     file.close()
  122. end
  123.  
  124. function getAllRecepts()
  125.     local listOfRecepts = {}
  126.     local file = fs.open(receptBookPath, "r")
  127.     for line in file.readLine do
  128.         local lTab = textutils.unserialize(line)
  129.         table.insert(listOfRecepts, lTab)
  130.     end
  131.     return listOfRecepts
  132. end
  133.  
  134. function getSimRecepts(result)
  135.     local listOfRecepts = {}
  136.     local file = fs.open(receptBookPath, "r")
  137.     for line in file.readLine do
  138.         local lTab = textutils.unserialize(line)
  139.         if string.find(lTab.result, result) then
  140.             table.insert(listOfRecepts, lTab)
  141.         end
  142.     end
  143.     return listOfRecepts
  144. end
  145.  
  146. function craft(receptToCraft, count)
  147.     storage:clearMain()
  148.     local count = count or 1
  149.     --counting in recept
  150.     local materials = {}
  151.     for i, mat in pairs(receptToCraft.craft) do
  152.         if mat ~= "air" then
  153.             if materials[mat] then
  154.                 materials[mat] = materials[mat] + 1
  155.             else
  156.                 materials[mat] = 1
  157.             end
  158.         end
  159.     end
  160.     for itm, c in pairs(materials) do
  161.         materials[itm] = materials[itm] * count
  162.     end
  163.     --comparing and try to craft
  164.     --collecting
  165.     for item, count in pairs(materials) do
  166.         storage:callItemByName(item, count)
  167.     end
  168.     --crafting
  169.     rednet.send(crafterBot, {type="crafting", slots=receptToCraft.craft, count = count})
  170. end
  171.  
  172. function makePages(tab, size)
  173.     local pages = {}
  174.     i = 0
  175.     for name, count in pairs(tab) do
  176.         local numPage = math.floor(i/size) + 1
  177.         local numLine = (i%size) + 1
  178.         if not pages[numPage] then
  179.             pages[numPage] = {}
  180.         end
  181.         pages[numPage][numLine] = {name, count}
  182.         i = i + 1
  183.     end
  184.     return pages
  185. end
  186.  
  187. function tabulate(...)
  188.     local argTab = {...}
  189.     local colorLine = {[0] = 2^7, [1] = colors.black}
  190.     local cX, cY = term.getCursorPos()
  191.     local sizeW, sizeH = term.getSize()
  192.     term.setBackgroundColor(colors.cyan)
  193.     term.clearLine()
  194.     term.write("Item name")
  195.     term.setCursorPos(sizeW - 4, cY)
  196.     term.write("Count")
  197.     for line = 1, #argTab do
  198.         term.setCursorPos(cX, cY + line)
  199.         term.setBackgroundColor(colorLine[line%2])
  200.         term.clearLine()
  201.         term.write(argTab[line][1])
  202.         term.setCursorPos(sizeW - 3, cY + line)
  203.         term.write(argTab[line][2])
  204.     end
  205. end
  206.  
  207. rednet.open("top")
  208.  
  209. if arg[1] == "clear" then
  210.     storage:clearMain()
  211. elseif arg[1] == "call" then
  212.     if not arg[2] or not tonumber(arg[3]) then
  213.         print("call <name> <count>")
  214.     else
  215.         local finded = findSimilar(arg[2])
  216.         local l = storage:getBalance()
  217.         for i, n in pairs(finded) do
  218.             print(i, n, l[n])
  219.         end
  220.         term.write("Call num:")
  221.         local choose = tonumber(io.read())
  222.         --print(finded[choose], tonumber(arg[3]))
  223.         storage:callItemByName(finded[choose], tonumber(arg[3]))
  224.     end
  225. elseif arg[1] == "balance" then
  226.     local l = {}
  227.     if arg[2] == "" or not arg[2] then
  228.         l = storage:getBalance()
  229.     else
  230.         local simList = findSimilar(arg[2])
  231.         local balance = storage:getBalance()
  232.         for i, itName in pairs(simList) do
  233.             l[itName] = balance[itName]
  234.         end
  235.     end
  236.     local w, h = term.getSize()
  237.     local itemTableList = makePages(l, h - 3)
  238.     local page = 0
  239.     repeat
  240.         term.setCursorPos(1,1)
  241.         term.setBackgroundColor(colors.black)
  242.         term.clear()
  243.         term.setTextColor(colors.white)
  244.         term.write("Page " ..  page + 1 .. "/" .. #itemTableList)
  245.         term.setCursorPos(1, 2)
  246.         tabulate(table.unpack(itemTableList[page+1]))
  247.         local e, b = os.pullEvent("key")
  248.         if b == 65 then -- A pushed
  249.             page = (page - 1) % #itemTableList
  250.         elseif b == 68 then -- D pushed
  251.             page = (page + 1) % #itemTableList
  252.         end
  253.     until b == 69
  254.     term.clear()
  255.     term.setCursorPos(1, 1)
  256.     term.write("Returning to shell.")
  257.     term.setCursorPos(1, 2)
  258.     sleep(1)
  259. elseif arg[1] == "read" then
  260.     rednet.send(crafterBot, "read")
  261.     local id, rec = rednet.receive(10)
  262.     --return {craft = {"something"}, result="st"}
  263.     for i, item in pairs(rec.craft) do
  264.         print(i, item)
  265.     end
  266.     print("Result", rec.result)
  267.     local ans = io.read()
  268.     if ans == "y" or ans == "Y" then
  269.         write2File(rec)
  270.     end
  271. elseif arg[1] == "craft" then
  272.     arg[3] = tonumber(arg[3])
  273.     local receptsList = getSimRecepts(arg[2])
  274.     for i, rec in pairs(receptsList) do
  275.         print(i, rec.result)
  276.     end
  277.     io.write("Your chose: ")
  278.     local ch = tonumber(io.read())
  279.     craft(receptsList[ch], arg[3])
  280. elseif arg[1] == "displayBalanceGoogle" then
  281.     local balance = storage:getBalance()
  282.     local google = peripheral.wrap(glasses)
  283.     local i = 0
  284.  
  285.     google.clear()
  286.     for name, count in pairs(balance) do
  287.         google.drawItemIcon(name, math.floor(i/15)*48+20, (i%15)*20+4)
  288.         google.drawString(tostring(count), math.floor(i/15)*48+20+20, (i%15)*20+8, 0xFFFFFF)
  289.         i = i + 1
  290.     end
  291. else
  292.     print("No argument...")
  293.     for i, n in pairs(storage:getAllChestName()) do
  294.         print("Chest" .. i .. " ".. n)
  295.     end
  296. end
  297.  
Add Comment
Please, Sign In to add comment