Advertisement
Blackhome

FunctionTest

Jun 18th, 2025 (edited)
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.72 KB | Gaming | 0 0
  1. -- pastebin get 180C8NRw TakeItem
  2.  
  3. local function suckSpecificItem(itemName, count)
  4.     local chest = peripheral.wrap("front")
  5.     if not chest or not chest.list then
  6.         print("Keine Kiste vor der Turtle erkannt.")
  7.         return false
  8.     end
  9.  
  10.     local chestItems = chest.list()
  11.     local foundSlots = {}
  12.     local totalAvailable = 0
  13.  
  14.     -- 1. Scanne Kiste nach dem gewünschten Item
  15.     for slot, item in pairs(chestItems) do
  16.         if item.name == itemName then
  17.             table.insert(foundSlots, {slot = slot, count = item.count})
  18.             totalAvailable = totalAvailable + item.count
  19.         end
  20.     end
  21.  
  22.     if totalAvailable < count then
  23.         print("Nicht genügend Items gefunden.")
  24.         return false
  25.     end
  26.  
  27.     -- 2. Finde freien Slot in der Turtle für gecachtes Item
  28.     local cachedItemSlot = nil
  29.     for i = 1, 16 do
  30.         if turtle.getItemCount(i) == 0 then
  31.             cachedItemSlot = i
  32.             break
  33.         end
  34.     end
  35.  
  36.     if not cachedItemSlot then
  37.         print("Kein freier Slot zum Zwischenspeichern des Kisten-Items.")
  38.         return false
  39.     end
  40.  
  41.     turtle.select(cachedItemSlot)
  42.     local cachedItem = nil
  43.  
  44.     local list = chest.list()
  45.     if list[1] and list[1].name ~= itemName then
  46.         if turtle.suck(64) then
  47.             cachedItem = turtle.getItemDetail(cachedItemSlot)
  48.         end
  49.     end
  50.  
  51.     -- 3. Erstes gewünschtes Item in Slot 1 schieben
  52.     local nextSlotIndex = 1
  53.     if not chest.getItemDetail(1) then
  54.         local nextSource = foundSlots[nextSlotIndex]
  55.         chest.pushItems("front", nextSource.slot, nextSource.count, 1)
  56.         nextSlotIndex = nextSlotIndex + 1
  57.     elseif (chest.getItemDetail(1) and chest.getItemDetail(1).name == itemName) then
  58.         nextSlotIndex = nextSlotIndex + 1
  59.     end
  60.  
  61.     -- 4. Gecachtes Item zurückgeben
  62.     if cachedItem then
  63.         turtle.select(cachedItemSlot)
  64.         turtle.drop() -- Kiste verteilt es automatisch auf einen freien Slot ≠ 1
  65.     end
  66.  
  67.     -- 5. Zielitems absaugen
  68.     local collected = 0
  69.     turtle.select(cachedItemSlot) -- Wiederverwenden für Einsammeln
  70.     while collected < count do
  71.         local remaining = count - collected
  72.         local slot1Item = chest.getItemDetail(1)
  73.         local pullCount = math.min(slot1Item.count, remaining)
  74.  
  75.         if turtle.suck(pullCount) then
  76.             collected = collected + pullCount
  77.         end
  78.  
  79.         if collected < count and nextSlotIndex <= #foundSlots then
  80.             local nextSource = foundSlots[nextSlotIndex]
  81.             chest.pushItems("front", nextSource.slot, nextSource.count, 1)
  82.             nextSlotIndex = nextSlotIndex + 1
  83.         end
  84.     end
  85.  
  86.     return true
  87. end
  88.  
  89. suckSpecificItem("minecraft:chest", 200)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement