Kacpis

CC: Tweaked Inventory Manager

May 29th, 2024 (edited)
925
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.62 KB | Gaming | 0 0
  1. --------------------------------------------------------- CONFIG ---------------------------------------------------------
  2. local Hz = 1
  3. local RECIPES = {
  4.     {   -- importing spruce logs into the system (makes it avaible to be exported)
  5.         type = "import",
  6.         inv = peripheral.wrap("minecraft:barrel_6"),
  7.     },
  8.     {
  9.         type = "storage",
  10.         allowExport = false,
  11.         inv = peripheral.wrap("minecraft:barrel_0"),
  12.         items = {
  13.             {
  14.                 id = "minecraft:cobblestone"
  15.             }
  16.         }
  17.     },
  18.     {
  19.         type = "storage",
  20.         allowExport = true,
  21.         inv = peripheral.wrap("minecraft:barrel_1"),
  22.         items = {
  23.             {
  24.                 id = "minecraft:stone"
  25.             }
  26.         }
  27.     },
  28.     {
  29.         type = "redstone",
  30.         peripheral = peripheral.wrap("redrouter_2"),
  31.         side = "back",
  32.         equation = {
  33.             {
  34.                 id = "minecraft:cobblestone",
  35.                 amount = 1660,
  36.                 symbol = ">"
  37.             }
  38.         }
  39.     },
  40.     {   -- SMELTER
  41.         type = "import",
  42.         inv = peripheral.wrap("minecraft:barrel_7"),
  43.     },
  44.     {   -- stone
  45.         type = "export",
  46.         inv = peripheral.wrap("create:chute_0"),
  47.         items = {
  48.             {
  49.                 id = "minecraft:cobblestone",    -- if you want nbt tag, then put it in a () eg. "minecraft:diamond_sword ('nbt tag here')"
  50.                 amount = 64,
  51.             }
  52.         },
  53.         equation = {    -- by adding equation, it will only trigger the exporter when it's true
  54.             {
  55.                 id = "minecraft:stone",
  56.                 amount = 1264,
  57.                 symbol = "<"
  58.             }
  59.         }
  60.     },
  61.     --{   -- exporting spruce logs out of the system
  62.     --    type = "export",
  63.     --    inv = peripheral.wrap("minecraft:barrel_5"),
  64.     --    items = {
  65.     --        {
  66.     --            id = "minecraft:spruce_log",    -- if you want nbt tag, then put it in a () eg. "minecraft:diamond_sword ('nbt tag here')"
  67.     --            amount = 64,
  68.     --        }
  69.     --    }
  70.     --},
  71.     -- {   -- redstone, outputs to back when spruce_log in system is > 300
  72.     --     type = "redstone",
  73.     --     peripheral = peripheral.wrap("redrouter_3"),
  74.     --     side = "back",
  75.     --     equation = {
  76.     --         {
  77.     --             id = "minecraft:spruce_log",
  78.     --             amount = 128,
  79.     --             symbol = ">" -- greater than (or < less or == equal)
  80.     --         }
  81.     --     }
  82.     -- },
  83.     -- {   -- storage just for stone and spruce logs
  84.     --     type = "storage",
  85.     --     inv = peripheral.wrap("minecraft:chest_5"),
  86.     --     allowExport = true,
  87.     --     items = {
  88.     --         {
  89.     --             id = "minecraft:spruce_log",
  90.     --         },
  91.     --         {
  92.     --             id = "minecraft:stone",
  93.     --         },
  94.     --     }
  95.     -- },
  96.     -- {   -- global storage (every item) no 'items' table specified
  97.     --     type = "storage",  
  98.     --     inv = peripheral.wrap("minecraft:chest_6"),
  99.     --     allowExport = true,
  100.     -- }
  101. }
  102. --------------------------------------------------------- END OF CONFIG ---------------------------------------------------------
  103. --                                                      DO NOT EDIT BELOW
  104.  
  105. local itemsAvaiable = {}
  106.  
  107. TYPEENUMS = {
  108.     ["import"] = true,
  109.     ["export"] = true,
  110.     ["storage"] = true,
  111.     ["redstone"] = true,
  112. }
  113.  
  114.  
  115. -- divide import, export, storage and redstone types into different tables
  116. local INVs = {
  117.     import = {},
  118.     export = {},
  119.     storage = {},
  120.     redstone = {},
  121. }
  122. for k,v in pairs(RECIPES) do
  123.     if TYPEENUMS[v.type] then
  124.  
  125.         if v.type == "storage" then -- if we're dealing with storage, we should have those with specififed items first, and the global storages last
  126.             if v.items then
  127.                 table.insert(INVs[v.type],1,v)
  128.             else
  129.                 table.insert(INVs[v.type],#INVs[v.type]+1,v)
  130.             end
  131.            
  132.         else
  133.             table.insert(INVs[v.type],v)
  134.         end
  135.     end
  136. end
  137.  
  138. -- draw main screen
  139. function drawTerminal()
  140.     term.clear()
  141.     local mw,mh = term.getSize()
  142.     local str = "HandlinCargo Inc."
  143.     term.setCursorPos((mw/2)-string.len(str)/2,(mh/2)-1)
  144.     term.write(str)
  145.     local str = "Please make sure you have"
  146.     term.setCursorPos((mw/2)-string.len(str)/2,(mh/2)+1)
  147.     term.write(str)
  148.     local str = "configured the app correctly"
  149.     term.setCursorPos((mw/2)-string.len(str)/2,(mh/2)+2)
  150.     term.write(str)
  151.    
  152.     local str = "Powered by Nilixen"
  153.     term.setCursorPos((mw/2)-string.len(str)/2,(mh-1))
  154.     term.write(str)
  155.    
  156. end
  157. drawTerminal()
  158.  
  159. function scanAvaibleItems()
  160.     local tbl = {}
  161.     -- handles items in import inventories
  162.     for k,import in pairs(INVs.import) do
  163.         local list = import.inv.list()
  164.         for slot, _ in pairs(list) do
  165.             local item = import.inv.getItemDetail(slot)
  166.             if item then
  167.                 local tag = item.name..(item.nbt and " ("..item.nbt..")" or "")
  168.                 if tbl[tag] then
  169.                     tbl[tag].amount = tbl[tag].amount + item.count
  170.                     table.insert(tbl[tag].locations,
  171.                     {
  172.                         inv = import.inv,
  173.                         slot = slot,
  174.                         amount = item.count,
  175.                         type="import"
  176.  
  177.                     })
  178.                 else
  179.                     tbl[tag] = {
  180.                         amount = item.count,
  181.                         locations = {
  182.                             {
  183.                                 inv = import.inv,
  184.                                 slot = slot,
  185.                                 amount = item.count,
  186.                                 type="import"
  187.                             },
  188.                         }
  189.                     }
  190.                 end
  191.             end
  192.         end
  193.     end
  194.     -- counts items from storage if allowExport == true
  195.     for k,storage in pairs(INVs.storage) do
  196.         if storage.allowExport then
  197.             local list = storage.inv.list()
  198.             for slot, _ in pairs(list) do
  199.                 local item = storage.inv.getItemDetail(slot)
  200.                 if item then
  201.                     local tag = item.name..(item.nbt and " ("..item.nbt..")" or "")
  202.                     if tbl[tag] then
  203.                         tbl[tag].amount = tbl[tag].amount + item.count
  204.                         table.insert(tbl[tag].locations,
  205.                         {
  206.                             inv = storage.inv,
  207.                             slot = slot,
  208.                             amount = item.count,
  209.                             type="storage"
  210.  
  211.                         })
  212.                     else
  213.                         tbl[tag] = {
  214.                             amount = item.count,
  215.                             locations = {
  216.                                 {
  217.                                     inv = storage.inv,
  218.                                     slot = slot,
  219.                                     amount = item.count,
  220.                                     type="storage"
  221.  
  222.                                 },
  223.                             }
  224.                         }
  225.                     end
  226.                 end
  227.             end
  228.         end
  229.     end
  230.     return tbl
  231. end
  232.  
  233. local redstoneFunctions = {
  234.     ["<"] = function (a,b) return a > b end,
  235.     [">"] = function (a,b) return a < b end,
  236.     ["=="] = function (a,b) return a == b end,
  237. }
  238. function handleRedstone()
  239.     for k,redstone in pairs(INVs.redstone) do
  240.         local boolean = true
  241.         for _,equation in pairs(redstone.equation) do
  242.             if itemsAvaiable[equation.id] then
  243.                 if redstoneFunctions[equation.symbol](equation.amount,itemsAvaiable[equation.id].amount) == false then
  244.                     boolean = false
  245.                 end
  246.             else
  247.                 if equation.symbol == ">" or equation.symbol == "==" then
  248.                     boolean = false
  249.                 end
  250.             end
  251.         end
  252.         redstone.peripheral.setOutput(redstone.side,boolean)
  253.     end
  254. end
  255.  
  256. function handleExport()
  257.     for k,export in pairs(INVs.export) do
  258.         local items = {}
  259.         local inv = export.inv
  260.         -- check whether we met the equation
  261.         local equationMet = true
  262.         for _,equation in pairs(export.equation) do
  263.             if itemsAvaiable[equation.id] then
  264.                 if redstoneFunctions[equation.symbol](equation.amount,itemsAvaiable[equation.id].amount) == false then
  265.                     equationMet = false
  266.                 end
  267.             else
  268.                 if equation.symbol == ">" or equation.symbol == "==" then
  269.                     equationMet = false
  270.                 end
  271.             end
  272.         end
  273.         if equationMet then
  274.             -- scan inventory to see what we have
  275.             for slot, _ in pairs(inv.list()) do
  276.                 local item = inv.getItemDetail(slot)
  277.                 if item then
  278.                     local tag = item.name..(item.nbt and " ("..item.nbt..")" or "")
  279.                     items[tag] = (items[tag] and items[tag] + item.count or item.count)
  280.                 end
  281.             end
  282.             -- iterate through what it wants and see if we need anything
  283.             local requires = {}
  284.             for i, item in ipairs(export.items) do
  285.                 if items[item.id] then
  286.                     if items[item.id] < item.amount then
  287.                         requires[item.id] = item.amount - items[item.id]
  288.                     end
  289.                 else
  290.                     requires[item.id] = item.amount
  291.                 end
  292.             end
  293.  
  294.             -- find items in imports and storages
  295.             for idRequired, amountRequired in pairs(requires) do
  296.                 local stillRequired = amountRequired
  297.                 if itemsAvaiable[idRequired] then
  298.                     for _, data in pairs(itemsAvaiable[idRequired].locations) do
  299.                         if data.amount >= stillRequired then
  300.                             inv.pullItems(peripheral.getName(data.inv),data.slot,stillRequired)
  301.                             break
  302.                         else
  303.                             inv.pullItems(peripheral.getName(data.inv),data.slot,data.amount)
  304.                             stillRequired = stillRequired - data.amount
  305.                         end
  306.                     end
  307.                 end
  308.             end
  309.         end
  310.     end
  311. end
  312.  
  313. -- get inv fill
  314. function handleStorage()
  315.     -- storage will only pull from importers
  316.     for k,storage in pairs(INVs.storage) do
  317.         local inv = storage.inv
  318.         if storage.items then   -- if you have specified items
  319.             for _,itemRequired in pairs(storage.items) do
  320.                 if itemsAvaiable[itemRequired.id] then
  321.                     for _, data in pairs(itemsAvaiable[itemRequired.id].locations) do
  322.                         if data.type ~= "storage" then
  323.                             inv.pullItems(peripheral.getName(data.inv),data.slot)
  324.                         end
  325.                     end
  326.                 end
  327.             end
  328.         else -- pull everything from importers
  329.             for id,data in pairs(itemsAvaiable) do
  330.                 for _,locations in pairs(data.locations) do
  331.                     if locations.type ~= "storage" then
  332.                         inv.pullItems(peripheral.getName(locations.inv),locations.slot)
  333.                     end
  334.                 end
  335.             end
  336.         end
  337.     end
  338. end
  339.  
  340. while true do
  341.     os.sleep(1/Hz)
  342.     itemsAvaiable = scanAvaibleItems()
  343.  
  344.     handleRedstone()
  345.     handleExport()
  346.     handleStorage()
  347.  
  348. end
Advertisement
Add Comment
Please, Sign In to add comment