Advertisement
osmarks

AutoMelon™

Mar 28th, 2020
667
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local storage = peripheral.find "minecraft:ender chest"
  2. local monitor = peripheral.find "monitor"
  3. local button = settings.get "melon.button" or "right"
  4. local dispense_count = 16
  5. local dispense_direction = settings.get "melon.dispense" or "west"
  6. local display_name = "Melon"
  7. local to_store = "minecraft:melon"
  8.  
  9. local function mon_write(...)
  10.     monitor.setTextScale(1)
  11.     local oldterm = term.redirect(monitor)
  12.     term.clear()
  13.     term.setCursorPos(1, 1)
  14.     print "GTech AutoMelon"
  15.     write(...)
  16.     term.redirect(oldterm)
  17. end
  18.  
  19. local function fill_chest()
  20.     while true do
  21.         local count = 0
  22.         for slot, stack in pairs(storage.list()) do
  23.             if stack.name == to_store then
  24.                 count = count + stack.count
  25.             end
  26.         end
  27.         mon_write(("%dx %s stored\nPress button for %dx %s"):format(count, display_name, dispense_count, display_name))
  28.         local timer = os.startTimer(5)
  29.         while true do
  30.             local ev, param = os.pullEvent()
  31.             if (ev == "timer" and param == timer) or ev == "refresh_storage" then break end
  32.         end
  33.     end
  34. end
  35.  
  36. local function handle_button()
  37.     while true do
  38.         os.pullEvent "redstone"
  39.         if redstone.getInput(button) then
  40.             local contents = storage.list()
  41.             for slot, stack in pairs(contents) do
  42.                 if stack.count > dispense_count then
  43.                     print("Dispensing", dispense_count, "from", slot, "to", dispense_direction)
  44.                     storage.drop(slot, dispense_count, dispense_direction)
  45.                     os.queueEvent("refresh_storage")
  46.                     break      
  47.                 end
  48.             end
  49.         end
  50.     end
  51. end
  52.  
  53. parallel.waitForAll(handle_button, fill_chest)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement