Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Storage and Auto Craft System for ComputerCraft Mod
- local arg = {...}
- local crafterBot = 19
- local receptBookPath = "recept.book"
- local glasses = "left"
- if not fs.exists(receptBookPath) then
- file = fs.open(receptBookPath, "w")
- file.close()
- end
- storage = {
- main = "minecraft:chest_9",
- furnaces = {
- },
- getAllChestName = function(self)
- local pList = peripheral.getNames()
- for i, cn in ipairs(pList) do
- local isChest = type(peripheral.wrap(cn).pushItems) ~= "function"
- if isChest or cn == self.main then
- pList[i] = nil
- end
- end
- --companing
- CList = {}
- local i = 1
- for _, line in pairs(pList) do
- table.insert(CList, line)
- end
- return CList
- end,
- clearMain = function(self)
- local mainChest = peripheral.wrap(self.main)
- local stor = self:getAllChestName()
- local openMain = mainChest.list()
- local n = 1
- for i, slot in pairs(openMain) do
- local _chest, _, _ = self:findItemByName(slot.name)
- local num = 0
- if _chest then
- local num = mainChest.pushItems(_chest, i, slot.count)
- end
- if num ~= slot.count or not _chest then
- for j, chest in pairs(stor) do
- num = num + mainChest.pushItems(chest, i, slot.count - num)
- end
- end
- end
- end,
- findItemByName = function(self, name)
- local storList = self:getAllChestName()
- for i, stor in ipairs(storList) do
- local openChest = peripheral.wrap(stor).list()
- for i, slot in pairs(openChest) do
- if slot.name == name then
- return stor, i, slot.count
- end
- end
- end
- return false
- end,
- callItemByName = function(self, name, count)
- local chest, slotNum, itemCount = self:findItemByName(name)
- if not chest then
- return false
- end
- local ch = peripheral.wrap(chest)
- ch.pushItems(self.main, slotNum, count)
- if itemCount < count then
- self:callItemByName(name, count - itemCount)
- end
- end,
- getMain = function(self)
- local mainChest = peripheral.wrap(self.main)
- local list = {}
- for i, item in pairs(mainChest.list()) do
- if list[item.name] then
- list[item.name] = list[item.name] + item.count
- else
- list[item.name] = item.count
- end
- end
- return list
- end,
- getBalance = function(self)
- local list = {}
- local chests = self:getAllChestName()
- for i, chest in pairs(chests) do
- local openStorege = peripheral.wrap(chest)
- for j, item in pairs(openStorege.list()) do
- if list[item.name] then
- list[item.name] = list[item.name] + item.count
- else
- list[item.name] = item.count
- end
- end
- end
- return list
- end,
- }
- function findSimilar(name)
- local list = storage:getBalance()
- local sim = {}
- for n, c in pairs(list) do
- if string.find(n, name) then
- table.insert(sim, n)
- end
- end
- return sim
- end
- function write2File(table)
- local lineOfTab = string.gsub(textutils.serialize(table), "\n", "")
- local file = fs.open(receptBookPath, "a")
- file.writeLine(lineOfTab)
- file.close()
- end
- function getAllRecepts()
- local listOfRecepts = {}
- local file = fs.open(receptBookPath, "r")
- for line in file.readLine do
- local lTab = textutils.unserialize(line)
- table.insert(listOfRecepts, lTab)
- end
- return listOfRecepts
- end
- function getSimRecepts(result)
- local listOfRecepts = {}
- local file = fs.open(receptBookPath, "r")
- for line in file.readLine do
- local lTab = textutils.unserialize(line)
- if string.find(lTab.result, result) then
- table.insert(listOfRecepts, lTab)
- end
- end
- return listOfRecepts
- end
- function craft(receptToCraft, count)
- storage:clearMain()
- local count = count or 1
- --counting in recept
- local materials = {}
- for i, mat in pairs(receptToCraft.craft) do
- if mat ~= "air" then
- if materials[mat] then
- materials[mat] = materials[mat] + 1
- else
- materials[mat] = 1
- end
- end
- end
- for itm, c in pairs(materials) do
- materials[itm] = materials[itm] * count
- end
- --comparing and try to craft
- --collecting
- for item, count in pairs(materials) do
- storage:callItemByName(item, count)
- end
- --crafting
- rednet.send(crafterBot, {type="crafting", slots=receptToCraft.craft, count = count})
- end
- function makePages(tab, size)
- local pages = {}
- i = 0
- for name, count in pairs(tab) do
- local numPage = math.floor(i/size) + 1
- local numLine = (i%size) + 1
- if not pages[numPage] then
- pages[numPage] = {}
- end
- pages[numPage][numLine] = {name, count}
- i = i + 1
- end
- return pages
- end
- function tabulate(...)
- local argTab = {...}
- local colorLine = {[0] = 2^7, [1] = colors.black}
- local cX, cY = term.getCursorPos()
- local sizeW, sizeH = term.getSize()
- term.setBackgroundColor(colors.cyan)
- term.clearLine()
- term.write("Item name")
- term.setCursorPos(sizeW - 4, cY)
- term.write("Count")
- for line = 1, #argTab do
- term.setCursorPos(cX, cY + line)
- term.setBackgroundColor(colorLine[line%2])
- term.clearLine()
- term.write(argTab[line][1])
- term.setCursorPos(sizeW - 3, cY + line)
- term.write(argTab[line][2])
- end
- end
- rednet.open("top")
- if arg[1] == "clear" then
- storage:clearMain()
- elseif arg[1] == "call" then
- if not arg[2] or not tonumber(arg[3]) then
- print("call <name> <count>")
- else
- local finded = findSimilar(arg[2])
- local l = storage:getBalance()
- for i, n in pairs(finded) do
- print(i, n, l[n])
- end
- term.write("Call num:")
- local choose = tonumber(io.read())
- --print(finded[choose], tonumber(arg[3]))
- storage:callItemByName(finded[choose], tonumber(arg[3]))
- end
- elseif arg[1] == "balance" then
- local l = {}
- if arg[2] == "" or not arg[2] then
- l = storage:getBalance()
- else
- local simList = findSimilar(arg[2])
- local balance = storage:getBalance()
- for i, itName in pairs(simList) do
- l[itName] = balance[itName]
- end
- end
- local w, h = term.getSize()
- local itemTableList = makePages(l, h - 3)
- local page = 0
- repeat
- term.setCursorPos(1,1)
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setTextColor(colors.white)
- term.write("Page " .. page + 1 .. "/" .. #itemTableList)
- term.setCursorPos(1, 2)
- tabulate(table.unpack(itemTableList[page+1]))
- local e, b = os.pullEvent("key")
- if b == 65 then -- A pushed
- page = (page - 1) % #itemTableList
- elseif b == 68 then -- D pushed
- page = (page + 1) % #itemTableList
- end
- until b == 69
- term.clear()
- term.setCursorPos(1, 1)
- term.write("Returning to shell.")
- term.setCursorPos(1, 2)
- sleep(1)
- elseif arg[1] == "read" then
- rednet.send(crafterBot, "read")
- local id, rec = rednet.receive(10)
- --return {craft = {"something"}, result="st"}
- for i, item in pairs(rec.craft) do
- print(i, item)
- end
- print("Result", rec.result)
- local ans = io.read()
- if ans == "y" or ans == "Y" then
- write2File(rec)
- end
- elseif arg[1] == "craft" then
- arg[3] = tonumber(arg[3])
- local receptsList = getSimRecepts(arg[2])
- for i, rec in pairs(receptsList) do
- print(i, rec.result)
- end
- io.write("Your chose: ")
- local ch = tonumber(io.read())
- craft(receptsList[ch], arg[3])
- elseif arg[1] == "displayBalanceGoogle" then
- local balance = storage:getBalance()
- local google = peripheral.wrap(glasses)
- local i = 0
- google.clear()
- for name, count in pairs(balance) do
- google.drawItemIcon(name, math.floor(i/15)*48+20, (i%15)*20+4)
- google.drawString(tostring(count), math.floor(i/15)*48+20+20, (i%15)*20+8, 0xFFFFFF)
- i = i + 1
- end
- else
- print("No argument...")
- for i, n in pairs(storage:getAllChestName()) do
- print("Chest" .. i .. " ".. n)
- end
- end
Add Comment
Please, Sign In to add comment