BigBlow_

MobFarmController_Slave

Mar 18th, 2025 (edited)
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.92 KB | None | 0 0
  1. -- CONFIGURATION --
  2. local redstoneSide = "bottom"     -- Side where redstone is connected
  3. local listenChannel = 128         -- Wireless modem listen channel
  4. local inverted = false            -- Invert redstone
  5.  
  6. -- Function to request and store the spawner ID
  7. local function askForID()
  8.     local idFile = "id_spawner.txt"
  9.  
  10.     -- Check if the ID file already exists
  11.     if fs.exists(idFile) then
  12.         local file = fs.open(idFile, "r")
  13.         local id = tonumber(file.readAll())
  14.         file.close()
  15.         return id
  16.     else
  17.         write("Enter the spawner ID: ")
  18.         local id = tonumber(read())
  19.  
  20.         local file = fs.open(idFile, "w")
  21.         file.write(tostring(id))
  22.         file.close()
  23.  
  24.         return id
  25.     end
  26. end
  27.  
  28. -- Ask for the spawner ID (and save it if first launch)
  29. local SPAWNER_ID = askForID()
  30.  
  31. local modem = peripheral.find("modem")
  32. if not modem then
  33.     error("No modem detected")
  34. end
  35.  
  36. modem.open(listenChannel) -- Open listening channel
  37. redstone.setOutput(redstoneSide, false) -- Disable redstone by default
  38.  
  39. -- Function to get the current time in HH:mm:ss_DD/MM format
  40. local function getTimestamp()
  41.     local t = os.date("*t")
  42.     return string.format("%02d:%02d:%02d_%02d/%02d", t.hour, t.min, t.sec, t.day, t.month)
  43. end
  44.  
  45. print("Waiting for commands for spawner ID: " .. SPAWNER_ID)
  46.  
  47. while true do
  48.     local event, side, senderChannel, replyChannel, message = os.pullEvent("modem_message")
  49.  
  50.     if type(message) == "table" and message.id == SPAWNER_ID then
  51.         local state = message.state
  52.  
  53.         -- Apply inverted mode
  54.         if inverted then
  55.             state = not state
  56.         end
  57.  
  58.         redstone.setOutput(redstoneSide, state)
  59.  
  60.         local timestamp = getTimestamp()
  61.         print("[" .. timestamp .. "] Spawner " .. SPAWNER_ID .. " " ..
  62.             ((state and not inverted) or (not state and inverted) and "activated" or "deactivated"))
  63.     end
  64. end
  65.  
Advertisement
Add Comment
Please, Sign In to add comment