Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- NAFA - Not another Furnace Automater by CreeperGoBoom
- This program works in 2 simple steps.
- 1. Network your chests and furnaces up.
- 2. Install this program onto a networked computer.
- Alternatively you could set this up first and it will detect all new chests and furnaces as you set up.
- To reconfigure chest / furnace layout:
- Simply add and remove furnaces ((Coming soon!) and chests) as required.
- BUGFIXES:
- -Fixed furnaces crashing script due to disconnect bug.
- -Note: Now each furnace that disconnects is removed and then readded to furnace list.
- TODO:
- -Chest readding and checking like furnaces.
- -Table to keep track of peripheral types.
- -Shulker boxes.
- FEATURES:
- -Now runs in parallel, additional event features now possible.
- -Now feeds fuel and ingredients based on vars below.
- -For example, if a furnace runs out of fuel. it will then send fuel at the fuel rate specified
- and wait for the furnace to run out before refueling.
- -Plethora optimized.
- -Fuel and ingredient rates shown on main screen.
- ]]
- --VARS
- local furnaceFuelRate = 1 --How much fuel will be placed into the furnaces when empty?
- local furnaceIngredientsRate = 8 --How many ingredients to send to furnaces at one time?
- local ingredients = {"minecraft:cobblestone", "minecraft:sand", "minecraft:iron_ore", "minecraft:gold_ore", "minecraft:log" }
- local fuels = {"minecraft:coal", "minecraft:sapling"}
- local idprint = false --saves all item data from chests into file idprint.lua
- local requiredAPIFuncs = {
- "fileWrite",
- "saveConfig",
- "colorPrint",
- "getPeripherals",
- }
- local function httpGet(stringURL, stringFileNameToSaveTo)
- local h, err = http.get(stringURL)
- if not h then printError(err) return nil end
- local f = fs.open(stringFileNameToSaveTo, "w")
- f.write(h.readAll())
- f.close()
- h.close()
- return true
- end
- if not fs.exists("apis/CGBCoreLib.lua") then
- if not httpGet("https://pastebin.com/raw/xuMVS2GP", "apis/CGBCoreLib.lua") then
- error("Error: Dependancy 'CGBCoreLib' could not be downloaded. Please connect your internet and restart")
- end
- local core = require("apis/CGBCoreLib") --Contains complete function library used accross multiple programs and to minimize code size.
- for _ , func in pairs(requiredAPIFuncs) do --For API checking to ensure not outdated
- if not core[func] then
- if not httpGet("https://pastebin.com/raw/xuMVS2GP", "apis/CGBCoreLib.lua") then
- error("Error: Your version of CGBCoreLib is outdated! Please connect your internet and restart!")
- else
- os.reboot()
- end
- end
- end
- local furnaces
- local chests
- local shulkers
- local storage
- local data = {}
- local debug = {}
- local function getItemList(chestName)
- local meta = peripheral.call(chestName,"list")
- return meta
- end
- local function getChestInfo()
- local meta = {}
- for key, val in pairs(storage) do
- meta.chestName = val.name
- meta[val] = getItemList(val)
- end
- if idprint then
- local sData = textutils.serialize(meta)
- core.fileWrite("data/idprint.lua", sData)
- end
- core.saveConfig("data.lua",meta)
- return meta
- end
- local function getStorage(...)
- local temp = {}
- local temp2 = {}
- local count = 1
- local peripherals = peripheral.getNames()
- for k , v in pairs({...}) do
- for _ , name in pairs(peripherals) do
- if name:find(v) then
- table.insert(temp,name)
- end
- end
- end
- return temp
- end
- local function main()
- term.clear()
- term.setCursorPos(1, 1)
- core.colorPrint("orange","NAFA V1.2 (Not Another Furnace Automater)")
- furnaces = core.getPeripherals("furnace")
- storage = getStorage("chest","shulker")
- core.saveConfig("storage.lua",storage)
- data = getChestInfo()
- --Statuses
- if not storage[1] then
- core.colorPrint("red","STATUS: No Storage found!")
- sleep(5)
- elseif not furnaces[1] then
- core.colorPrint("red","STATUS: No Furnaces found!")
- sleep(5)
- elseif storage[1] and furnaces[1] then
- core.colorPrint("green","STATUS: OK!")
- core.colorPrint("green","Fuel rate: " .. furnaceFuelRate .. " per refuel.")
- core.colorPrint("green","Ingredient rate: " .. furnaceIngredientsRate .. " per refill.")
- end
- --Main operation code
- if furnaces[1] and storage[1] then
- --for chestNum , chest in pairs(storage) do
- for furnaceNum , furnace in ipairs(furnaces) do
- for chestName , chestContents in pairs(data) do
- local success = pcall(function ()
- furnaceContents = peripheral.call(furnace, "list")
- for slot , item in pairs(chestContents) do
- --[[Basic order of operation here:
- 1.Check if theres any output to push to chests.
- No point checking fuel levels or ingredients if furnace output is full.
- This allows other furnaces to be used in this case.
- 2.Check fuel.
- Refuel if needed.
- 3.Check that ingredients slot is empty.
- Send new lot of ingredients as per vars at top.]]
- if furnaceContents[3] and furnaceContents[3].count then --only pushes items out if theres something there else it wont call it, makes it more plethora friendly.
- peripheral.call(furnace, "pushItems", chestName, 3, furnaceContents[3].count)
- elseif not (furnaceContents[2] and furnaceContents[2].count) then -- Refuel
- for _ , fuel in ipairs(fuels) do
- if item.name == fuel then
- table.insert(debug,furnace .. " received " .. furnaceFuelRate .. " fuel from " .. chestName)
- if pcall(peripheral.call,chestName, "pushItems", furnace, slot, furnaceFuelRate, 2) then
- break
- end
- end
- end
- elseif not (furnaceContents[1] and furnaceContents[1].count) then -- Refill ingredients
- for _ , ingredient in ipairs(ingredients) do
- if item.name == ingredient then
- table.insert(debug,furnace .. " received " .. furnaceIngredientsRate .. " " .. item.name .. " from " .. chestName)
- if pcall(peripheral.call,chestName, "pushItems", furnace, slot, furnaceIngredientsRate, 1) then
- break
- end
- end
- end
- end
- end
- end)
- if not success then
- table.remove(furnaces, furnaceNum)
- end
- end
- end
- --end
- end
- core.saveConfig("debug.lua",debug)
- end
- local function secondary()
- local x = 10 -- interval in seconds
- local tmr = os.startTimer(x)
- while true do
- local event = {os.pullEvent()}
- if event[1] == "timer" and event[2] == tmr then
- os.queueEvent("runmain") -- tell maim func to run
- tmr = os.startTimer(x)
- elseif event[1] == "peripheral_detach" then
- for k , v in pairs(furnaces) do
- if v == event[2] then
- table.remove(furnaces, k)
- break
- end
- end
- elseif event[1] == "peripheral" then
- if peripheral.getType(event[2]) == "furnace" then
- table.insert(furnaces, #furnaces + 1, event[2])
- end
- end
- end
- end
- while true do
- parallel.waitForAny(main, secondary)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement