Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --------------------------------------------------------- CONFIG ---------------------------------------------------------
- local Hz = 1
- local RECIPES = {
- { -- importing spruce logs into the system (makes it avaible to be exported)
- type = "import",
- inv = peripheral.wrap("minecraft:barrel_6"),
- },
- {
- type = "storage",
- allowExport = false,
- inv = peripheral.wrap("minecraft:barrel_0"),
- items = {
- {
- id = "minecraft:cobblestone"
- }
- }
- },
- {
- type = "storage",
- allowExport = true,
- inv = peripheral.wrap("minecraft:barrel_1"),
- items = {
- {
- id = "minecraft:stone"
- }
- }
- },
- {
- type = "redstone",
- peripheral = peripheral.wrap("redrouter_2"),
- side = "back",
- equation = {
- {
- id = "minecraft:cobblestone",
- amount = 1660,
- symbol = ">"
- }
- }
- },
- { -- SMELTER
- type = "import",
- inv = peripheral.wrap("minecraft:barrel_7"),
- },
- { -- stone
- type = "export",
- inv = peripheral.wrap("create:chute_0"),
- items = {
- {
- id = "minecraft:cobblestone", -- if you want nbt tag, then put it in a () eg. "minecraft:diamond_sword ('nbt tag here')"
- amount = 64,
- }
- },
- equation = { -- by adding equation, it will only trigger the exporter when it's true
- {
- id = "minecraft:stone",
- amount = 1264,
- symbol = "<"
- }
- }
- },
- --{ -- exporting spruce logs out of the system
- -- type = "export",
- -- inv = peripheral.wrap("minecraft:barrel_5"),
- -- items = {
- -- {
- -- id = "minecraft:spruce_log", -- if you want nbt tag, then put it in a () eg. "minecraft:diamond_sword ('nbt tag here')"
- -- amount = 64,
- -- }
- -- }
- --},
- -- { -- redstone, outputs to back when spruce_log in system is > 300
- -- type = "redstone",
- -- peripheral = peripheral.wrap("redrouter_3"),
- -- side = "back",
- -- equation = {
- -- {
- -- id = "minecraft:spruce_log",
- -- amount = 128,
- -- symbol = ">" -- greater than (or < less or == equal)
- -- }
- -- }
- -- },
- -- { -- storage just for stone and spruce logs
- -- type = "storage",
- -- inv = peripheral.wrap("minecraft:chest_5"),
- -- allowExport = true,
- -- items = {
- -- {
- -- id = "minecraft:spruce_log",
- -- },
- -- {
- -- id = "minecraft:stone",
- -- },
- -- }
- -- },
- -- { -- global storage (every item) no 'items' table specified
- -- type = "storage",
- -- inv = peripheral.wrap("minecraft:chest_6"),
- -- allowExport = true,
- -- }
- }
- --------------------------------------------------------- END OF CONFIG ---------------------------------------------------------
- -- DO NOT EDIT BELOW
- local itemsAvaiable = {}
- TYPEENUMS = {
- ["import"] = true,
- ["export"] = true,
- ["storage"] = true,
- ["redstone"] = true,
- }
- -- divide import, export, storage and redstone types into different tables
- local INVs = {
- import = {},
- export = {},
- storage = {},
- redstone = {},
- }
- for k,v in pairs(RECIPES) do
- if TYPEENUMS[v.type] then
- if v.type == "storage" then -- if we're dealing with storage, we should have those with specififed items first, and the global storages last
- if v.items then
- table.insert(INVs[v.type],1,v)
- else
- table.insert(INVs[v.type],#INVs[v.type]+1,v)
- end
- else
- table.insert(INVs[v.type],v)
- end
- end
- end
- -- draw main screen
- function drawTerminal()
- term.clear()
- local mw,mh = term.getSize()
- local str = "HandlinCargo Inc."
- term.setCursorPos((mw/2)-string.len(str)/2,(mh/2)-1)
- term.write(str)
- local str = "Please make sure you have"
- term.setCursorPos((mw/2)-string.len(str)/2,(mh/2)+1)
- term.write(str)
- local str = "configured the app correctly"
- term.setCursorPos((mw/2)-string.len(str)/2,(mh/2)+2)
- term.write(str)
- local str = "Powered by Nilixen"
- term.setCursorPos((mw/2)-string.len(str)/2,(mh-1))
- term.write(str)
- end
- drawTerminal()
- function scanAvaibleItems()
- local tbl = {}
- -- handles items in import inventories
- for k,import in pairs(INVs.import) do
- local list = import.inv.list()
- for slot, _ in pairs(list) do
- local item = import.inv.getItemDetail(slot)
- if item then
- local tag = item.name..(item.nbt and " ("..item.nbt..")" or "")
- if tbl[tag] then
- tbl[tag].amount = tbl[tag].amount + item.count
- table.insert(tbl[tag].locations,
- {
- inv = import.inv,
- slot = slot,
- amount = item.count,
- type="import"
- })
- else
- tbl[tag] = {
- amount = item.count,
- locations = {
- {
- inv = import.inv,
- slot = slot,
- amount = item.count,
- type="import"
- },
- }
- }
- end
- end
- end
- end
- -- counts items from storage if allowExport == true
- for k,storage in pairs(INVs.storage) do
- if storage.allowExport then
- local list = storage.inv.list()
- for slot, _ in pairs(list) do
- local item = storage.inv.getItemDetail(slot)
- if item then
- local tag = item.name..(item.nbt and " ("..item.nbt..")" or "")
- if tbl[tag] then
- tbl[tag].amount = tbl[tag].amount + item.count
- table.insert(tbl[tag].locations,
- {
- inv = storage.inv,
- slot = slot,
- amount = item.count,
- type="storage"
- })
- else
- tbl[tag] = {
- amount = item.count,
- locations = {
- {
- inv = storage.inv,
- slot = slot,
- amount = item.count,
- type="storage"
- },
- }
- }
- end
- end
- end
- end
- end
- return tbl
- end
- local redstoneFunctions = {
- ["<"] = function (a,b) return a > b end,
- [">"] = function (a,b) return a < b end,
- ["=="] = function (a,b) return a == b end,
- }
- function handleRedstone()
- for k,redstone in pairs(INVs.redstone) do
- local boolean = true
- for _,equation in pairs(redstone.equation) do
- if itemsAvaiable[equation.id] then
- if redstoneFunctions[equation.symbol](equation.amount,itemsAvaiable[equation.id].amount) == false then
- boolean = false
- end
- else
- if equation.symbol == ">" or equation.symbol == "==" then
- boolean = false
- end
- end
- end
- redstone.peripheral.setOutput(redstone.side,boolean)
- end
- end
- function handleExport()
- for k,export in pairs(INVs.export) do
- local items = {}
- local inv = export.inv
- -- check whether we met the equation
- local equationMet = true
- for _,equation in pairs(export.equation) do
- if itemsAvaiable[equation.id] then
- if redstoneFunctions[equation.symbol](equation.amount,itemsAvaiable[equation.id].amount) == false then
- equationMet = false
- end
- else
- if equation.symbol == ">" or equation.symbol == "==" then
- equationMet = false
- end
- end
- end
- if equationMet then
- -- scan inventory to see what we have
- for slot, _ in pairs(inv.list()) do
- local item = inv.getItemDetail(slot)
- if item then
- local tag = item.name..(item.nbt and " ("..item.nbt..")" or "")
- items[tag] = (items[tag] and items[tag] + item.count or item.count)
- end
- end
- -- iterate through what it wants and see if we need anything
- local requires = {}
- for i, item in ipairs(export.items) do
- if items[item.id] then
- if items[item.id] < item.amount then
- requires[item.id] = item.amount - items[item.id]
- end
- else
- requires[item.id] = item.amount
- end
- end
- -- find items in imports and storages
- for idRequired, amountRequired in pairs(requires) do
- local stillRequired = amountRequired
- if itemsAvaiable[idRequired] then
- for _, data in pairs(itemsAvaiable[idRequired].locations) do
- if data.amount >= stillRequired then
- inv.pullItems(peripheral.getName(data.inv),data.slot,stillRequired)
- break
- else
- inv.pullItems(peripheral.getName(data.inv),data.slot,data.amount)
- stillRequired = stillRequired - data.amount
- end
- end
- end
- end
- end
- end
- end
- -- get inv fill
- function handleStorage()
- -- storage will only pull from importers
- for k,storage in pairs(INVs.storage) do
- local inv = storage.inv
- if storage.items then -- if you have specified items
- for _,itemRequired in pairs(storage.items) do
- if itemsAvaiable[itemRequired.id] then
- for _, data in pairs(itemsAvaiable[itemRequired.id].locations) do
- if data.type ~= "storage" then
- inv.pullItems(peripheral.getName(data.inv),data.slot)
- end
- end
- end
- end
- else -- pull everything from importers
- for id,data in pairs(itemsAvaiable) do
- for _,locations in pairs(data.locations) do
- if locations.type ~= "storage" then
- inv.pullItems(peripheral.getName(locations.inv),locations.slot)
- end
- end
- end
- end
- end
- end
- while true do
- os.sleep(1/Hz)
- itemsAvaiable = scanAvaibleItems()
- handleRedstone()
- handleExport()
- handleStorage()
- end
Advertisement
Add Comment
Please, Sign In to add comment