Advertisement
iMajesticButter

Untitled

Feb 14th, 2022
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. local modem = peripheral.find("modem", rednet.open);
  2.  
  3. local logging = "Potion_Brewer_LOGGER"
  4.  
  5. local errorName = "DISPENSER_CONTROLLER"
  6.  
  7. -- different protocols used by this program
  8. local dispenserRequestProtocol = "Potion_Brewer_Dispenser_Request_v001"
  9. local dispenserNodeProtocol = "Potion_Brewer_Ingredient_Dispenser_Node_v001"
  10. local beltSelectorProtocol = "Potion_Brewer_BELT_SELECTOR"
  11. local scannerProtocol = "Potion_Brewer_SCANNER"
  12.  
  13. local timeoutErrorProtocol = "Potion_Brewer_ERROR_PROTOCOL_TIMEOUT"
  14.  
  15. -- different event names used by this program (lets just use protocols in future)
  16. local eventDispenseItem = "EVENT_DISPENSE_"
  17. local eventDispenseItemStarted = "EVENT_DISPENSE_STARTED_"
  18. local eventSelectBelt = "EVENT_BELT_SELECT_"
  19. local eventScanner = "EVENT_SCANNER_"
  20.  
  21. local scannerDispenser = "dispenser"
  22.  
  23. local timeoutTime = 10
  24.  
  25. local error = false
  26.  
  27. function log(string)
  28. print(string)
  29. rednet.broadcast("[" .. errorName .. "] " ..string, logging)
  30. end
  31.  
  32. function dispenseItem(string)
  33.  
  34. parallel.waitForAny(
  35. function()
  36. log("attempting to dispense " .. string)
  37.  
  38. -- requrest that the item is dispensed
  39. rednet.broadcast(eventDispenseItem .. string, dispenserNodeProtocol)
  40. local message = ""
  41. local senderID = 0
  42.  
  43. log("dispense request sent to nodes")
  44.  
  45. -- wait for the response from the dispenser node
  46. while message ~= eventDispenseItemStarted .. string do
  47. senderID, message = rednet.receive(dispenserNodeProtocol)
  48. end
  49.  
  50. log("response received")
  51.  
  52. message = ""
  53. -- wait for the item to be detected on the conveyor
  54. while message ~= eventScanner .. scannerDispenser do
  55. senderID, message = rednet.receive(scannerProtocol)
  56. end
  57.  
  58. log("item detected! dispense successful!")
  59. end,
  60. function()
  61. -- wait for timeout
  62. sleep(timeoutTime)
  63. log("Dispense Item Timeout! Took longer than " .. timeoutTime .. " seconds!")
  64. rednet.broadcast(errorName, timeoutErrorProtocol)
  65. error = true
  66. end)
  67.  
  68. end
  69.  
  70. dispenseItem("netherwart")
  71.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement