Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- pastebin get wCZcgvnn fkt_inventory
- local function contains(list, value)
- if type(list) ~= "table" then
- if list == value then
- return true
- end
- return false
- end
- for _, v in pairs(list) do
- if v == value then
- return true
- end
- if type(v) == "table" then
- for _, innerValue in pairs(v) do
- if innerValue == value then
- return true
- end
- end
- end
- end
- return false
- end
- local function findIndex(list, value)
- if type(list) ~= "table" then
- if list == value then
- return 0
- end
- return nil
- end
- for mainIndex, v in ipairs(list) do
- if v == value then
- return mainIndex
- end
- if type(v) == "table" then
- for _, innerValue in pairs(v) do
- if innerValue == value then
- return mainIndex
- end
- end
- end
- end
- return nil
- end
- local function listContainsSubstring(list, strText)
- if type(list) == "string" and string.find(list, strText) then
- return true
- end
- if type(list) ~= "table" then
- return false
- end
- for _, element in ipairs(list) do
- if type(element) == "string" and string.find(element, strText) then
- return true
- end
- end
- return false
- end
- local function isListElementSubstringOf(searchList, targetString)
- if type(targetString) ~= "string" then
- return false
- end
- if type(searchList) == "string" then
- if string.find(targetString, searchList) then
- return true
- end
- end
- if type(searchList) ~= "table" then
- return false
- end
- for _, element in ipairs(searchList) do
- if type(element) == "string" then
- if string.find(targetString, element) then
- return true
- end
- end
- end
- return false
- end
- local function shallowCopy(list)
- newList = {}
- for _, i in ipairs(list) do
- table.insert(newList, i)
- end
- return newList
- end
- local function combine(list1, list2, varType)
- requiredType = varType
- retList = {}
- local function processInput(input)
- if input == nil then
- return
- end
- local input_type = type(input)
- if input_type ~= "table" then
- -- input is single value
- if requiredType == nil or input_type == requiredType then
- table.insert(retList, input)
- end
- else
- -- input is table
- for _, item in ipairs(input) do
- if requiredType == nil or type(item) == requiredType then
- table.insert(retList, item)
- end
- end
- end
- end
- processInput(list1)
- processInput(list2)
- return retList
- end
- Inventory = {}
- inventoryList = {
- "minecraft:chest",
- "shulker_box",
- }
- -- select slot with given item name
- function Inventory.selectItem(itemName)
- for i = 1, 16, 1 do
- item = turtle.getItemDetail(i)
- if item and item.count > 0 then
- if item.name == itemName then
- turtle.select(i)
- return true
- end
- end
- end
- return false
- end
- -- Dumps inventory except items in itemList.
- -- ItemList can be
- -- 1. One string (item name) - All items with this name will stay in inventory
- -- 2. A list of strings (item names) - All items with strings in this list stay in inventory
- -- 3. A list of tables {itemName, amount} - The given amount of the item stays in inventory
- -- 4. A mix of 2. and 3. - Single strings stay in inventory, tables in specified amount
- function Inventory.dumpKeepItems(dumpDir, itemList, extInvList)
- fktInspect = {
- ["front"] = turtle.inspect,
- ["top"] = turtle.inspectUp,
- ["bottom"] = turtle.inspectDown
- }
- fktDrop = {
- ["front"] = turtle.drop,
- ["top"] = turtle.dropUp,
- ["bottom"] = turtle.dropDown
- }
- local inspect = fktInspect[dumpDir] or fktInspect["front"]
- local drop = fktDrop[dumpDir] or fktDrop["front"]
- local invList = combine(inventoryList, extInvList, "string")
- local success, extInventory = inspect()
- while not (success and isListElementSubstringOf(invList, extInventory.name)) do
- print("No Inventory found. Please place one at the " .. (dumpDir or "front") .. ".")
- print("Press enter to continue.")
- read()
- success, extInventory = inspect()
- end
- local exceptionCount = {}
- for i = 1, 16, 1 do
- item = turtle.getItemDetail(i)
- if item and item.count > 0 then
- local index = findIndex(itemList, item.name)
- if index then
- if type(itemList[index]) == "table" then
- local cntItem = exceptionCount[index] or 0
- local exeptionName = itemList[index][1]
- local numExceptions = itemList[index][2] - cntItem
- local dropNumber = math.max(0, item.count - numExceptions)
- exceptionCount[index] = cntItem + item.count - dropNumber
- turtle.select(i)
- drop(dropNumber)
- end
- else
- turtle.select(i)
- drop()
- end
- end
- end
- turtle.select(1)
- end
- -- Compacts the inventory by stacking same items on top of each other if possible
- function Inventory.compactInventory()
- for i = 1, 16 do
- local itemI = turtle.getItemDetail(i)
- if itemI and itemI.count < 64 then
- for j = i + 1, 16 do
- local itemJ = turtle.getItemDetail(j)
- if itemJ and itemJ.name == itemI.name then
- turtle.select(j)
- turtle.transferTo(i)
- -- Aktuellen Slot updaten
- itemI = turtle.getItemDetail(i)
- if not itemI or itemI.count == 64 then
- break
- end
- end
- end
- end
- end
- turtle.select(1) -- zum Standard zurückkehren
- end
- -- gets specified item in given amount (min(available, requested)) from chest
- function Inventory.suckSpecificItem(itemName, count, strDir)
- local chest = peripheral.wrap(strDir)
- if not chest or not chest.list then
- return false
- end
- local chestItems = chest.list()
- local foundSlots = {}
- local totalAvailable = 0
- -- 1. Scans chest for requested Item
- for slot, item in pairs(chestItems) do
- if item.name == itemName then
- table.insert(foundSlots, {slot = slot, count = item.count})
- totalAvailable = totalAvailable + item.count
- end
- end
- count = math.min(totalAvailable, count)
- if count == 0 then
- return false
- end
- -- 2. Looks for available Slot for cached item
- local cachedItemSlot = nil
- for i = 1, 16 do
- if turtle.getItemCount(i) == 0 then
- cachedItemSlot = i
- break
- end
- end
- if not cachedItemSlot then
- print("Kein freier Slot zum Zwischenspeichern des Kisten-Items.")
- return false
- end
- turtle.select(cachedItemSlot)
- local cachedItem = nil
- fktSuck = {
- ["front"] = turtle.suck,
- ["top"] = turtle.suckUp,
- ["bottom"] = turtle.suckDown
- }
- fktDrop = {
- ["front"] = turtle.drop,
- ["top"] = turtle.dropUp,
- ["bottom"] = turtle.dropDown
- }
- suck = fktSuck[strDir] or fktSuck["front"]
- drop = fktDrop[strDir] or fktDrop["front"]
- local list = chest.list()
- if list[1] and list[1].name ~= itemName then
- if suck(64) then
- cachedItem = turtle.getItemDetail(cachedItemSlot)
- end
- end
- -- 3. Moves first requested item to slot 1
- local nextSlotIndex = 1
- if not chest.getItemDetail(1) then
- local nextSource = foundSlots[nextSlotIndex]
- chest.pushItems(strDir, nextSource.slot, nextSource.count, 1)
- nextSlotIndex = nextSlotIndex + 1
- elseif (chest.getItemDetail(1) and chest.getItemDetail(1).name == itemName) then
- nextSlotIndex = nextSlotIndex + 1
- end
- -- 4. Returns cached item (returns to first available chest slot ≠ 1)
- if cachedItem then
- turtle.select(cachedItemSlot)
- drop()
- end
- -- 5. Pulls requested items
- local collected = 0
- turtle.select(cachedItemSlot)
- while collected < count do
- local remaining = count - collected
- local slot1Item = chest.getItemDetail(1)
- local pullCount = math.min(slot1Item.count, remaining)
- if suck(pullCount) then
- collected = collected + pullCount
- end
- if collected < count and nextSlotIndex <= #foundSlots then
- local nextSource = foundSlots[nextSlotIndex]
- chest.pushItems(strDir, nextSource.slot, nextSource.count, 1)
- nextSlotIndex = nextSlotIndex + 1
- end
- end
- return true
- end
- return Inventory
Advertisement
Add Comment
Please, Sign In to add comment