Thursday, October 10, 2013

LUA program for ComputerCraft to send server messages

Today I updated a quick program I made to be able to send server messages to the Minecraft server. It uses an Advanced Computer and a Command Block. Of course you need to enable Command Blocks in the server.properties file and in the config file for ComputerCraft. The program actually consists of two programs. One is the 'startup' program to actually send the server messages and the other is the 'Broadcast' program. This last program will ask you on which side of the computer you place the command block, how many messages you want it to send, what the messages are and at what time interval you want the messages to be sent. Those variables are then stored in a file called 'Variables' and it will run the 'startup' program. The 'startup' program will check if the 'Variables' file is available and if so it will run using those variables. If the 'Variables' file is not available it will run the 'Broadcast' file to ask for the variables needed and then store them to run 'startup' again. The file actually sending the messages is of course called 'startup' so it will continue to run even after a server restart. To stop it from running and change the variables, just hold <ctrl> + T for a few seconds to terminate and then run 'Broadcast'.

Here are the pastebin links for both files needed:
'startup': http://pastebin.com/vsA51u6Q
'Broadcast': http://pastebin.com/Fgh82w5y

Click below (read more) to read the full code for both files.


****************************************
-- Call this program 'startup'
-- This program needs a second program called 'Broadcast'
-- This program will send server messages and needs command blocks enabled in both CC and server.properties

local function CheckVariables()
  if fs.exists("Variables") then
    readData = fs.open("Variables", "r")
    Side = readData.readLine()
    Amount = readData.readLine()
    Repeat = readData.readLine()
    for i = 1, Amount do
      Message[i] = readData.readLine()
    end
    readData.close()
  else
    term.setTextColor(colors.red)
    print("File Variables not found!")
    sleep(5)
    shell.run("Broadcast")
  end
end

local function Broadcast()
  Interval = Repeat*60
  commandBlock = peripheral.wrap(Side)
  for i = 1, Amount do
    commandBlock.setCommand("/say "..Message[i])
    commandBlock.runCommand()
    sleep(Interval)
  end
  Broadcast()
end

term.clear()
term.setCursorPos(1,1)
term.setTextColor(colors.white)
Message = {}
CheckVariables()
print("Now broadcasting messages to the server!")
Broadcast()

****************************************
-- Call this program 'Broadcast'
-- it needs a second program to be called 'startup'
-- This program will ask for messages to send as the server console and needs a command block (enabled in CC and server.properties)
local function Variables()
  print("Command block will be on which side of the computer?")
  print("left, right, front, back, top or bottom?")
  Side = read()
  print("How many different messages do you want to alternate?")
  Amount = read()
  Message = {}
  for i = 1, Amount do
    print("Message("..i..") to be broadcast?")
    Message[i] = read()
  end
  print("Repeat in minutes?")
  Repeat = read()
  print()
  print("I will broadcast the following messages:")
  for i = 1, Amount do
    print(Message[i])
  end
  print("Interval will be "..Repeat.." minutes.")
end

local function StoreData()
  writeData = fs.open("Variables", "w")
  writeData.writeLine(Side)
  writeData.writeLine(Amount)
  writeData.writeLine(Repeat)
  for i = 1, Amount do
    writeData.writeLine(Message[i])
  end
  writeData.close()
  term.setTextColor(colors.red)
  print("Data stored!")
  sleep(5)
end

local function Broadcast()
  shell.run("startup")
end

term.clear()
term.setCursorPos(1,1)
term.setTextColor(colors.white)
Variables()
StoreData()
Broadcast()

No comments:

Post a Comment