Blackhome

InventoryFunctions

Dec 3rd, 2025 (edited)
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.38 KB | Gaming | 0 0
  1. -- pastebin get wCZcgvnn fkt_inventory
  2.  
  3. local function contains(list, value)
  4.     if type(list) ~= "table" then
  5.         if list == value then
  6.             return true
  7.         end
  8.         return false
  9.     end
  10.  
  11.     for _, v in pairs(list) do
  12.         if v == value then
  13.             return true
  14.         end
  15.  
  16.         if type(v) == "table" then
  17.             for _, innerValue in pairs(v) do
  18.                 if innerValue == value then
  19.                     return true
  20.                 end
  21.             end
  22.         end
  23.     end
  24.     return false
  25. end
  26.  
  27. local function findIndex(list, value)
  28.     if type(list) ~= "table" then
  29.         if list == value then
  30.             return 0
  31.         end
  32.         return nil
  33.     end
  34.  
  35.     for mainIndex, v in ipairs(list) do
  36.         if v == value then
  37.             return mainIndex
  38.         end
  39.  
  40.         if type(v) == "table" then
  41.             for _, innerValue in pairs(v) do
  42.                 if innerValue == value then
  43.                     return mainIndex
  44.                 end
  45.             end
  46.         end
  47.     end
  48.     return nil
  49. end
  50.  
  51. local function listContainsSubstring(list, strText)
  52.     if type(list) == "string" and string.find(list, strText) then
  53.         return true
  54.     end
  55.     if type(list) ~= "table" then
  56.         return false
  57.     end
  58.  
  59.     for _, element in ipairs(list) do
  60.         if type(element) == "string" and string.find(element, strText) then
  61.             return true
  62.         end
  63.     end
  64.     return false
  65. end
  66.  
  67. local function isListElementSubstringOf(searchList, targetString)
  68.     if type(targetString) ~= "string" then
  69.         return false
  70.     end
  71.  
  72.     if type(searchList) == "string" then
  73.         if string.find(targetString, searchList) then
  74.             return true
  75.         end
  76.     end
  77.    
  78.     if type(searchList) ~= "table" then
  79.         return false
  80.     end
  81.  
  82.     for _, element in ipairs(searchList) do
  83.         if type(element) == "string" then
  84.             if string.find(targetString, element) then
  85.                 return true
  86.             end
  87.         end
  88.     end
  89.  
  90.     return false
  91. end
  92.  
  93. local function shallowCopy(list)
  94.     newList = {}
  95.     for _, i in ipairs(list) do
  96.         table.insert(newList, i)
  97.     end
  98.     return newList
  99. end
  100.  
  101. local function combine(list1, list2, varType)
  102.     requiredType = varType
  103.     retList = {}
  104.  
  105.     local function processInput(input)
  106.         if input == nil then
  107.             return
  108.         end
  109.  
  110.         local input_type = type(input)
  111.  
  112.         if input_type ~= "table" then
  113.             -- input is single value
  114.             if requiredType == nil or input_type == requiredType then
  115.                 table.insert(retList, input)
  116.             end
  117.         else
  118.             -- input is table
  119.             for _, item in ipairs(input) do
  120.                 if requiredType == nil or type(item) == requiredType then
  121.                     table.insert(retList, item)
  122.                 end
  123.             end
  124.         end
  125.     end
  126.    
  127.     processInput(list1)
  128.     processInput(list2)
  129.  
  130.     return retList
  131. end
  132.  
  133. Inventory = {}
  134.  
  135. inventoryList = {
  136.                 "minecraft:chest",
  137.                 "shulker_box",
  138. }
  139.  
  140. -- select slot with given item name
  141. function Inventory.selectItem(itemName)
  142.     for i = 1, 16, 1 do
  143.         item = turtle.getItemDetail(i)
  144.         if item and item.count > 0 then
  145.             if item.name == itemName then
  146.                 turtle.select(i)
  147.                 return true
  148.             end
  149.         end
  150.     end
  151.     return false
  152. end
  153.  
  154. -- Dumps inventory except items in itemList.
  155. -- ItemList can be
  156. --      1. One string (item name)               - All items with this name will stay in inventory
  157. --      2. A list of strings (item names)       - All items with strings in this list stay in inventory
  158. --      3. A list of tables {itemName, amount}  - The given amount of the item stays in inventory
  159. --      4. A mix of 2. and 3.                   - Single strings stay in inventory, tables in specified amount
  160. function Inventory.dumpKeepItems(dumpDir, itemList, extInvList)
  161.     fktInspect = {
  162.         ["front"] = turtle.inspect,
  163.         ["top"] = turtle.inspectUp,
  164.         ["bottom"] = turtle.inspectDown
  165.     }
  166.     fktDrop = {
  167.         ["front"] = turtle.drop,
  168.         ["top"] = turtle.dropUp,
  169.         ["bottom"] = turtle.dropDown
  170.     }
  171.     local inspect = fktInspect[dumpDir] or fktInspect["front"]
  172.     local drop = fktDrop[dumpDir] or fktDrop["front"]
  173.  
  174.     local invList = combine(inventoryList, extInvList, "string")
  175.  
  176.     local success, extInventory = inspect()
  177.     while not (success and isListElementSubstringOf(invList, extInventory.name)) do
  178.         print("No Inventory found. Please place one at the " .. (dumpDir or "front") .. ".")
  179.         print("Press enter to continue.")
  180.         read()
  181.         success, extInventory = inspect()
  182.     end
  183.  
  184.     local exceptionCount = {}
  185.  
  186.     for i = 1, 16, 1 do
  187.         item = turtle.getItemDetail(i)
  188.         if item and item.count > 0 then
  189.             local index = findIndex(itemList, item.name)
  190.             if index then
  191.                 if type(itemList[index]) == "table" then
  192.                     local cntItem = exceptionCount[index] or 0
  193.                     local exeptionName = itemList[index][1]
  194.                     local numExceptions = itemList[index][2] - cntItem
  195.  
  196.                     local dropNumber = math.max(0, item.count - numExceptions)
  197.  
  198.                     exceptionCount[index] = cntItem + item.count - dropNumber
  199.                     turtle.select(i)
  200.                     drop(dropNumber)
  201.                 end
  202.             else
  203.                 turtle.select(i)
  204.                 drop()
  205.             end
  206.         end
  207.     end
  208.  
  209.     turtle.select(1)
  210. end
  211.  
  212. -- Compacts the inventory by stacking same items on top of each other if possible
  213. function Inventory.compactInventory()
  214.     for i = 1, 16 do
  215.         local itemI = turtle.getItemDetail(i)
  216.         if itemI and itemI.count < 64 then
  217.             for j = i + 1, 16 do
  218.                 local itemJ = turtle.getItemDetail(j)
  219.                 if itemJ and itemJ.name == itemI.name then
  220.                     turtle.select(j)
  221.                     turtle.transferTo(i)
  222.                    
  223.                     -- Aktuellen Slot updaten
  224.                     itemI = turtle.getItemDetail(i)
  225.                     if not itemI or itemI.count == 64 then
  226.                         break
  227.                     end
  228.                 end
  229.             end
  230.         end
  231.     end
  232.     turtle.select(1) -- zum Standard zurückkehren
  233. end
  234.  
  235. -- gets specified item in given amount (min(available, requested)) from chest
  236. function Inventory.suckSpecificItem(itemName, count, strDir)
  237.     local chest = peripheral.wrap(strDir)
  238.     if not chest or not chest.list then
  239.         return false
  240.     end
  241.  
  242.     local chestItems = chest.list()
  243.     local foundSlots = {}
  244.     local totalAvailable = 0
  245.  
  246.     -- 1. Scans chest for requested Item
  247.     for slot, item in pairs(chestItems) do
  248.         if item.name == itemName then
  249.             table.insert(foundSlots, {slot = slot, count = item.count})
  250.             totalAvailable = totalAvailable + item.count
  251.         end
  252.     end
  253.     count = math.min(totalAvailable, count)
  254.     if count == 0 then
  255.         return false
  256.     end
  257.  
  258.     -- 2. Looks for available Slot for cached item
  259.     local cachedItemSlot = nil
  260.     for i = 1, 16 do
  261.         if turtle.getItemCount(i) == 0 then
  262.             cachedItemSlot = i
  263.             break
  264.         end
  265.     end
  266.  
  267.     if not cachedItemSlot then
  268.         print("Kein freier Slot zum Zwischenspeichern des Kisten-Items.")
  269.         return false
  270.     end
  271.  
  272.     turtle.select(cachedItemSlot)
  273.     local cachedItem = nil
  274.  
  275.     fktSuck = {
  276.         ["front"] = turtle.suck,
  277.         ["top"] = turtle.suckUp,
  278.         ["bottom"] = turtle.suckDown
  279.     }
  280.     fktDrop = {
  281.         ["front"] = turtle.drop,
  282.         ["top"] = turtle.dropUp,
  283.         ["bottom"] = turtle.dropDown
  284.     }
  285.     suck = fktSuck[strDir] or fktSuck["front"]
  286.     drop = fktDrop[strDir] or fktDrop["front"]
  287.  
  288.  
  289.     local list = chest.list()
  290.     if list[1] and list[1].name ~= itemName then
  291.         if suck(64) then
  292.             cachedItem = turtle.getItemDetail(cachedItemSlot)
  293.         end
  294.     end
  295.  
  296.     -- 3. Moves first requested item to slot 1
  297.     local nextSlotIndex = 1
  298.     if not chest.getItemDetail(1) then
  299.         local nextSource = foundSlots[nextSlotIndex]
  300.         chest.pushItems(strDir, nextSource.slot, nextSource.count, 1)
  301.         nextSlotIndex = nextSlotIndex + 1
  302.     elseif (chest.getItemDetail(1) and chest.getItemDetail(1).name == itemName) then
  303.         nextSlotIndex = nextSlotIndex + 1
  304.     end
  305.    
  306.     -- 4. Returns cached item (returns to first available chest slot ≠ 1)
  307.     if cachedItem then
  308.         turtle.select(cachedItemSlot)
  309.         drop()
  310.     end
  311.    
  312.     -- 5. Pulls requested items
  313.     local collected = 0
  314.     turtle.select(cachedItemSlot)
  315.     while collected < count do
  316.         local remaining = count - collected
  317.         local slot1Item = chest.getItemDetail(1)
  318.         local pullCount = math.min(slot1Item.count, remaining)
  319.        
  320.         if suck(pullCount) then
  321.             collected = collected + pullCount
  322.         end
  323.  
  324.         if collected < count and nextSlotIndex <= #foundSlots then
  325.             local nextSource = foundSlots[nextSlotIndex]
  326.             chest.pushItems(strDir, nextSource.slot, nextSource.count, 1)
  327.             nextSlotIndex = nextSlotIndex + 1
  328.         end
  329.     end
  330.     return true
  331. end
  332.  
  333. return Inventory
Advertisement
Add Comment
Please, Sign In to add comment