LaniusFNV

CC Inventory Manager

Jul 7th, 2021 (edited)
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.21 KB | None | 0 0
  1. local args = {...}
  2.  
  3. local input_inv = peripheral.wrap("minecraft:barrel_9")
  4.  
  5. local available_invs = {
  6.     ["forge:gems"] = { [1] = peripheral.wrap("minecraft:barrel_2") },
  7.     ["forge:seeds"] = { [1] = peripheral.wrap("minecraft:barrel_3"), [2] = peripheral.wrap("minecraft:barrel_14"), [3] = peripheral.wrap("minecraft:barrel_16") },
  8.     ["forge:crops"] = { [1] = peripheral.wrap("minecraft:barrel_4"), [2] = peripheral.wrap("minecraft:barrel_15"), [3] = peripheral.wrap("minecraft:barrel_17") },
  9.     ["minecraft:saplings"] = { [1] = peripheral.wrap("minecraft:barrel_5") },
  10.     ["minecraft:logs"] = { [1] = peripheral.wrap("minecraft:barrel_6") },
  11.     ["minecraft:fishes"] = { [1] = peripheral.wrap("minecraft:barrel_7") },
  12.     ["forge:ores"] = { [1] = peripheral.wrap("minecraft:barrel_10"), [2] = peripheral.wrap("minecraft:barrel_19") },
  13.     ["forge:dusts"] = { [1] = peripheral.wrap("minecraft:barrel_11") },
  14.     ["forge:ingots"] = { [1] = peripheral.wrap("minecraft:barrel_12") },
  15.     ["forge:nuggets"] = { [1] = peripheral.wrap("minecraft:barrel_13") },
  16.     ["minecraft:flowers"] = { [1] = peripheral.wrap("minecraft:barrel_18") },
  17.     -- 20, 21, 22
  18. }
  19.  
  20. local inv_colors = {
  21.     ["forge:ores"] = colors.blue,
  22.     ["forge:dusts"] = colors.green,
  23.     ["forge:gems"] = colors.red,
  24.     ["forge:seeds"] = colors.yellow,
  25.     ["forge:crops"] = colors.brown,
  26.     ["minecraft:saplings"] = colors.purple,
  27.     ["minecraft:logs"] = colors.lime,
  28.     ["minecraft:fishes"] = colors.pink,
  29.     ["forge:ingots"] = colors.lightBlue,
  30.     ["forge:nuggets"] = colors.magenta,
  31.     ["minecraft:flowers"] = colors.orange
  32. }
  33.  
  34. local function tcontains_val(table, needle)
  35.     if table then
  36.         for _, v in ipairs(table) do
  37.             if v == needle then
  38.                 return true
  39.             end
  40.         end
  41.     end
  42.  
  43.     return false
  44. end
  45.  
  46. local function tcontains_key(table, needle)
  47.     for k, _ in pairs(table) do
  48.         if k == needle then
  49.             return true
  50.         end
  51.     end
  52.  
  53.     return false
  54. end
  55.  
  56. local function space_available(inventory)
  57.     local available_space = inventory.size() * 64
  58.     local used_space = 0
  59.  
  60.     for slot, v in inventory.list() do
  61.         local details = v.getItemDetail(slot)
  62.  
  63.         -- Make sure that we get the actual max space:
  64.         -- if a slot's maximum amount of items is less than 64
  65.         -- replace that slots available_space accordingly
  66.         local max_count = details["maxCount"]
  67.         if max_count < 64 then
  68.             available_space = available_space - 64 + max_count
  69.         end
  70.  
  71.         used_space = used_space + v["count"]
  72.     end
  73.  
  74.     return used_space < available_space
  75. end
  76.  
  77. local function slots_available(inventory)
  78.     local available_slots = inventory.size()
  79.     local used_slots = 0
  80.  
  81.     for _, _ in ipairs(inventory.list()) do
  82.         used_slots = used_slots + 1
  83.     end
  84.  
  85.     return used_slots < available_slots
  86. end
  87.  
  88. local function choose(inventories, tag)
  89.     local chosen
  90.  
  91.     for _, inv in ipairs(inventories) do
  92.         if slots_available(inv) then
  93.             chosen = inv
  94.         end
  95.     end
  96.    
  97.     if chosen == nil then
  98.         print("No inventories for " .. tag .. " available!")
  99.     end
  100.     return chosen
  101. end
  102.  
  103. local function sort_by_tag(tag)
  104.     for slot = 1, input_inv.size() do
  105.         local details = input_inv.getItemDetail(slot)
  106.  
  107.         if details ~= nil then
  108.             if tcontains_key(details["tags"], tag) then
  109.                 choose(available_invs[tag], tag).pullItems(peripheral.getName(input_inv), slot)
  110.             end
  111.         end
  112.     end
  113. end
  114.  
  115. local function sort()
  116.     for tag, _ in pairs(available_invs) do
  117.         sort_by_tag(tag)
  118.     end
  119. end
  120.  
  121. local function category(tag, col_width)
  122.     local category = string.gsub(tag, "%a*:", "")
  123.     local len = string.len(category)
  124.  
  125.     local first = string.sub(category, 1, 1)
  126.     first = string.upper(first) -- Uppercase the first char
  127.  
  128.     local rest = string.sub(category, 2)
  129.  
  130.     return ' ' .. first .. rest .. string.rep(' ', col_width - len - 1)
  131. end
  132.  
  133. local function items(inventories)
  134.     local items = {}
  135.  
  136.     for inv_idx, inventory in ipairs(inventories) do
  137.         for slot, item in ipairs(inventory.list()) do
  138.             local details = inventory.getItemDetail(slot)
  139.             -- this should be an update instead!
  140.             table.insert(items, details)
  141.         end
  142.     end
  143.  
  144.     return items
  145. end
  146.  
  147. local function display()
  148.     local mon = peripheral.wrap("monitor_1")
  149.  
  150.     mon.setTextScale(0.5)
  151.     mon.clear()
  152.  
  153.     local width, height = mon.getSize()
  154.     local num_invs = 7 -- This is manual, so be careful
  155.  
  156.     local col_width = math.ceil(width / num_invs)
  157.  
  158.     local col = 0
  159.        
  160.     for k, v in pairs(available_invs) do
  161.         mon.setCursorPos((col * col_width) + 1, 1)
  162.         mon.setBackgroundColor(inv_colors[k])
  163.         mon.write(category(k, col_width))
  164.         mon.setBackgroundColor(colors.black)
  165.  
  166.         local inv = items(v)
  167.  
  168.         local num_entries = 0;
  169.         for slot, item in ipairs(inv) do
  170.             if item["count"] > 0 then
  171.                 local _, y = mon.getCursorPos()
  172.                 mon.setCursorPos((col * col_width) + 1, y + 1)
  173.  
  174.                 mon.write(item["displayName"] .. '\t' .. item["count"])
  175.                 num_entries = num_entries + 1
  176.             end
  177.         end
  178.  
  179.         if num_entries > 0 then
  180.             col = col + 1
  181.         end
  182.     end
  183. end
  184.  
  185. -- This doesn't work anymore
  186. local function request(name, count) -- currently this can only request 64 items max ):
  187.     for k, v in pairs(available_invs) do
  188.         local num_transferred = 0
  189.  
  190.         local inv = items(v)
  191.  
  192.         for slot, item in ipairs(inv) do
  193.             if (string.find(item["name"], name)) ~= nil then
  194.                 input_inv.pullItems(peripheral.getName(v), slot, math.min(count, 64))
  195.             end
  196.         end
  197.     end
  198. end
  199.  
  200. local function main(args)
  201.     if args[1] == "sort" then
  202.         print("Sorting...")
  203.         sort()
  204.         print("Done!")
  205.     elseif args[1] == "display" then
  206.         display()
  207.     elseif args[1] == "request" then
  208.         local item_name = args[2]
  209.         local item_count = tonumber(args[3])
  210.  
  211.         request(item_name, item_count)
  212.     end
  213. end
  214.  
  215. main(args)
Add Comment
Please, Sign In to add comment