ComputerCraft - Modem*

View/post tutorials for use on our Tekkit server

ComputerCraft - Modem*

Postby Lord Cuddles » 22 Nov 2013, 18:09

Name your program 'modem'
Code: Select all
local tArgs = { ... }

local function printUsage()
  print( "Usage:" )
  print( "modem connect <side>" )
  print( "modem channel <open|close> <channel>" )
  print( "modem channel closeall" )
end

function closeall()
  if connected == true then
    print("Closing all channels...")
    for i = 1, 65535 do
      modem.close(i)
    end
    print("Channels closed.")
  else
    print("Error: No channels to close!")
  end
end

local sCommand = tArgs[1]
if sCommand == "connect" then
  local sParam1 = tArgs[2]
  if sParam1 == "top" or sParam1 == "left" or sParam1 == "right" or sParam1 == "bottom" or sParam1 == "back" or sParam1 == "front" then
    if peripheral.isPresent( sParam1 ) == true then
      if peripheral.getType( sParam1 ) == "modem" then
        local modem = peripheral.wrap( sParam1 )
        print( "Connected!" )
      else
        print( "Error: Chosen peripheral is not a modem!" )
      end
    else
      print( "Error: No peripheral was detected on that side!" )
    end
  elseif sParam1 == "list" then
    print( "Side Names:" )
    print( "- top")
    print( "- left" )
    print( "- right" )
    print( "- front" )
    print( "- back" )
    print( "- bottom" )
  else
    print( "Error: Invalid side!" )
  end
elseif sCommand == "channel" then
  local sParam2 = tArgs[2]
  local sParam3 = tonumber(tArgs[3])
  if sParam2 == "closeall" then
    closeall()
    return
  end
  if sParam3 > 0 and sParam3 < 65536 then
    if sParam2 == "open" then
      modem.open( sParam3 )
      print( "Channel "..sParam3.." opened." )
    elseif sParam2 == "close" then
      modem.close( sParam3 )
      print( "Channel "..sParam3.." closed." )
    else
      print( "Usage: 'modem channel <open|close> <channel>" )
    end
  else
    print( "Error: Invalid channel!" )
  end
else
  printUsage()
end


To use:
Attach a wired or wireless modem to a side, then type into the console: 'modem connect <side>', replacing <side> with the side that the modem is on.
Use 'modem channel open <channel>' to open a channel. You can then use the modem API to send and receive messages, I'll post a tutorial on how to create one of those.

Please note, if you are using a wireless modem then there is a maximum range of about 64 blocks away when not raining. In addition to this, computers with a wireless modem attached and open channels are vulnerable to having their transmissions intercepted, it is much more secure to use wired modems for short and long distances.
Last edited by Disy on 25 Nov 2013, 02:59, edited 4 times in total.
Reason: Added a quick option to close all open channels
Still play Minecraft? Contact me via my website URL in my profile
User avatar
Lord Cuddles
GOD LIKE
  
Name:Lord_Cuddles
Build:Ultra
Survival:Ultra
FTB:Ultra
 
Build portfolio
 
Posts:1417
Joined:12 Nov 2011, 17:27
Location:London - UK

Return to Tekkit Tutorials

Who is online

Users browsing this forum: No registered users and 1 guest
cron