Minecraft – How to control computers using rednet? 1.7.10

minecraft-computercraftminecraft-java-edition

I would like to set up a big computercraft network with one main computer using wireless modems and rednet api. Though I was not able to find any tutorials – all of the tutorials I found were using Craft os 1.6 or lower (I am using 1.7). The wiki was not much help either.

Thank you in advance.

Best Answer

Using 3 basic rednet functions - open(side), broadcast(message, protocol) and receive(protocol, timeout), it is possible to implement this.

Control Computer code:

print('Main Computer')
print('  Enter any command to broadcast to slaves')

rednet.open('top') -- Open modem on top

while true do -- Run this forever
  io.write('>>> ')
  command = io.read() -- Read a command from stdin
  if command == 'exit' then -- If it's exit
    rednet.close('top') -- Close top modem
    return -- Exit
  end
  rednet.broadcast(command, 'slave')  -- Otherwise, broadcast the command with protocol slave
end

Slave code:

rednet.open('top') -- Same, open top
while true do -- Run forever
  id, data = rednet.receive('slave') -- Receive message under protocol 'slave'
  print('Received command ' .. data) 
  if data == 'quit' then -- If it's quit
    rednet.close('top') -- Cleanup, close top modem
    print('Quitting...')
    return -- Exit
  elseif string.match(data, 'math .+') then -- Regex: command starts with math
    print('Math:')
    equation = string.match(data, 'math (.+)') -- Regex: get actual equation
    print(equation .. ' = ' .. loadstring('return ' .. equation)()) -- Eval it. I'm not responsible for attacks. 
  end
end