Advertisement
SemlerPDX

Minecraft ComputerCraft Mob Farm Control v1.2

Sep 2nd, 2024 (edited)
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.00 KB | Gaming | 0 0
  1. --Mob Farm Touch Screen Control w/Remote RS Timer PC
  2. --v1.2 by SemlerPDX Sept2024
  3.  
  4. -- pastebin get GyWbhYEK startup
  5.  
  6. --Peripheral Sides
  7. redstoneSide = "bottom"
  8. monitorSide = "front"
  9. detectorSide = "top"
  10. modemSide = "back"
  11.  
  12. --rednet protocol and computer IDs
  13. protocolCMD = "MobFarmCMD"
  14. controlPC = 22 --id of (this) farm control PC
  15. remotePC = 20 --id of remote redstone timer PC
  16.  
  17. --Remote Command Keywords
  18. msgOpenComms = "opencomms"
  19. msgConfirmed = "copycopy"
  20. msgDenied = "samestate"
  21. commandOn = "on"
  22. commandOff = "off"
  23.  
  24. local messageOn = {" "," ","T","U","R","N"," ","O","N"," "," "," "}
  25. local messageOff = {" "," ","T","U","R","N"," ","O","F","F"," "," "}
  26.  
  27. local messageString = messageOn
  28. local commandString = commandOff
  29. local isOnline = 1 -- initial toggle will invert this for "off" state before main loop
  30.  
  31. --Init
  32. term.clear()
  33. term.setCursorPos(1,1)
  34. monitor = peripheral.wrap(monitorSide)
  35. detector = peripheral.wrap(detectorSide)
  36. modem = peripheral.wrap(modemSide)
  37. rednet.open(modemSide)
  38.  
  39.  
  40. --Rednet Receive Requested Message
  41. local function NetReceive(msg)
  42.   id,message = rednet.receive(protocolCMD,timeout)
  43.   if message ~= nil then
  44.     if msg == message then
  45.       return true
  46.     else
  47.       return false
  48.     end
  49.   end  
  50. end
  51.  
  52. --Rednet Send with Receipt Confirmation
  53. local function NetSend(toID,msg,rcv,ccode)
  54.   rednet.send(toID,msg,protocolCMD)
  55.   if rcv then
  56.     returnReceipt = NetReceive(ccode)
  57.     if returnReceipt then
  58.       return true
  59.     else
  60.       return false
  61.     end
  62.   end
  63. end
  64.  
  65. --Toggle Remote RS Timer via Rednet with Return Confirmation
  66. local function ToggleTimer(toID,msg)
  67.   while true do
  68.     commsOpen = NetSend(toID,msgOpenComms,true,msgConfirmed)
  69.     if commsOpen then
  70.       commsOut = NetSend(toID,msg,true,msgConfirmed)
  71.       return commsOut
  72.     end
  73.     sleep(1)
  74.   end
  75. end
  76.  
  77. --Toggle Monitor Display and Mob Farm Online State
  78. local function ToggleState(stateNew)
  79.  
  80.   if stateNew == 1 then
  81.     stateNew = 0
  82.     messageString = messageOn
  83.     commandString = commandOff
  84.     monitor.setBackgroundColor(colors.red)
  85.   else
  86.     stateNew = 1
  87.     messageString = messageOff
  88.     commandString = commandOn
  89.     monitor.setBackgroundColor(colors.green)
  90.   end
  91.  
  92.   rs.setAnalogOutput(redstoneSide,stateNew)
  93.   monitor.clear()
  94.   for y=1,12 do
  95.     monitor.setCursorPos(4,y)
  96.     monitor.write(messageString[y])
  97.   end
  98.  
  99.   ToggleTimer(remotePC,commandString)
  100.  
  101.   return stateNew
  102. end
  103.  
  104. local function DetectTouch()
  105.   event,side,xPos,yPos = os.pullEvent("monitor_touch")
  106. end
  107.  
  108. local function DetectPlayers()
  109.   while true do
  110.     sleep(1)
  111.     playerPresent = detector.isPlayersInRange(8)
  112.     if not playerPresent then
  113.       return false
  114.     end
  115.   end
  116. end
  117.  
  118.  
  119. --MAIN
  120. while true do
  121.   isOnline = ToggleState(isOnline)
  122.  
  123.   if isOnline == 1 then
  124.     controlFunction = parallel.waitForAny(DetectTouch,DetectPlayers)
  125.     if controlFunction == 2 then
  126.       term.write("No player is present - shutting down mob farm...")
  127.     end
  128.   else
  129.     DetectTouch()
  130.   end
  131. end
Tags: minecraft
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement