Advertisement
osmarks

thing mover

Mar 29th, 2020
344
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.15 KB | None | 0 0
  1. local inputs = {"minecraft:ironchest_gold_338"}
  2. local storage = {peripheral.find "minecraft:ironchest_iron"}
  3.  
  4. local free_space_cache = {}
  5.  
  6. local function has_free_space(chest)
  7.     if free_space_cache[chest] then return free_space_cache[chest] > 0 end
  8.     local max = chest.size() * 64
  9.     local count = 0
  10.     for slot, content in pairs(chest.list()) do
  11.         count = count + content.count
  12.     end
  13.     free_space_cache[chest] = max - count
  14.     return count < max
  15. end
  16.  
  17. local function move_stack(source, slot, size)
  18.     local remaining = size
  19.     for _, chest in pairs(storage) do
  20.         if has_free_space(chest) then
  21.             local removed = chest.pullItems(source, slot)
  22.             free_space_cache[chest] = free_space_cache[chest] - removed
  23.             remaining = remaining - removed
  24.         end
  25.         if remaining <= 0 then return true end
  26.     end
  27.     return false
  28. end
  29.  
  30. while true do
  31.     for _, input in pairs(inputs) do
  32.         for slot, content in pairs(peripheral.call(input, "list")) do
  33.             print(input, slot, content.count)
  34.             move_stack(input, slot, content.count)
  35.             sleep(0.5)
  36.         end
  37.     end
  38.     sleep(10)
  39. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement