Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Script by Savage_Me55iah of CraftAU.com.au and Nuqube.co--
- local scriptName, version, pastebinCode = "StockKeeper", "v1.0", "iWHbXKyf"
- --Other Savage_Me55iah scripts @ http://pastebin.com/u/Savage_Me55iah
- --INSTRUCTIONS ON USE--
- --Instruction video on http://youtube.com/savagemessiah5
- --Contact Savage_Me55iah @ CraftAU.com.au or Nuqube.co or http://www.computercraft.info/forums2/ with suggestions/fixes/bugs
- -- CHANGELOG --
- --V1.0
- -- SETUP --
- local tArgs = {...}
- do
- update = function(script_name, pastebincode)
- print("UPDATING") shell.run("pastebin get "..pastebincode.." update")
- if fs.exists("update") then
- fs.delete(script_name) fs.copy("update", script_name) fs.delete("update") print("UPDATE SUCCESS")
- else
- print("UPDATE FAILED")
- end
- sleep(2) os.reboot()
- end
- end
- if tArgs[1] == "update" then update(scriptName, pastebinCode) end
- timer = 0.1
- facing = "north"
- input = {"bottom", "top", "front", "back"}
- side = {"front", "right", "back", "left"}
- sideReverse = {["bottom"] = "up", ["top"] = "down", ["left"] = "right", ["right"] = "left", ["front"] = "back", ["back"] = "front"}
- directions = {"north", "east", "south", "west"}
- directionsReverse = {["down"] = "up", ["up"] = "down", ["north"] = "south", ["south"] = "north", ["east"] = "west", ["west"] = "east"}
- output = "bottom"
- craftingLoc = {1, 2, 3, 5, 6, 7, 9, 10, 11}
- redstone.setOutput("back", false)
- chestTranslate = {["up"] = "up", ["down"] = "down"}
- for a,b in pairs(directions) do
- if b == facing then
- e = a
- for c,d in pairs(side) do
- chestTranslate[d] = directions[e]
- e = e + 1
- if e > 4 then e = 1 end
- end
- end
- end
- table.insert(side, "top") table.insert(side, "bottom") table.insert(directions, "up") table.insert(directions, "down")
- if fs.exists("dataStore") then
- local fh = fs.open("dataStore", 'r')
- data = textutils.unserialize(fh:readAll())
- fh.close()
- end
- data = data or {}
- function wrapper(param)
- local result = false
- result = peripheral.wrap(param) or false
- if result == false then print("missing chest @ "..param) end
- return result
- end
- chest = wrapper(output)
- function crafting(paramA, paramB)
- local result, tempPeripheral, inputData, materialsData, requiredMaterials, gatheringMaterials, matReturn = false, false, {}, {}, true, true, {}
- print(paramA)
- for a,b in pairs(input) do
- tempPeripheral = wrapper(b)
- if tempPeripheral ~= false then
- tempPeripheral.condenseItems()
- inputData[b] = tempPeripheral.getAllStacks()
- end
- end
- for a,b in pairs(inputData) do
- for c,d in pairs(inputData[a]) do
- materialsData[inputData[a][c]["id"]..":"..inputData[a][c]["rawName"]] = (materialsData[inputData[a][c]["id"]..":"..inputData[a][c]["rawName"]] or 0) + inputData[a][c]["qty"]
- end
- end
- for a,b in pairs(data[paramA][paramB]["materials"]) do
- if tostring(materialsData[a]) ~= "nil" then
- if tonumber(materialsData[a]) < b then
- requiredMaterials = false
- end
- else
- requiredMaterials = false
- end
- end
- if requiredMaterials then
- for y,z in pairs(data[paramA][paramB]["recipe"]) do
- done = false
- inputData = {}
- for a,b in pairs(input) do
- tempPeripheral = wrapper(b)
- if tempPeripheral ~= false then
- tempPeripheral.condenseItems()
- inputData[b] = tempPeripheral.getAllStacks()
- end
- end
- for a,b in pairs(inputData) do
- for c,d in pairs(inputData[a]) do
- if done == false then
- if inputData[a][c]["id"]..":"..inputData[a][c]["rawName"] == data[paramA][paramB]["recipe"][y]["id"] then
- if inputData[a][c]["qty"] >= data[paramA][paramB]["recipe"][y]["qty"] then
- tempPeripheral, matReturn[y] = wrapper(a), a
- -- print(y..":"..data[paramA][paramB]["recipe"][y]["id"]..":"..c..":"..data[paramA][paramB]["recipe"][y]["qty"]..":"..craftingLoc[y])
- tempPeripheral.pushItemIntoSlot(chestTranslate[sideReverse[a]], c, data[paramA][paramB]["recipe"][y]["qty"], craftingLoc[y])
- done = true
- end
- end
- end
- end
- end
- end
- -- sleep(timer)
- -- print("--")
- turtle.select(4)
- done = true
- for y,z in pairs(data[paramA][paramB]["recipe"]) do
- -- print(y..":"..turtle.getItemCount(y)..":"..data[paramA][paramB]["recipe"][y]["qty"])
- if turtle.getItemCount(craftingLoc[y]) == data[paramA][paramB]["recipe"][y]["qty"] then
- done = done
- else
- done = false
- end
- end
- if done then result = turtle.craft() end
- for a,b in pairs(matReturn) do
- tempPeripheral = wrapper(matReturn[a])
- tempPeripheral.pullItemIntoSlot(chestTranslate[sideReverse[matReturn[a]]], craftingLoc[a], 64)
- end
- tempPeripheral = wrapper(output)
- tempPeripheral.pullItemIntoSlot(chestTranslate[sideReverse[output]], 4, 64)
- end
- return result
- end
- function scan()
- local craft, tempListA, tempListB, result = true, nil, {}, false
- for a,b in pairs(data) do
- chest.condenseItems()
- -- sleep(0.5)
- tempListA = chest.getAllStacks()
- tempListB = {}
- for a,b in pairs(tempListA) do tempListB[tempListA[a]["id"]..":"..tempListA[a]["rawName"]] = (tempListB[tempListA[a]["id"]..":"..tempListA[a]["rawName"]] or 0) + tempListA[a]["qty"] end
- if data[a]["min"] ~= false then
- if tostring(tempListB[a]) == "nil" or tempListB[a] < data[a]["min"] then
- craft = crafting(a, "craftMin")
- result = true
- else
- craft = false
- end
- end
- if tostring(tempListB[a]) ~= "nil" and data[a]["max"] ~= false then
- if tempListB[a] > data[a]["max"] then
- craft = crafting(a, "craftMax")
- result = true
- else
- craft = false
- end
- end
- end
- return result
- end
- if tArgs[1] == "mem" then
- local tempPeripheral, listA, listB, paramMin, paramMax, tempData, num, hold = wrapper(output), nil, {}, nil, nil, data, 0, nil
- local lock = true
- while lock do
- term.clear() term.setCursorPos(1,1)
- print("needs empty chest @ output to work")
- print("place result item in first slot of turtle and hit enter")
- read()
- if turtle.getItemCount(1) >= 1 then lock = false end
- end
- tempPeripheral.pullItemIntoSlot(chestTranslate[sideReverse[output]], 1, 1, 1)
- listA = tempPeripheral.getAllStacks()
- hold = listA[1]["id"]..":"..listA[1]["rawName"]
- tempData[hold] = {["min"] = false, ["max"] = false, ["craftMin"] = {["recipe"] = false, ["materials"] = false}, ["craftMax"] = {["recipe"] = false, ["materials"] = false}}
- tempPeripheral.pushItemIntoSlot(chestTranslate[sideReverse[output]], 1, 1, 1)
- term.clear() term.setCursorPos(1,1)
- print("type minium amount to keep stocked or false and hit enter")
- tempData[hold]["min"] = tonumber(read()) or false
- term.clear() term.setCursorPos(1,1)
- print("type maxium amount to keep stocked or false and hit enter")
- tempData[hold]["max"] = tonumber(read()) or false
- if tempData[hold]["min"] ~= false then
- lock = true
- while lock do
- term.clear() term.setCursorPos(1,1)
- print("remove previous setup items")
- print("place minium craft recipe in top-left 3x3 format and hit enter")
- read()
- num = 0
- for a,b in pairs(craftingLoc) do num = num + turtle.getItemCount(b) end
- if num >= 1 then lock = false end
- end
- for a,b in pairs(craftingLoc) do tempPeripheral.pullItemIntoSlot(chestTranslate[sideReverse[output]], b, 64, a) end
- listA = tempPeripheral.getAllStacks()
- tempData[hold]["craftMin"]["materials"], tempData[hold]["craftMin"]["recipe"] = {}, {}
- for a,b in pairs(listA) do
- tempData[hold]["craftMin"]["materials"][listA[a]["id"]..":"..listA[a]["rawName"]] = (tempData[hold]["craftMin"]["materials"][listA[a]["id"]..":"..listA[a]["rawName"]] or 0) + listA[a]["qty"]
- tempData[hold]["craftMin"]["recipe"][a] = {["id"] = listA[a]["id"]..":"..listA[a]["rawName"], ["qty"] = listA[a]["qty"]}
- end
- for a,b in pairs(craftingLoc) do tempPeripheral.pushItemIntoSlot(chestTranslate[sideReverse[output]], a, 64, b) end
- end
- if tempData[hold]["max"] ~= false then
- lock = true
- while lock do
- term.clear() term.setCursorPos(1,1)
- print("remove previous setup items")
- print("place maxium craft recipe in top-left 3x3 format and hit enter")
- read()
- num = 0
- for a,b in pairs(craftingLoc) do num = num + turtle.getItemCount(b) end
- if num >= 1 then lock = false end
- end
- for a,b in pairs(craftingLoc) do tempPeripheral.pullItemIntoSlot(chestTranslate[sideReverse[output]], b, 64, a) end
- listA = tempPeripheral.getAllStacks()
- tempData[hold]["craftMax"]["materials"], tempData[hold]["craftMax"]["recipe"] = {}, {}
- for a,b in pairs(listA) do
- tempData[hold]["craftMax"]["materials"][listA[a]["id"]..":"..listA[a]["rawName"]] = (tempData[hold]["craftMax"]["materials"][listA[a]["id"]..":"..listA[a]["rawName"]] or 0) + listA[a]["qty"]
- tempData[hold]["craftMax"]["recipe"][a] = {["id"] = listA[a]["id"]..":"..listA[a]["rawName"], ["qty"] = listA[a]["qty"]}
- end
- for a,b in pairs(craftingLoc) do tempPeripheral.pushItemIntoSlot(chestTranslate[sideReverse[output]], a, 64, b) end
- end
- data = tempData
- local fh = fs.open("dataStore", 'w')
- fh.write(textutils.serialize(data))
- fh.close()
- else
- while true do
- local result = false
- result = scan()
- if not result then
- os.startTimer(1)
- os.pullEvent()
- end
- end
- end
Add Comment
Please, Sign In to add comment