Advertisement
osmarks

Fancy Remote Dispenser

Jun 2nd, 2019
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.98 KB | None | 0 0
  1. local source = "turtle_2187"
  2. local dispense_from = "minecraft:ironchest_iron_1508"
  3. local monitor = peripheral.wrap "monitor_2978"
  4. local button = "top"
  5. local dispense_count = 16
  6. local dispense_direction = "west"
  7. local size = peripheral.call(dispense_from, "size")
  8. local stack_size = 64
  9. local display_name = "Melon"
  10.  
  11. local function mon_write(...)
  12.     monitor.setTextScale(1)
  13.     local oldterm = term.redirect(monitor)
  14.     term.clear()
  15.     term.setCursorPos(1, 1)
  16.     write(...)
  17.     term.redirect(oldterm)
  18. end
  19.  
  20. local function fill_chest()
  21.     while true do
  22.         local current_contents = peripheral.call(dispense_from, "list")
  23.  
  24.         local item_count = 0
  25.         local can_fill = true
  26.         for slot = 1, size do
  27.             local slot_contents = current_contents[slot]
  28.  
  29.             if slot_contents and slot_contents.count then
  30.                 item_count = item_count + slot_contents.count
  31.             end
  32.             if can_fill and (not slot_contents or slot_contents.count < stack_size) then
  33.                 print("Filling slot", slot)
  34.                 local source_contents = peripheral.call(source, "list")
  35.                 local moved
  36.                 for source_slot in pairs(source_contents) do
  37.                     moved = peripheral.call(source, "pushItems", dispense_from, source_slot, nil, slot)
  38.                 end
  39.                 if moved then
  40.                     print("Moved", moved, "items")
  41.                 else
  42.                     print("Insufficient items in source")
  43.                     can_fill = false
  44.                 end
  45.             end
  46.         end
  47.  
  48.         mon_write(("%dx %s stored\n\nPress button for %dx %s"):format(item_count, display_name, dispense_count, display_name))
  49.         sleep(5)
  50.     end
  51. end
  52.  
  53. local function handle_button()
  54.     while true do
  55.         os.pullEvent "redstone"
  56.         if redstone.getInput(button) then
  57.             local contents = peripheral.call(dispense_from, "list")
  58.             for slot, stack in pairs(contents) do
  59.                 if stack.count > dispense_count then
  60.                     print("Dispensing", dispense_count, "from", slot, "to", dispense_direction)
  61.                     peripheral.call(dispense_from, "drop", slot, dispense_count, dispense_direction)
  62.                     break
  63.                 end
  64.             end
  65.         end
  66.     end
  67. end
  68.  
  69. parallel.waitForAll(handle_button, fill_chest)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement