Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local args = {...}
- local input_inv = peripheral.wrap("minecraft:barrel_9")
- local available_invs = {
- ["forge:gems"] = { [1] = peripheral.wrap("minecraft:barrel_2") },
- ["forge:seeds"] = { [1] = peripheral.wrap("minecraft:barrel_3"), [2] = peripheral.wrap("minecraft:barrel_14"), [3] = peripheral.wrap("minecraft:barrel_16") },
- ["forge:crops"] = { [1] = peripheral.wrap("minecraft:barrel_4"), [2] = peripheral.wrap("minecraft:barrel_15"), [3] = peripheral.wrap("minecraft:barrel_17") },
- ["minecraft:saplings"] = { [1] = peripheral.wrap("minecraft:barrel_5") },
- ["minecraft:logs"] = { [1] = peripheral.wrap("minecraft:barrel_6") },
- ["minecraft:fishes"] = { [1] = peripheral.wrap("minecraft:barrel_7") },
- ["forge:ores"] = { [1] = peripheral.wrap("minecraft:barrel_10"), [2] = peripheral.wrap("minecraft:barrel_19") },
- ["forge:dusts"] = { [1] = peripheral.wrap("minecraft:barrel_11") },
- ["forge:ingots"] = { [1] = peripheral.wrap("minecraft:barrel_12") },
- ["forge:nuggets"] = { [1] = peripheral.wrap("minecraft:barrel_13") },
- ["minecraft:flowers"] = { [1] = peripheral.wrap("minecraft:barrel_18") },
- -- 20, 21, 22
- }
- local inv_colors = {
- ["forge:ores"] = colors.blue,
- ["forge:dusts"] = colors.green,
- ["forge:gems"] = colors.red,
- ["forge:seeds"] = colors.yellow,
- ["forge:crops"] = colors.brown,
- ["minecraft:saplings"] = colors.purple,
- ["minecraft:logs"] = colors.lime,
- ["minecraft:fishes"] = colors.pink,
- ["forge:ingots"] = colors.lightBlue,
- ["forge:nuggets"] = colors.magenta,
- ["minecraft:flowers"] = colors.orange
- }
- local function tcontains_val(table, needle)
- if table then
- for _, v in ipairs(table) do
- if v == needle then
- return true
- end
- end
- end
- return false
- end
- local function tcontains_key(table, needle)
- for k, _ in pairs(table) do
- if k == needle then
- return true
- end
- end
- return false
- end
- local function space_available(inventory)
- local available_space = inventory.size() * 64
- local used_space = 0
- for slot, v in inventory.list() do
- local details = v.getItemDetail(slot)
- -- Make sure that we get the actual max space:
- -- if a slot's maximum amount of items is less than 64
- -- replace that slots available_space accordingly
- local max_count = details["maxCount"]
- if max_count < 64 then
- available_space = available_space - 64 + max_count
- end
- used_space = used_space + v["count"]
- end
- return used_space < available_space
- end
- local function slots_available(inventory)
- local available_slots = inventory.size()
- local used_slots = 0
- for _, _ in ipairs(inventory.list()) do
- used_slots = used_slots + 1
- end
- return used_slots < available_slots
- end
- local function choose(inventories, tag)
- local chosen
- for _, inv in ipairs(inventories) do
- if slots_available(inv) then
- chosen = inv
- end
- end
- if chosen == nil then
- print("No inventories for " .. tag .. " available!")
- end
- return chosen
- end
- local function sort_by_tag(tag)
- for slot = 1, input_inv.size() do
- local details = input_inv.getItemDetail(slot)
- if details ~= nil then
- if tcontains_key(details["tags"], tag) then
- choose(available_invs[tag], tag).pullItems(peripheral.getName(input_inv), slot)
- end
- end
- end
- end
- local function sort()
- for tag, _ in pairs(available_invs) do
- sort_by_tag(tag)
- end
- end
- local function category(tag, col_width)
- local category = string.gsub(tag, "%a*:", "")
- local len = string.len(category)
- local first = string.sub(category, 1, 1)
- first = string.upper(first) -- Uppercase the first char
- local rest = string.sub(category, 2)
- return ' ' .. first .. rest .. string.rep(' ', col_width - len - 1)
- end
- local function items(inventories)
- local items = {}
- for inv_idx, inventory in ipairs(inventories) do
- for slot, item in ipairs(inventory.list()) do
- local details = inventory.getItemDetail(slot)
- -- this should be an update instead!
- table.insert(items, details)
- end
- end
- return items
- end
- local function display()
- local mon = peripheral.wrap("monitor_1")
- mon.setTextScale(0.5)
- mon.clear()
- local width, height = mon.getSize()
- local num_invs = 7 -- This is manual, so be careful
- local col_width = math.ceil(width / num_invs)
- local col = 0
- for k, v in pairs(available_invs) do
- mon.setCursorPos((col * col_width) + 1, 1)
- mon.setBackgroundColor(inv_colors[k])
- mon.write(category(k, col_width))
- mon.setBackgroundColor(colors.black)
- local inv = items(v)
- local num_entries = 0;
- for slot, item in ipairs(inv) do
- if item["count"] > 0 then
- local _, y = mon.getCursorPos()
- mon.setCursorPos((col * col_width) + 1, y + 1)
- mon.write(item["displayName"] .. '\t' .. item["count"])
- num_entries = num_entries + 1
- end
- end
- if num_entries > 0 then
- col = col + 1
- end
- end
- end
- -- This doesn't work anymore
- local function request(name, count) -- currently this can only request 64 items max ):
- for k, v in pairs(available_invs) do
- local num_transferred = 0
- local inv = items(v)
- for slot, item in ipairs(inv) do
- if (string.find(item["name"], name)) ~= nil then
- input_inv.pullItems(peripheral.getName(v), slot, math.min(count, 64))
- end
- end
- end
- end
- local function main(args)
- if args[1] == "sort" then
- print("Sorting...")
- sort()
- print("Done!")
- elseif args[1] == "display" then
- display()
- elseif args[1] == "request" then
- local item_name = args[2]
- local item_count = tonumber(args[3])
- request(item_name, item_count)
- end
- end
- main(args)
Add Comment
Please, Sign In to add comment